Skip to content

Send transactional template to multiple recipients #899

@stockcj

Description

@stockcj

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="from_email@example.com", 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions