The purpose of this lambda function is to receive structured payloads from an Amazon Kinesis stream, retrieve the corresponding templates from S3, render the template(s) using Mustache.js and then use SES to send the email.
All templates and subject lines for emails are stored in an S3 bucket and the function itself is configuration-less. The function expects to receive Kinesis payloads similar to the following:
{
"Configuration": {
"Bucket": "my-email-templates-bucket"
},
"Email" : {
"Properties": {
"TemplateKey" : "templates/en/welcome",
"Partials" : "partials/en",
"Data" : {
"name" : "Jeremy"
}
},
"Payload": {
"Destination": {
"BccAddresses": [],
"CcAddresses": [],
"ToAddresses": ["[email protected]"]
},
"Source": "[email protected]",
"ReplyToAddresses": ["[email protected]"]
}
}
}
The TemplateKey
field determines where the function will attempt to retrieve the template files from inside the configured S3 bucket (Configuration.Bucket
), in-order to construct the email's body and subject line.
Using the example payload above, the function would expect the following files to be present:
s3://my-email-templates-bucket/templates/en/welcome.subj
s3://my-email-templates-bucket/templates/en/welcome.html
s3://my-email-templates-bucket/templates/en/welcome.txt
The Partials
key indicates where partials are to be located. In the above example, let us assume that a partial of footer
exists.
s3://my-email-templates-bucket/partials/en/footer.html
s3://my-email-templates-bucket/partials/en/footer.txt
Note: The .html
or .txt
versions of emails are optional. However, the .subj
is required.
welcome.subj
- Determines the subject line for the email.welcome.html
- The HTML content of the email.welcome.txt
- The plain text content of the email.footer.html
- Thefooter
partial in html format.footer.txt
- Thefooter
partial in txt format.
All files retrieved from S3 are rendered using Mustache, with the Email.Properties.Data
object passed into the renderer. Therefore, if welcome.subj
contained the following content:
Welcome to my website, {{name}}
Using the example above, it would be rendered as "Welcome to my website, Jeremy". If you need more help with what mustache templates are capable of doing, be sure to checkout the documentation.
These may help with your templates if you have lots of emails that all require common content. If a partial is defined, it is required to be present in .html
, format if a .html
template version is present, likewise for .txt
.
Note: All partials located under the path are loaded at runtime, although their use is optionally required.
Given the example payload above, you can incorporate partials into your templates as follows:
<html>
<body>
<h1>Hello {{ name }}!</h1>
{{> footer}}
</body>
</html>
<p>Goodbye {{ name }}</p>
<html>
<body>
<h1>Hello Jeremy!</h1>
<p>Goodbye Jeremy</p>
</body>
</html>
The Payload
key is the base object passed into the AWS SES sendEmail
call. It corresponds to the documented request payload here. This makes it easy to pass in additional keys for your implementation, such as ReturnPath
or SourceArn
. The only object that is injected in is Message
, which is generated by the function itself.