Creating Custom Email Templates with Amazon SES

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.

The following examples show how to:

Prerequisite Tasks

To set up and run this example, you must first complete these tasks:

  • Configure your AWS credentials, as described in Quickstart.

Create an Email Template

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.

Example

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)

Get an Email Template

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.

Example

import boto3

# Create SES client
ses = boto3.client('ses')

response = ses.get_template(
  TemplateName = 'TEMPLATE_NAME'
)

print(response)

List All Email Templates

To retrieve a list of all email templates that are associated with your AWS account in the current AWS Region, use the ListTemplates operation.

Example

import boto3

# Create SES client
ses = boto3.client('ses')

response = ses.list_templates(
  MaxItems=10
)

print(response)

Update an Email Template

To change the content for a specific email template including the subject line, HTML body, and plain text, use the UpdateTemplate operation.

Example

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)

Send an Email with a Template

To use a template to send an email to recipients, use the SendTemplatedEmail operation.

Example

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)