I have been using SES templates for a while now, but I just learned something interesting. Each user will have their own templates, unless I missed something.
Templates are incredibly simple to create, you need only specific the subject and the content. A sample template looks like this:
{
"Template": {
"TemplateName": "MyTemplateName",
"SubjectPart": "{{MySubjectText}}",
"TextPart": "string",
"HtmlPart": "<p>Here is my templated content</p>"
}
}
Ok, I lied you do need a template name and the text part. There are a couple of things to note on the htmlPart. It is not an entire HTML document, only the content between the body tags. As you might expect this also will need to be escaped since it is a proper JSON payload. The help shrink it down I use https://www.textfixer.com/html/compress-html-compression.php to remove all of the line breaks and other characters that would stop this from processing.
Deploying these templates are pretty straight forward, you need to run the aws configure cli command to make sure you are using the correct credentials and pointing to the region that you want (SES is not available everywhere). After that you simple:
aws ses create-template --cli-input-json "$(cat ./mytemplate.json)"
This will add it to the library so that you can use it. You follow the same process for updating but you swap create for update:
aws ses update-template --cli-input-json "$(cat ./mytemplate.json)"
The way I figured out that the templates were user specific was using the get-template command. When I asked for the template back, I was seeing what I expected. When I used it, I was still getting an old version. I use a different set of access keys for my production and development systems. After all of the non-sense debugging, I decided to give the other credential a try. Sure enough it had the old versions. This is why it is important to know how you are configured ( I knew that) when you are using the cli. FYI, here is how you get the template:
aws ses get-template --template-name mytemplate
As you know, I am a .NET engineer so that is at the heart of everything I do. Here is an example of a method that I used to send out the templated email:
private async Task<bool> SendTemplateEmailAsync(List<string> destinations, object parameters, string from, string templateName)
{
SendTemplatedEmailRequest r = new SendTemplatedEmailRequest();
Destination d = new Destination(destinations);
r.Destination = d;
r.TemplateData = JsonConvert.SerializeObject(parameters);
r.Template = templateName;
r.Source = from;
r.ConfigurationSetName = "Initial";
try
{
var result = await _client.SendTemplatedEmailAsync(r).ConfigureAwait(false);
if (result.HttpStatusCode == System.Net.HttpStatusCode.OK)
{
return true;
}
return false;
}
catch (Exception exception)
{
_logger.LogError(0, exception, "Failed to send the email);
}
return false;
}
That is the full path for using the template emails in .NET.