Using templates to send personalized email with the Amazon SES API
In Amazon SES you can send templated email either by using a stored template or by using an inline template.
-
Stored template – Refers to the
Template
resource that is created and saved in SES by using theCreateEmailTemplate
operation in the Amazon SES v2 API. The template contains the subject and body of the email containing variables (placeholders) inline with the written content. The name of the stored template and the dynamic data to the placeholder variables in the template are provided when calling either theSendEmail
orSendBulkEmail
v2 API operations.Stored templates can be easily reused and can save you time and effort when sending similar types of emails. Instead of creating each email from scratch, you only need to create the base structure and design once, then simply update the dynamic content within the template.
-
Inline template – The
Template
resource is not used, but rather, the subject and body of the email containing variables (placeholders) inline with the written content along with the values for those placeholder variables are provided when calling either theSendEmail
orSendBulkEmail
v2 API operations.Inline templates streamline the process for sending bulk email by eliminating the need to manage template resources in your SES account and simplify the integration process by allowing you to include template content directly within your application logic. They do not count against the 20,000-template limit per AWS Region.
The following limits apply when using stored templates:
-
You can create up to 20,000 email templates in each AWS Region.
-
Each template can be up to 500 KB in size, including both the text and HTML parts.
The following limit applies when using inline templates:
-
Each input JSON file can be up to 1 MB in size, including both the text and HTML parts.
The following applies to both stored and inline templates:
-
There are no limits to the number of replacement variables that can be used.
-
You can send email to up to 50 destination objects in each call to the
SendBulkEmail
operation. TheDestination
object can contain multiple recipients defined in ToAddresses, CcAddresses, and BccAddresses. The number of destinations you can contact in a single call to the v2 API may be limited by your account's maximum sending rate. For more information, see Managing your Amazon SES sending limits.
This chapter includes procedures with examples for using both stored templates and inline templates.
Note
The procedures in this section assume that you've already installed and configured the AWS CLI. For more information about installing and configuring the AWS CLI, see the AWS Command Line Interface User Guide.
(Optional) Part 1: Set up Rendering Failure event notifications
If you send an email that contains invalid personalization content, Amazon SES might accept the message, but won't be able to deliver it. For this reason, if you plan to send personalized email, you should configure SES to send Rendering Failure event notifications through Amazon SNS. When you receive a Rendering Failure event notification, you can identify which message contained the invalid content, fix the issues, and send the message again.
The procedure in this section is optional, but highly recommended.
To configure Rendering Failure event notifications
-
Create an Amazon SNS topic. For procedures, see Create a Topic in the Amazon Simple Notification Service Developer Guide.
-
Subscribe to the Amazon SNS topic. For example, if you want to receive Rendering Failure notifications by email, subscribe an email endpoint (that is, your email address) to the topic.
For procedures, see Subscribe to a Topic in the Amazon Simple Notification Service Developer Guide.
-
Complete the procedures in Set up an Amazon SNS event destination for event publishing to set up your configuration sets to publish Rendering Failure events to your Amazon SNS topic.
(Optional) Part 2: Create an email template
If you intend on using a stored template, this section will show
you how to use the CreateEmailTemplate
SES v2 API operation to create
the template. You can skip this step if you want to use an inline
template.
This procedure assumes that you've already installed and configured the AWS CLI. For more information about installing and configuring the AWS CLI, see the AWS Command Line Interface User Guide.
To create the template
-
In a text editor, create a new file and paste the following code customizing it as you need.
{ "TemplateName": "MyTemplate", "TemplateContent": { "Subject": "Greetings, {{name}}!", "Text": "Dear {{name}},\r\nYour favorite animal is {{favoriteanimal}}.", "Html": "<h1>Hello {{name}},</h1><p>Your favorite animal is {{favoriteanimal}}.</p>" } }
This code contains the following properties:
-
TemplateName – The name of the
Template
resource. When you send the email, you refer to this name. -
TemplateContent – A container for the following attributes:
-
SubjectPart – The subject line of the email. This property may contain replacement tags. These tags use the following format:
{{tagname}}
. When you send the email, you can specify a value fortagname
for each destination. -
HtmlPart – The HTML body of the email. This property may contain replacement tags. The preceding example includes two tags:
{{name}}
and{{favoriteanimal}}
. -
TextPart – The text body of the email. Recipients whose email clients don't display HTML content will see this version of the email. This property may also contain replacement tags.
-
-
-
Customize the preceding example to fit your needs, and then save the file as
mytemplate.json
. -
At the command line, type the following command to create a new template using the
CreateEmailTemplate
v2 API operation:aws sesv2 create-email-template --cli-input-json file://
mytemplate.json
Part 3: Sending the personalized email
You can use the following two SES v2 API operations to send emails using either stored templates or inline templates:
-
The
SendEmail
operation is useful for sending a customized email to a single destination object. The v2 APIDestination
object can contain the ToAddresses, CcAddresses, and BccAddresses properties. These can be used in any combination and can contain one or more email addresses that will receive the same email. -
The
SendBulkEmail
operation is useful for sending unique emails to multiple destination objects in a single call to the v2 API.
This section provides examples of how to use the AWS CLI to send templated email using both of these send operations.
Sending templated email to a single destination object
You can use the SendEmail
operation to send an email to one or more
recipients defined in a single destination object. All of the recipients in the
Destination
object will receive the same email.
To send a templated email to a single destination object
-
Depending on whether you want to use a stored template or inline template, select the respective code example to paste into a text editor, customizing it as you need.
-
Customize the preceding example to fit your needs, and then save the file as
myemail.json
. -
At the command line, type the following v2 API command to send the email:
aws sesv2 send-email --cli-input-json file://
myemail.json
Sending templated email to multiple destination objects
You can use the SendBulkEmail
operation to send an email to multiple
destination objects in a single call to the SES v2 API. SES sends a
unique email to the recipient or recipients in each Destination
object.
To send a templated email to multiple destination objects
-
Depending on whether you want to use a stored template or inline template, select the respective code example to paste into a text editor, customizing it as you need.
-
Change the values in the code in the previous step to meet your needs, and then save the file as
mybulkemail.json
. -
At the command line, type the following v2 API command to send the bulk email:
aws sesv2 send-bulk-email --cli-input-json file://
mybulkemail.json