2016-11-22 88 views
2

我使用sendgrid的python模块(https://github.com/sendgrid/sendgrid-python)发送交易电子邮件。我需要在预定时间发送电子邮件的语法方面的帮助。 一般sendgrid文档要求修改json为“{”send_at“:1409348513}”。我相信这个json不能在sendgrid-python中直接访问。我需要语法来做与python库等价的事情。使用sendgrid计划电子邮件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 
+0

我会建议发布此github回购,而不是SO – bwest

回答

3

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

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

+1

谢谢,这就是我正在寻找的 – Sharad

+0

你能显示可用的代码吗?我一直在运行非常类似的代码与调度不工作。 – killerbarney

0

看起来你应该能够做到这一点通过以下this例子。您可以指定所有您需要的原始字段。

+0

谢谢,但这是一个完全不同的方法。它直接将它填充到json中。我在想,应该有一些直观的功能,如mail.send_at()类的东西 – Sharad

相关问题