Amazon Simple Email Service (SES) enables you to send emails that are personalized for each recipient by using templates. Templates include a subject line and the text and HTML parts of the email body. The subject and body sections can also contain unique values that are personalized for each recipient.
For more information, see Sending Personalized Email Using the Amazon SES API.
The following examples show how to:
To set up and run this example, you must first complete these tasks:
To create a template to send personalized email messages, use the CreateTemplate operation. The template can be used by any account authorized to send messages in the AWS Region to which the template is added.
Note
SES doesn't validate your HTML, so be sure that HtmlPart is valid before sending an email.
import boto3
# Create SES client
ses = boto3.client('ses')
response = ses.create_template(
Template = {
'TemplateName' : 'TEMPLATE_NAME',
'SubjectPart' : 'SUBJECT_LINE',
'TextPart' : 'TEXT_CONTENT',
'HtmlPart' : 'HTML_CONTENT'
}
)
print(response)
To view the content for an existing email template including the subject line, HTML body, and plain text, use the GetTemplate operation. Only TemplateName is required.
import boto3
# Create SES client
ses = boto3.client('ses')
response = ses.get_template(
TemplateName = 'TEMPLATE_NAME'
)
print(response)
To retrieve a list of all email templates that are associated with your AWS account in the current AWS Region, use the ListTemplates operation.
import boto3
# Create SES client
ses = boto3.client('ses')
response = ses.list_templates(
MaxItems=10
)
print(response)
To change the content for a specific email template including the subject line, HTML body, and plain text, use the UpdateTemplate operation.
import boto3
# Create SES client
ses = boto3.client('ses')
response = ses.update_template(
Template={
'TemplateName': 'TEMPLATE_NAME',
'SubjectPart' : 'SUBJECT_LINE',
'TextPart' : 'TEXT_CONTENT',
'HtmlPart' : 'HTML_CONTENT'
}
)
print(response)
To use a template to send an email to recipients, use the SendTemplatedEmail operation.
import boto3
# Create SES client
ses = boto3.client('ses')
response = ses.send_templated_email(
Source='EMAIL_ADDRESS',
Destination={
'ToAddresses': [
'EMAIL_ADDRESS',
],
'CcAddresses': [
'EMAIL_ADDRESS',
]
},
ReplyToAddresses=[
'EMAIL_ADDRESS',
],
Template='TEMPLATE_NAME',
TemplateData='{ \"REPLACEMENT_TAG_NAME\":\"REPLACEMENT_VALUE\" }'
)
print(response)