Skip to content
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

Closed
thinkingserious opened this issue Sep 23, 2016 · 11 comments
Closed

Send a Single Email to Multiple Recipients #225

thinkingserious opened this issue Sep 23, 2016 · 11 comments
Labels
status: help wanted requesting help from the community type: community enhancement feature request not on Twilio's roadmap

Comments

@thinkingserious
Copy link
Contributor

Acceptance Criteria:

  • A email object that represents the response body payload
  • A mail object that handles sending email objects, data validation and error handling

Reference:

@DannyLee12
Copy link

Do you want someone to write a script that demonstrates the ability to send a single email to multiple recipients?

@thinkingserious
Copy link
Contributor Author

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()

@carolinapeisch
Copy link

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?

@thinkingserious
Copy link
Contributor Author

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

@carolinapeisch
Copy link

carolinapeisch commented Feb 5, 2017 via email

@mbernier
Copy link
Contributor

mbernier commented Feb 6, 2017

@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!

@shelling
Copy link

shelling commented Mar 2, 2017

Any new API improvement to this issue? It that possible to create a class Emails to wrap multiple recipients? I could do this.

@thinkingserious
Copy link
Contributor Author

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

@thinkingserious thinkingserious added type: community enhancement feature request not on Twilio's roadmap and removed type: task labels Apr 28, 2017
@vijay-axis
Copy link

vijay-axis commented Sep 11, 2017

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")

@thinkingserious
Copy link
Contributor Author

This has been moved here.

@GIT-STATA
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: help wanted requesting help from the community type: community enhancement feature request not on Twilio's roadmap
Projects
None yet
Development

No branches or pull requests

7 participants