-
Notifications
You must be signed in to change notification settings - Fork 714
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Send a Single Email to Multiple Recipients #225
Comments
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 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 ***@***.***> 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)
|
@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 |
I think we can put it in a for loop and add emails one at a time
|
This has been moved here. |
I am still having exactly the same issues! |
Acceptance Criteria:
Reference:
The text was updated successfully, but these errors were encountered: