Soni Pandey
5 min readApr 7, 2020

--

How can we use Amazon SES template to send email in Node.js?

In this article, We’ll learn about how can we use Amazon SES to send Bulk Email and use Email Templates. We’ll be focussed on-

  1. What is Amazon SES?
  2. Why we need to move out from Amazon SES sandbox?
  3. Introduce Email Templates and Bulk Sending.

Prerequisites -

Follow these steps if your machine is not having any of these -

i) Install Node.js® and npm.

ii) Sign up for an AWS account.

iii) Configure AWS CLI.

Follow these steps for last two steps.

1. What is Amazon SES?

Amazon SES is an email platform that provides an easy, cost-effective way for you to send and receive email using your own email addresses and domains.

For example, you can send marketing emails such as special offers, transactional emails such as order confirmations, and other types of correspondence such as newsletters.

2. Why we need to move out from Amazon SES sandbox?

Amazon place all new accounts in the Amazon SES sandbox. While your account is in the sandbox, you can use all of the features of Amazon SES. However, when your account is in the sandbox, we apply the following restrictions to your account:

  • We can only send mail to verified email addresses and domains, or to the Amazon SES mailbox simulator.
  • We can only send mail from verified email addresses and domains. Note:This restriction applies even when your account isn’t in the sandbox.
  • We can send a maximum of 200 messages per 24-hour period.
  • We can send a maximum of 1 message per second.

When your account is out of the sandbox, you can send email to any recipient, regardless of whether the recipient’s address or domain is verified. However, you still have to verify all identities that you use as “From”, “Source”, “Sender”, or “Return-Path” addresses.

3. Introduce Email Templates and Bulk Sending -

Email templates

We can use email templates to create the structure of an email that you plan to send to multiple recipients, or that you will use again in the future. Each template contains a subject line, a text part, and an HTML part. Both the subject and the email body can contain variables that are automatically replaced with values specific to each recipient. For example, you can include a {{name}} variable in the body of your email. When you send the email, you specify the value of {{name}} for each recipient. Amazon SES then automatically replaces the {{name}} variable with the recipient’s first name.

Creating a template

To create a template, you use the CreateTemplate API operation. To use this operation, pass a JSON object with four properties: a template name (TemplateName), a subject line (SubjectPart), a plain text version of the email body (TextPart), and an HTML version of the email body (HtmlPart). You can include variables in the subject line or message body by enclosing the variable names in two sets of curly braces. The following example shows the structure of this JSON object.

{
"Template": {
"TemplateName": "EmailTemplate", //This is the template name which you'll refer while sending through SES.
"SubjectPart": "Hey {{name}}! Welcome to our site!",
"HtmlPart": "Please find the details - {{name}}, {{email}} {{phone}}."
}
}
//can't contain line breaks within these values.

Important: In the preceding code example, the values of the HtmlPart and TextPart attributes include line breaks to make the example easier to read. The JSON file for your template can't contain line breaks within these values. If you copied and pasted this example into your own JSON file, remove the line breaks and extra spaces from the HtmlPart and TextPart sections before proceeding.

Use this example to create your own template, and save the resulting file as email-template.json.

const AWS = require("aws-sdk");
const ses = new AWS.SES({
region: 'us-west-2'
});
/**
*
@fileOverview create-template.js Create an email template
* */
const mainFunction = async () => {
const params = require("./email-template.json");
return await ses.createTemplate(params).promise();
}
mainFunction().then(() => {
console.log('template created successfully.');
}, (ex) => {
console.log('Error in template creation.');
console.dir(ex.message);
});

Update an existing template

To update an existing template, you can’t run the same command for any changes in template.

const AWS = require("aws-sdk");
const ses = new AWS.SES({
region: 'us-west-2'
});
/**
*
@fileOverview update-template.js Update existing email template
* */
const mainFunction = async () => {
const params = require("./email-template.json");
return await ses.updateTemplate(params).promise();
}
mainFunction().then(() => {
console.log('template created successfully.');
}, (ex) => {
console.log('Error in template creation.');
console.dir(ex.message);
});

Bulk email sending

In most cases, you should use email templates to send personalized emails to several customers at the same time. The SendBulkTemplatedEmail API operation helps you do that. This operation also accepts a JSON object. At a minimum, you must supply a sender email address (Source), a reference to an existing template (Template), a list of recipients in an array called Destinations (within which you specify the recipient’s email address, and the variable values for that recipient), and a list of fallback values for the variables in the template (DefaultTemplateData). The following example shows the structure of this JSON object.

const AWS = require("aws-sdk");
const ses = new AWS.SES({
region: 'us-west-2'
});
/**
* @fileOverview bulk-email-send.js Bulk Email Sending
**/
const mainFunction = async () => {
const templateData = JSON.stringify({
name: 'Soni Pandey',
email: 'sonipandey.71@gmail.com',
phone: '+919999999999'
})
const params = {
Destinations: [/* required */
{
Destination: {
ToAddresses: [
"sonipandey.71@gmail.com",
"abc@xyz.com"
],
CcAddresses: [
"sonipandey.71@gmail.com",
"abc@xyz.com"
]
},
ReplacementTemplateData: templateData
},
/* more items */
],
Source: 'Notifications <notifications@thepandeysoni.com>', /* required */
Template: 'EmailTemplate', /* required */
DefaultTemplateData: "{ \"name\":\"unknown\", \"email\":\"unknown\", \"phone\":\"unknown\" }"
};
console.log(JSON.stringify(params)) await ses.sendBulkTemplatedEmail(params).promise();
}
mainFunction().then(() => {
console.log('template created successfully.');
}, (ex) => {
console.log('Error in template creation.');
console.dir(ex.message);
});

NOTE:

  1. You can change the template name (TemplateName in email-template.json file)as per your need.
  2. email-template.json file can’t contain line breaks within these values (HtmlPart, TextPart, SubjectPart).
  3. templateData in bulk-email-send file could be vary as per your template html part.
  4. If you have moved from Amazon SES sandbox then only you can send email to unverified users (Destination).
  5. Source email should be verified always.
  6. Template in bulk-email-send file should be the same which name you have used in email-template.json file as TemplateName.

Other considerations

There are a few limits and other considerations when using these features:

  • You can create up to 10,000 email templates per Amazon SES account.
  • Each template can be up to 500KB in size, including both the text and HTML parts.
  • You can include an unlimited number of replacement variables in each template.
  • You can send email to up to 50 destinations in each call to the SendBulkTemplatedEmail operation. A destination includes a list of recipients, as well as CC and BCC recipients. Note that the number of destinations you can contact in a single call to the API may be limited by your account’s maximum sending rate. For more information, see Managing Your Amazon SES Sending Limits in the Amazon SES Developer Guide.

Conclusion:

We have learned about how can we use Amazon SES to send Bulk Email and use Email Templates in Node.js. Here is the source code. You can reach out to me for any doubt and suggestions.

Reference -

  1. https://docs.aws.amazon.com/ses/latest/DeveloperGuide/Welcome.html
  2. https://docs.aws.amazon.com/ses/latest/DeveloperGuide/request-production-access.html
  3. https://aws.amazon.com/blogs/messaging-and-targeting/introducing-email-templates-and-bulk-sending/
  4. https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SES.html#createTemplate-property
  5. https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-personalized-email-advanced.html

--

--

Soni Pandey

I am a Node.js Developer and eager to learn new technology. I blog, tweet & read whenever I can.