如何使用python用Sendgrid从csv文件中发送数据 [英] How to email out data from a csv file with Sendgrid using python
问题描述
我正在使用Sendgrid向特定部门发送电子邮件,但我希望该电子邮件包含来自csv文件的数据.据我了解,Sendgrid使用HTML.怎样才能抓取一个csv文件并使用Sendgrid发送它?
I am using Sendgrid to send out emails to specific departments but I want the email to include data from a csv file. From my understanding, Sendgrid works with HTML. How would it be possible to scrap a csv file and send it using 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库,但收到一个错误.我确实更改了该帖子的电子邮件地址.
I tried using the csv library but received an error. I did change the email address for this post.
推荐答案
您可以使用pandas读取csv并使用io解析为html字符串...稍后再在html_content内添加变量html_str ...以供使用熊猫只需使用pip install pandas安装
You can use pandas for read the csv and parse to html string using io...and later to add variable html_str inside of your html_content...for use pandas just install with 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)
这篇关于如何使用python用Sendgrid从csv文件中发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!