使用sendgrid python安排电子邮件
作者:互联网
我正在使用sendgrid(https://github.com/sendgrid/sendgrid-python)的python模块发送事务性电子邮件.我需要有关语法的帮助,以便在计划的时间发送电子邮件.
一般的sendgrid文档要求将json修改为“ {{send_at”:1409348513}”.我相信这个json无法在sendgrid-python中直接访问.我需要使用python库做等效操作的语法.
我当前的代码等效于下面复制的代码.如果有人可以建议如何修改此代码以在特定时间安排它,那就太好了,例如datetime.dateime.now()datetime.timedelta(天数= 1)
import sendgrid
from sendgrid.helpers.mail import Email, Content, Substitution, Mail
import urllib2 as urllib
def send_email_custom():
sg = sendgrid.SendGridAPIClient(apikey=myApiKey)
from_email = Email(sendEmail)
to_email = Email(custEmail)
reply_to_email = Email(receiveEmail)
content = Content("text/html", "Introduction")
mail = Mail(from_email, subject="Hi!", to_email=to_email, content=content)
mail.personalizations[0].add_substitution(Substitution("_firstName_", firstName))
mail.set_template_id(templateId)
try:
response = sg.client.mail.send.post(request_body=mail.get())
except urllib.HTTPError as e:
print(e.read())
return False
if response.status_code >=200 and response.status_code < 300:
return True
else:
return False
解决方法:
send_at是个性化对象的组成部分,因此您可以在该级别定义它,从而可以为每个收件人/个性化设置设置不同的发送时间.
如果不需要,也可以将其设置为at the top mail level.
标签:email-integration,python-2-7,sendgrid,python 来源: https://codeday.me/bug/20191112/2024102.html