Search code examples
amazon-web-servicesamazon-ses

For loop in email template


Can we create a for loop for the SES templates?

Looking at the code here (https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-personalized-email-api.html) , the only example I see is for key value pair.

The syntax looks like what I used to use in handlebar.js.

Something like this:

{
  "Template": {
    "TemplateName": "MyTemplate",
    "SubjectPart": "Greetings, {{name}}!",
    "HtmlPart": "<h1>Hello {{name}},</h1><p>Your favorite animals are {{#each animals}}<h1>{{favoriteanimal}}</h1>{{/each}}.</p>",
  }
}

Solution

  • Yes!

    Example template data:

    {
      "meta":{
        "userId":"51806220607"
      },
      "contact":{
        "firstName":"Anaya",
        "lastName":"Iyengar",
        "city":"Bengaluru",
        "country":"India",
        "postalCode":"560052"
      },
      "subscription":[
        {
          "interest":"Sports"
        },
        {
          "interest":"Travel"
        },
        {
          "interest":"Cooking"
        }
      ]
    }
    

    Example template:

    {
      "Template": {
        "TemplateName": "Preferences",
        "SubjectPart": "Subscription Preferences for {{contact.firstName}} {{contact.lastName}}",
        "HtmlPart": "<h1>Your Preferences</h1>
                     <p>You have indicated that you are interested in receiving 
                       information about the following subjects:</p>
                     <ul>
                       {{#each subscription}}
                         <li>{{interest}}</li>
                       {{/each}}
                     </ul>
                     <p>You can change these settings at any time by visiting 
                        the <a href=https://www.example.com/prefererences/i.aspx?id={{meta.userId}}>
                        Preference Center</a>.</p>",
        "TextPart": "Your Preferences\n\nYou have indicated that you are interested in 
                     receiving information about the following subjects:\n
                     {{#each subscription}}
                       - {{interest}}\n
                     {{/each}}
                     \nYou can change these settings at any time by 
                     visiting the Preference Center at 
                     https://www.example.com/prefererences/i.aspx?id={{meta.userId}}"
      }
    }
    

    More information can be found here: https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-personalized-email-advanced.html