Navigate back to the homepage

Send templated AWS SES emails with PowerShell

Dominic Böttger
March 31st, 2020 · 1 min read

There is a lot of documentation and examples for the Amazon Web Services cli tools but there are less examples for the AWS PowerShell module. I had the problem that I did not find a working example to send emails via AWS SES and had to find a solution by myself. I hope the following post will save your time.

I assume that you already have the PowerShell tools installed and your AWS credentials have been set up.

As I already had the AWS cli installed my credentials were already stored and I just had to install and load the PowerShell module.

1Install-Module -Name AWSPowerShell.NetCore
2Import-Module AWSPowerShell.NetCore

If you need to install it differently or further information on how to set up the credentials you should have a look at the official AWS PowerShell tools documentation

Creating and storing the template

First, I created a json file for the template and saved the contents below as mytemplate.json.

1{
2 "Template": {
3 "TemplateName": "MyTemplate",
4 "SubjectPart": "Greetings, TESTSTRING!",
5 "HtmlPart": "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitiona
6l.dtd\"> <html xmlns=\"http://www.w3.org/1999/xhtml\"> <head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /
7> <title>Demystifying Email Design</title> <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/> </head> <body>
8Español üäü {{name}}</body> </html>",
9 "TextPart": "Dear TESTSTRING,\r\nYour favorite animal is TESTSTRING."
10 }
11}

To load the template into the SES template store the content of the json needs to be loaded (Unicode is important! ), converted from json to an object and then piped to the New-SesTemplate CmdLet.

1Get-Content -Path ./mytemplate.json -Encoding UTF-8 | convertFrom-Json | New-SesTemplate

Sending the mail

After the template has been created you are able to send mails with the content of the template and fill the placeholders in the template with data.

For this you should save the contents below in a file named myemail.json.

1{
2 "Source":"Mary Major <mary.major@example.com>",
3 "Template": "MyTemplate",
4 "Destination_ToAddress": [ "test@example.com" ],
5 "TemplateData": "{ \"name\":\"Español\", \"favoriteanimal\": \"alligator in IL template\" }"
6}

The json needs to be adjusted to fulfill your needs. Which means especially the mail addresses should be replaced to align with your SES and domain configuration.

To send the mail you just need to execute the code below.

1Get-Content -Path ../myemail.json -Encoding UTF-8 | convertFrom-JSON | Send-SESTemplatedEmail

Congratulations for delivering your email via SES with PowerShell! 👍

More articles from Dominic Böttger

AvePoint Fly Migrator TLS 1.2 workaround

A workaround to enable TLS 1.2 support for IMAP migrations with AvePoint's Fly Migrator.

November 29th, 2020 · 1 min read

Optimize Visual Studio Code for C# .NET Core development

A brief introduction and some information on how VSCode can be optimized for a great C# .NET core developer experience

May 10th, 2020 · 1 min read
© 2020 Dominic Böttger
Link to $https://twitter.com/dboettgerLink to $https://github.com/dominicboettgerLink to $https://www.linkedin.com/in/dominic-b%C3%B6ttger-74952a138/