Search code examples
amazon-web-servicesboto3amazon-ses

AWS SESv2: Using contact attributes for template data


I have a contact list, with each contact having the "Name" attribute, such as:

{
    "ContactListName": "myusers",
    "EmailAddress": "[email protected]",
    "UnsubscribeAll": false,
    "TopicPreferences": [
        {
            "TopicName": "Announcements",
            "SubscriptionStatus": "OPT_IN"
        }
    ],
    "AttributesData": "{\"Name\": \"John\"}"
}

I would like send an email to all customers interested in a particular topic. The email is based on a template that begins with "Dear {{Name}}". I want {{Name}} to be replaced with the name from the AttributesData of each contact.

I can obtain the list of contacts interested in the topic using list_contacts. However, the response does not contain the AttributeData and so I have to send the email like this:

response = ses_client.send_bulk_email(
    FromEmailAddress='[email protected]',
    ReplyToAddresses=[
        '[email protected]',
    ],
    DefaultContent={
        'Template': {
            'TemplateName': 'mytemplate',
            'TemplateData': '{"Name":"Unknown"}'
        }
    },
    BulkEmailEntries=[
        {
            'Destination': {
                'ToAddresses': [
                    '[email protected]',
                ],
            },
            'ReplacementEmailContent': {
                'ReplacementTemplate': {
                    'ReplacementTemplateData': '{"Name":"John"}'
                }
            }
        },
    ],
)

If I skip the ReplacementEmailContent key, {{Name}} in the template is substituted for Unknown. How do I get it to use the contact's attribute data? (Note that calling get_contact for each contact would take too long when sending bulk email to many users)

P.S. The question in re:Post


Solution

  • How do I get it to use the contact's attribute data? (Note that calling get_contact for each contact would take too long when sending bulk email to many users)

    I don't think you can do that. Would be happy to be wrong and learn otherwise.

    Contacts and lists are mainly there for subscription management. You can store contact attributes with the contact (free of charge), but, as you mentioned, your only option to retrieve them is calling GetContact in a loop (also free of charge). It should not be much of a problem if your application is hosted on AWS, as you can parallelize the calls (and maybe set up a PrivateLink to SES).

    If your application is hosted outside AWS, then the roundtrips could indeed become a problem. In this case, you would be better off maintaining your contact attributes in another store, which would allow you to pass a batch of keys and retrieve a batch of values in a single call.