我正在尝试使用Sendgrid发送电子邮件,并将csv中的数据添加到电子邮件的正文中。我可以使用smtplib来完成此过程,但现在我需要使用sendgrid来完成此过程。
import os
import pandas as pd
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
db = pd.read_csv('changes.csv', delimiter = ",", skiprows=6)
db["Email"] = "add@test.com"
db = db.drop_duplicates(subset = ["Employee Name"], keep = "first")
field1 = db["Field Desc"] == "Status"
field2 = db["Field Desc"] == "Default Cost Center 10 (Job)"
message = Mail(
from_email='test@test.com',
to_emails='test@test.com',
subject='{Employee} -- Kronos Change',
html_content='''\
We have received the change for {Employee}.
Title: {Title}
Old: {Old}
New: {New}
Profit Center: {PC}
Supervisor: {Supervisor}
''')
message.content = Content("text","changes.csv")
try:
sg=SendGridAPIClient(os.environ.get('API KEY'))
response = sg.send(message)
print(response.status_code)
print(response.body)
print(response.headers)
except Exception as e:
print(e.message)
我不确定调用message.content来读取csv文件是否正确。当我使用smtplib时,其中一些代码对我有效。
1条答案
按热度按时间62lalag41#
使用以下两个函数:
调用函数read_csv_email(csv_path)