使用sendgrid python安排电子邮件

时间:2016-11-22 02:50:44

标签: python python-2.7 sendgrid email-integration

我正在使用sendgrid(https://github.com/sendgrid/sendgrid-python)的python模块来发送交易电子邮件。我需要语法帮助才能在预定时间发送电子邮件。 一般的sendgrid文档要求将json修改为" {“send_at”:1409348513}"。我相信这个json不能直接在sendgrid-python中访问。我需要使用python库做同等事情的语法。

我当前的代码相当于下面复制的代码。如果某人可以建议如何修改此代码以在特定时间安排它,那将是很好的,例如datetime.dateime.now()+ datetime.timedelta(days = 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

2 个答案:

答案 0 :(得分:3)

send_atpersonalizations对象的一个​​组件,因此您可以在该级别定义它,允许您为每个收件人/个性化集设置不同的发送时间。

如果您不需要,也可以设置at the top mail level

答案 1 :(得分:0)

看起来您应该可以按照this示例执行此操作。您可以指定所需的所有原始字段。