Acceptance Criteria:
Reference:
Do you want someone to write a script that demonstrates the ability to send a single email to multiple recipients?
Hi @DannyLee12,
This issue is part of this project: https://github.com/sendgrid/sendgrid-python/projects/1
It is a refactor of the Mail Helper to make it more user friendly by hiding the underlying API as much as possible.
For example, right now to send a single email to multiple recipients, we need to do this:
def build_email():
mail = Mail()
mail.set_from(Email("[email protected]", "Example User"))
mail.set_subject("Hello World from the SendGrid Python Library")
personalization = Personalization()
personalization.add_to(Email("[email protected]", "Example User"))
personalization.add_to(Email("[email protected]", "Example User"))
mail.add_personalization(personalization)
mail.add_content(Content("text/plain", "some text here"))
mail.add_content(Content("text/html", "<html><body>some text here</body></html>"))
return mail.get()
def send_email():
sg = SendGridAPIClient()
data = build_email()
response = sg.client.mail.send.post(request_body=data)
print(response.status_code)
print(response.headers)
print(response.body)
send_email()
When something like this would be nicer to work with:
email = Email(SendGridAPIClient(apiKey))
email.set_from("[email protected]", "Example User")
// or you can pass a dict to email.add_tos()
email.add_to("[email protected]", "Example User")
email.add_to("[email protected]", "Example User")
email.set_subject("Hello World from the SendGrid Python Library")
email.set_content("text/plain", "some text here")
if email.verify():
response = email.send()
Hello - I'm using the example mail script (found here - https://github.com/sendgrid/sendgrid-python). However, if I have a recipient list made (through the v3 API), how can I link the data in the list to the above code so that it sends an email to all the recipients in the list? I want to avoid manually inputting each email address with personalization.add_to(Email("[email protected]", "Example User"))
Also, how could I format the code so that each email is addressed to the person's first name?
Hello @carolinapeisch,
With the recipient list, you would use the contactdb resource (https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html) to pull the emails you would like to send to, plugging them into the personalizations as needed.
With regards to formatting the code so that each email is addressed to the person's first name, please check out the full example here: https://github.com/sendgrid/sendgrid-python/blob/master/examples/helpers/mail/mail_example.py#L20 (You would replace Example User with the first name)
I hope that gets you pointed in the right direction. Please let me know if you have further questions.
With Best Regards,
Elmer
Hi Elmer,
Thank you so much for your response! Unfortunately, I'm still confused how
to take my list of recipients and "plugging them into the personalizations
as needed" - if my list of recipients is called "School_Emails", where in
the attached script would I reference that list?
I really appreciate all your help.
Best,
Carolina
On Thu, Jan 26, 2017 at 12:13 AM, Elmer Thomas notifications@github.com
wrote:
Hello @carolinapeisch https://github.com/carolinapeisch,
With the recipient list, you would use the contactdb resource (
https://sendgrid.com/docs/API_Reference/Web_API_v3/
Marketing_Campaigns/contactdb.html) to pull the emails you would like to
send to, plugging them into the personalizations as needed.With regards to formatting the code so that each email is addressed to the
person's first name, please check out the full example here:
https://github.com/sendgrid/sendgrid-python/blob/master/
examples/helpers/mail/mail_example.py#L20 (You would replace Example User
with the first name)I hope that gets you pointed in the right direction. Please let me know if
you have further questions.With Best Regards,
Elmer
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/sendgrid/sendgrid-python/issues/225#issuecomment-275309613,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AJLRK4lQgwubFMsABPzjy6MlgZYe5fGoks5rWCuRgaJpZM4KEn53
.
sg = sendgrid.SendGridAPIClient(apikey='REDACTED')
from_email = Email("[email protected]")
subject = "Your Weekly Movemeant Updates!"
to_email = Email("[email protected]")
content = Content("text/html", " ")
mail = Mail(from_email, subject, to_email, content)
mail.personalizations[0].add_(Email("[email protected]"))
mail.personalizations[0].add_substitution(Substitution("-cohort-", "Cornell Tech"))
mail.personalizations[0].add_substitution(Substitution("-1-", one))
mail.personalizations[0].add_substitution(Substitution("-2-", two))
mail.personalizations[0].add_substitution(Substitution("-3-", three))
mail.personalizations[0].add_substitution(Substitution("-4-", four))
mail.personalizations[0].add_substitution(Substitution("-5-", five))
mail.personalizations[0].add_substitution(Substitution("-id_1-", id_1))
mail.personalizations[0].add_substitution(Substitution("-id_2-", id_2))
mail.personalizations[0].add_substitution(Substitution("-id_3-", id_3))
mail.personalizations[0].add_substitution(Substitution("-id_4-", id_4))
mail.personalizations[0].add_substitution(Substitution("-id_5-", id_5))
mail.personalizations[0].add_substitution(Substitution("-nyc-", nyc_map))
mail.personalizations[0].add_substitution(Substitution("-visit_number-", visits_to_date))
mail.personalizations[0].add_substitution(Substitution("-common-", common_venues))
mail.personalizations[0].add_substitution(Substitution("-charturl-", category_chart))
mail.set_template_id("758827dd-3b0b-49e9-b67d-491aefee9ee5")
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)
@carolinapeisch I think I know what's going on here. The recipient list functionality is specific to the Marketing Campaigns functionality. Right now, there's not a cross over of a recipient list from Marketing Campaigns to the v3/mail/send (transactional) API endpoint.
So, instead of using /v3/mail/send, you would use the "Create Campaign" endpoint to attach that recpient list, to use a template, and create the campaign.
Sorry for the confusion!
Any new API improvement to this issue? It that possible to create a class Emails to wrap multiple recipients? I could do this.
Hello @shelling,
The first step is to propose the new Mail Helper interface, like so.
Thanks for your interest and we look forward to collaborating with you!
With Best Regards,
Elmer
I think we can put it in a for loop and add emails one at a time
for email in ["[email protected]", "[email protected]"]:
email.add_to(email, "Example User")
email.add_to(email, "Example User")
This has been moved here.
Hi Elmer, Thank you so much for your response! Unfortunately, I'm still confused how to take my list of recipients and "plugging them into the personalizations as needed" - if my list of recipients is called "School_Emails", where in the attached script would I reference that list? I really appreciate all your help. Best, Carolina
On Thu, Jan 26, 2017 at 12:13 AM, Elmer Thomas @.*> wrote: Hello @carolinapeisch https://github.com/carolinapeisch, With the recipient list, you would use the contactdb resource ( https://sendgrid.com/docs/API_Reference/Web_API_v3/ Marketing_Campaigns/contactdb.html) to pull the emails you would like to send to, plugging them into the personalizations as needed. With regards to formatting the code so that each email is addressed to the person's first name, please check out the full example here: https://github.com/sendgrid/sendgrid-python/blob/master/ examples/helpers/mail/mail_example.py#L20 (You would replace Example User with the first name) I hope that gets you pointed in the right direction. Please let me know if you have further questions. With Best Regards, Elmer — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#225 (comment)>, or mute the thread https://github.com/notifications/unsubscribe-auth/AJLRK4lQgwubFMsABPzjy6MlgZYe5fGoks5rWCuRgaJpZM4KEn53 .MAIL SCRIPT sg = sendgrid.SendGridAPIClient(apikey='REDACTED') from_email = Email("[email protected]") subject = "Your Weekly Movemeant Updates!" to_email = Email("[email protected]") content = Content("text/html", " ") mail = Mail(from_email, subject, to_email, content) mail.personalizations[0].add_(Email("[email protected]")) mail.personalizations[0].add_substitution(Substitution("-cohort-", "Cornell Tech")) mail.personalizations[0].add_substitution(Substitution("-1-", one)) mail.personalizations[0].add_substitution(Substitution("-2-", two)) mail.personalizations[0].add_substitution(Substitution("-3-", three)) mail.personalizations[0].add_substitution(Substitution("-4-", four)) mail.personalizations[0].add_substitution(Substitution("-5-", five)) mail.personalizations[0].add_substitution(Substitution("-id_1-", id_1)) mail.personalizations[0].add_substitution(Substitution("-id_2-", id_2)) mail.personalizations[0].add_substitution(Substitution("-id_3-", id_3)) mail.personalizations[0].add_substitution(Substitution("-id_4-", id_4)) mail.personalizations[0].add_substitution(Substitution("-id_5-", id_5)) mail.personalizations[0].add_substitution(Substitution("-nyc-", nyc_map)) mail.personalizations[0].add_substitution(Substitution("-visit_number-", visits_to_date)) mail.personalizations[0].add_substitution(Substitution("-common-", common_venues)) mail.personalizations[0].add_substitution(Substitution("-charturl-", category_chart)) mail.set_template_id("758827dd-3b0b-49e9-b67d-491aefee9ee5") 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)
Hi Elmer, Thank you so much for your response! Unfortunately, I'm still confused how to take my list of recipients and "plugging them into the personalizations as needed" - if my list of recipients is called "School_Emails", where in the attached script would I reference that list? I really appreciate all your help. Best, Carolina
On Thu, Jan 26, 2017 at 12:13 AM, Elmer Thomas @.*> wrote: Hello @carolinapeisch https://github.com/carolinapeisch, With the recipient list, you would use the contactdb resource ( https://sendgrid.com/docs/API_Reference/Web_API_v3/ Marketing_Campaigns/contactdb.html) to pull the emails you would like to send to, plugging them into the personalizations as needed. With regards to formatting the code so that each email is addressed to the person's first name, please check out the full example here: https://github.com/sendgrid/sendgrid-python/blob/master/ examples/helpers/mail/mail_example.py#L20 (You would replace Example User with the first name) I hope that gets you pointed in the right direction. Please let me know if you have further questions. With Best Regards, Elmer — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#225 (comment)>, or mute the thread https://github.com/notifications/unsubscribe-auth/AJLRK4lQgwubFMsABPzjy6MlgZYe5fGoks5rWCuRgaJpZM4KEn53 .MAIL SCRIPT sg = sendgrid.SendGridAPIClient(apikey='REDACTED') from_email = Email("[email protected]") subject = "Your Weekly Movemeant Updates!" to_email = Email("[email protected]") content = Content("text/html", " ") mail = Mail(from_email, subject, to_email, content) mail.personalizations[0].add_(Email("[email protected]")) mail.personalizations[0].add_substitution(Substitution("-cohort-", "Cornell Tech")) mail.personalizations[0].add_substitution(Substitution("-1-", one)) mail.personalizations[0].add_substitution(Substitution("-2-", two)) mail.personalizations[0].add_substitution(Substitution("-3-", three)) mail.personalizations[0].add_substitution(Substitution("-4-", four)) mail.personalizations[0].add_substitution(Substitution("-5-", five)) mail.personalizations[0].add_substitution(Substitution("-id_1-", id_1)) mail.personalizations[0].add_substitution(Substitution("-id_2-", id_2)) mail.personalizations[0].add_substitution(Substitution("-id_3-", id_3)) mail.personalizations[0].add_substitution(Substitution("-id_4-", id_4)) mail.personalizations[0].add_substitution(Substitution("-id_5-", id_5)) mail.personalizations[0].add_substitution(Substitution("-nyc-", nyc_map)) mail.personalizations[0].add_substitution(Substitution("-visit_number-", visits_to_date)) mail.personalizations[0].add_substitution(Substitution("-common-", common_venues)) mail.personalizations[0].add_substitution(Substitution("-charturl-", category_chart)) mail.set_template_id("758827dd-3b0b-49e9-b67d-491aefee9ee5") 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)
I am still having exactly the same issues!
I am new to SendGrid! Do you have a working example @carolinapeisch? That would be so wonderful!
A lot of links are broken here to follow the conversations!
Thanks so much!
Best wishes!
Most helpful comment
I think we can put it in a for loop and add emails one at a time