DEV Community

Cover image for Send templated AWS SES emails with Powershell
⎝Dominic Böttger⎠
⎝Dominic Böttger⎠

Posted on • Originally published at

Send templated AWS SES emails with Powershell

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.

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

If you need to install it differently or further informatins on how to set up the credentials you should have a look at the offical 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.

  "Template": {
    "TemplateName": "MyTemplate",
    "SubjectPart": "Greetings, TESTSTRING!",
    "HtmlPart": "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"
l.dtd\"> <html xmlns=\"\"> <head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /
> <title>Demystifying Email Design</title> <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/> </head> <body>
Español üäü {{name}}</body> </html>",
    "TextPart": "Dear TESTSTRING,\r\nYour favorite animal is TESTSTRING."

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 a object and then piped to the New-SesTemplate CmdLet.

Get-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.

  "Source":"Mary Major <>",
  "Template": "MyTemplate",
  "Destination_ToAddress": [ "" ],
  "TemplateData": "{ \"name\":\"Español\", \"favoriteanimal\": \"alligator in IL template\" }"

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

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

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

Congratulations for delivering your email via SES with Powershell! :-)

Top comments (1)

ericwoodford profile image
Eric Woodford • Edited

Thank you Dominic for this excellent example. I was also trying to feed the command a hashtable created JSON variable and I couldn't get it to work.