繁体   English   中英

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

[英]How to email out data from a csv file with Sendgrid using python

我正在使用 Sendgrid 向特定部门发送电子邮件,但我希望电子邮件包含来自 csv 文件的数据。 根据我的理解,Sendgrid 使用 HTML。 如何抓取csv 文件并使用 Sendgrid 发送它?

message = Mail(
    from_email='noreply@gmail.com',
    to_emails='test@gmail.com',
    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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM