如何使用 python 通过 Sendgrid 通过电子邮件发送 csv 文件中的数据

2024-03-26

我正在使用 Sendgrid 向特定部门发送电子邮件,但我希望电子邮件包含 csv 文件中的数据。据我了解,Sendgrid 使用 HTML。怎么可能scrape一个 csv 文件并使用 Sendgrid 发送它?

message = Mail(
    from_email='[email protected] /cdn-cgi/l/email-protection',
    to_emails='[email protected] /cdn-cgi/l/email-protection',
    subject='New User CAF',
    html_content= """<p>This is to inform IT that {Employee Name} will be starting at {PC} on {Effective Date}. Their supervisor is {Supervisor} and their manager is {Manager 2 Name}. Their title is {Title}.</br>
    </br>
    Office 365: {O365}</br>
    Laptop: {Computer}
    """)

with open("contacts.csv") as file:
        reader = csv.reader(file)
        # skip first header row
        next(reader)

我尝试使用 csv 库但收到错误。我确实更改了这篇文章的电子邮件地址。


您可以使用 pandas 读取 csv 并使用 io 解析为 html 字符串...然后在 html_content 中添加变量 html_str ...要使用 pandas 只需使用 pip install pandas 安装

import pandas as pd
import io

str_io = io.StringIO()
df = pd.read_csv('file.csv')
df.to_html(buf=str_io)
html_str = str_io.getvalue()
print(html_str)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 python 通过 Sendgrid 通过电子邮件发送 csv 文件中的数据 的相关文章

随机推荐