SendGrid Multiple Recipients Python Json

939 views Asked by At

I am trying to write a Python script using SendGrid to send personalized emails to approx 30 people. Instead of inputting each individual's first name and email address under a substitution (as I have below), isn't there a way to link to a JSON header file (let's call it names.json) that would have all the information and that python would pull from? How and where would I reference the JSon file that would pull each person's name and email address?

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("[email protected]")
subject = "Your Monthly Updates!"
to_email = Email("[email protected]")
content = Content("text/plain", " ")
mail = Mail(from_email, subject, to_email, content)


mail.personalizations[0].add_to(Email("[email protected]"))
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

There are 0 answers