Search code examples
node.jsamazon-web-servicesamazon-sesemail-templates

Is there a way of dynamically changing the email template subject field on SES templates?


I am currently using SES to store email templates with serverless-ses-template on deployment.
The templates get stored with the following parameters, both of which are mandatory:

templateId: 'status-template',
templateSubject: 'Some Title'

In my Lambda, I get the template I need, I map the values to the template and send it:

const email = {
        Destination: {
            ToAddresses: targetAddresses
        },
        Source: 'sourcemail@mail.com',
        Template: "status-template",
        TemplateData: JSON.stringify(templateData)
    };

await ses.sendTemplatedEmail(email).promise();

Once this email is recieved, the subject is expectedly "Some Title" as in the template.
Is there a way of dynamically changing that title before sending it i.e. changing the title from "Some Title" to "Other Title"?


Solution

  • You can customize your subject and pretty much any other field, by creating what is essentially a custom field value and wrapping it in double curly brackets, like so:

    templateSubject: "Important Message for {{ username }}"

    Then add your "username" parameter to your templateData object, and when the email is delivered it will replace the {{ username }} with the value, in this case "Marko Nikolov".

    const templateData = {
        "username": "Marko Nikolov"
    };
    
    const email = {
            Destination: {
                ToAddresses: targetAddresses
            },
            Source: 'sourcemail@mail.com',
            Template: "status-template",
            TemplateData: JSON.stringify(templateData),
        };
    
    await ses.sendTemplatedEmail(email).promise();
    

    You can read more about the sendTemplatedEmail property in the API docs here, and creating and customizing SES email templates here.