Send Emails with Amazon SES, Part 2: SES Template with Lists

Shivanjali Nimbalkar
IntelliconnectQ Engineering
2 min readMay 28, 2021

In Send Emails with Amazon SES, Part 1: Simple SES Template, we have created simple Email templates with variables.

Now, let’s see how to

  • Parse nested attributes
  • Iterate through lists
  • Use basic conditional statements

to send personalized dynamic content to our recipients.

For example, sometimes the data is organized in different categories, and data is structured accordingly in a detailed form, like shown below :

Nested Attributes :

In your email templates, you can refer to nested attributes by providing the name of the parent attribute, followed by a period (.), followed by the name of the attribute which you want to include. For example, to include each recipient’s first name in the email template, include the following text in your email template: Hi {{user.firstName}}!

Iterating through lists :

The each helper function iterates through items in an array. Given below is an example that shows the use of eachhelper function to create a list of books referred.

You can create this template similarly as described in Part 1, using AWS CLI or AWS SDK. As long as each recipient has at least one value in the book_references object, they receive an email that includes the itemised list of the references. Given below is the JSON file that can be used to send email to multiple recipients using the above template :

When you send an email using JSON mentioned above, they will receive a message that resembles the example shown in the following image :

Basic conditional statements :

By using the {{if}} helper, you can format the email differently if a certain attribute is present in the template data as shown below :

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 or find the templates here.

Thanks for reading!

--

--