SendGrid多个收件人Python Json

时间:2016-12-30 19:41:02

标签: python json sendgrid sendgrid-api-v3

我正在尝试使用SendGrid编写一个Python脚本,以便向大约30人发送个性化电子邮件。而不是在替换下输入每个人的名字和电子邮件地址(如下所示),是不是有办法链接到JSON头文件(让我们称之为names.json )那将有所有信息和python将拉出来?我将如何以及在何处引用可以提取每个人姓名和电子邮件地址的JSon文件?

import sendgrid
import os
from sendgrid.helpers.mail import Email, Content, Substitution, Mail
try:
    # Python 3
    import urllib.request as urllib
except ImportError:
    # Python 2
    import urllib2 as urllib

sg = sendgrid.SendGridAPIClient([API_KEY])
from_email = Email("sender@gmail.com")
subject = "Your Monthly Updates!"
to_email = Email("john@gmail.com")
content = Content("text/plain", " ")
mail = Mail(from_email, subject, to_email, content)


mail.personalizations[0].add_to(Email("Jane@gmail.com"))
mail.personalizations[0].add_substitution(Substitution("-name-", "John"))
mail.personalizations[0].add_substitution(Substitution("-name-", "Jane"))

mail.set_template_id(TEMPLATE_ID)

try:
    response = sg.client.mail.send.post(request_body=mail.get())
except urllib.HTTPError as e:
    print e.read()
    exit()
print(response.status_code)
print(response.body)
print(response.headers)

0 个答案:

没有答案