Boto3: Sending bulk template email returns "The template data is invalid"

Created on 4 Dec 2018  路  3Comments  路  Source: boto/boto3

Hi,
I'm having a strange problem.
This code appears to be fine, but it returns "The template data is invalid" every single time.
Am I doing something wrong?
Can it be a Bug?

Thanks

MacOS, latest version.
Python 3.6.6
boto3==1.9.57

import boto3

verified_email='[email protected]'

template_name = "TestTemplateDeleteMe"
template = {
    "TemplateName": template_name,
    "SubjectPart": "Test send bulk template email",
    "HtmlPart": "<h1>Hello {{name}},</h1>Favorite animal is {{favorite_animal}}.",
    "TextPart": "Dear {{name}},\r\nYour favorite animal is {{favorite_animal}}."
}

client = boto3.client('ses', region_name='eu-west-1')

try:
    client.create_template(Template=template)
except Exception as e:
    print(str(e))
    try:
        if e.response['Error']['Code'] != 'AlreadyExists':
            raise e
    except KeyError:
        raise e

response = client.send_bulk_templated_email(
    Source=verified_email,
    Template='TestTemplateDeleteMe',
    Destinations=[{'Destination': {'ToAddresses': [verified_email]}}],
    DefaultTemplateData='{"name": "TestEmailPerson", "favorite_animal": "TestEmailAnimal"}'
)

print(response)

Most helpful comment

Hello @kumaresoy . I suggest you look for help in stack overflow. ;)

All 3 comments

Looks like this

"ReplacementTemplateData": "{}"

is missing in the call to send_bulk_templated_email.

Looks like documentation is a bit lacking on this.

The full code would be:

import boto3

verified_email='[email protected]'

template_name = "TestTemplateDeleteMe"
template = {
    "TemplateName": template_name,
    "SubjectPart": "Test send bulk template email",
    "HtmlPart": "<h1>Hello {{name}},</h1>Favorite animal is {{favorite_animal}}.",
    "TextPart": "Dear {{name}},\r\nYour favorite animal is {{favorite_animal}}."
}

client = boto3.client('ses', region_name='eu-west-1')

try:
    client.create_template(Template=template)
except Exception as e:
    print(str(e))
    try:
        if e.response['Error']['Code'] != 'AlreadyExists':
            raise e
    except KeyError:
        raise e

response = client.send_bulk_templated_email(
    Source=verified_email,
    Template='TestTemplateDeleteMe',
    Destinations=[{
        'Destination': {
            'ToAddresses': [
                verified_email
            ]
        },
        "ReplacementTemplateData": "{}"
        }
    ],
    DefaultTemplateData='{"name": "TestEmailPerson", "favorite_animal": "TestEmailAnimal"}'
)

print(response)

Thanks

Please help on this scenario. I am preparing a script which has one "for" loop & that will work user by user basis. I just want to use the SES Template to send mail to each user.

How can i pass the user name & the password age to the script in TemplateData.


email_alert = ses.send_templated_email(
Source=email_from,
Destination={
'ToAddresses': [
'user_mail',
]
},
Template='SES-TEMPLATE-Password-Warning',
TemplateData='{"name": "username", "password_expires": "password_expires_in"}'

)

Where the "username" is the IAM user name which can be fetched from the excel file using "row['user']" and i calculated the age using below formula and i just need to add these values in the above template data.


        last_changed_date=dateutil.parser.parse(row['password_last_changed']).date()
        expires = (last_changed_date + datetime.timedelta(MaxPasswordAge)) - datetime.date.today()

password_expires_in = expires.days

Hello @kumaresoy . I suggest you look for help in stack overflow. ;)

Was this page helpful?
0 / 5 - 0 ratings