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 transactional template to multiple recipients #899

Closed
stockcj opened this issue May 27, 2020 · 3 comments · Fixed by #908
Closed

Send transactional template to multiple recipients #899

stockcj opened this issue May 27, 2020 · 3 comments · Fixed by #908
Labels
status: work in progress Twilio or the community is in the process of implementing type: twilio enhancement feature request on Twilio's roadmap

Comments

@stockcj
Copy link

stockcj commented May 27, 2020

Issue Summary

Is it possible to use transactional templates and send to multiple recipients that have their own dynamic data? If it is possible it would be great if an example could be added to the documentation.

I'm doing this currently by creating a message for each recipient individually, and then sending individually. What I'd like to do is a cross between the sending multiple emails to multiple recipients example and the transactional templates example.

This is my current solution:

import csv
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail, To

key = MY_KEY
sg = SendGridAPIClient(key)

with open("test.csv") as csv_file:
    recipients = [
        {k: v for k, v in row.items()}
        for row in csv.DictReader(csv_file, skipinitialspace=True)
    ]

for recipient in recipients:
    message = Mail(from_email="[email protected]", to_emails=recipient["email"],)
    message.dynamic_template_data = {
        "name": recipient["name"],
        "dummy": recipient["dummy"],
    }
    message.template_id = MY_TEMPLATE_ID
    try:
        response = sg.send(message=message)
        print(response.status_code)
        print(response.body)
        print(response.headers)
    except Exception as e:
        print(e)

I'd like to be able to submit this all as one job though.

@childish-sambino
Copy link
Contributor

Yup, that's totally possible. You'd basically be merging the two examples that you've linked.

May be simplest to use sending multiple emails to multiple recipients as a starting point and replace substitutions with dynamic_template_data and then add your template_id.

@childish-sambino childish-sambino added status: waiting for feedback waiting for feedback from the submitter type: question question directed at the library labels Jun 8, 2020
@apanagar
Copy link

Yup, that's totally possible. You'd basically be merging the two examples that you've linked.

May be simplest to use sending multiple emails to multiple recipients as a starting point and replace substitutions with dynamic_template_data and then add your template_id.

I tried this but got the following error
TypeError: __init__() got an unexpected keyword argument 'dynamic_template_data'

Is there a working example somewhere of the sendgrid v3 syntax for sending a message to multiple recipients using personalization?

@childish-sambino
Copy link
Contributor

Ack, you're right. I'll look into an update to make this simpler.

@childish-sambino childish-sambino added status: work in progress Twilio or the community is in the process of implementing type: twilio enhancement feature request on Twilio's roadmap and removed status: waiting for feedback waiting for feedback from the submitter type: question question directed at the library labels Jun 12, 2020
childish-sambino pushed a commit that referenced this issue Jun 12, 2020
Fixes #899

This is helpful when sending multiple emails to multiple recipients. You can now include the dynamic template data with the recipient which will then be included in the personalization.
childish-sambino pushed a commit that referenced this issue Jun 13, 2020
Fixes #899

This is helpful when sending multiple emails to multiple recipients. You can now include the dynamic template data with the recipient which will then be included in the personalization.
childish-sambino pushed a commit that referenced this issue Jun 13, 2020
Fixes #899

This is helpful when sending multiple emails to multiple recipients. You can now include the dynamic template data with the recipient which will then be included in the personalization.
childish-sambino pushed a commit that referenced this issue Jun 17, 2020
Fixes #899

This is helpful when sending multiple emails to multiple recipients. You can now include the dynamic template data with the recipient which will then be included in the personalization.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: work in progress Twilio or the community is in the process of implementing type: twilio enhancement feature request on Twilio's roadmap
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants