Problems:
1. Creating a valid JSON file by escaping the HTML Email Text property
2. Updating the templated HTML file ( manual command-line task )
3. Testing the templated emails
![]() |
How to automate AWS SES E-mail Template Update |
Solution:
I've come up with a normal update script written in Nodejs language with the help of AWS SDK that can help in scaling email template updates faster.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// install those dependencies first | |
var minify = require("html-minifier").minify; | |
var fs = require("fs"); | |
var AWS = require("aws-sdk"); | |
// usage : node .\template-creater.js .\emailtemplate.html emailtemplate.json false emailtemplate-unique-name "Subject for the email" | |
// 1st Argument => Templated HTML File | |
// 2nd Argument => Name of the generated template json file | |
// 3rd Argument => Update or Create template ( boolean ) | |
// 4th Argument => Unique template name | |
// 5th Argument => Subject of the email | |
let isUpdate = process.argv[4]; | |
AWS.config.update({ | |
accessKeyId: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", | |
secretAccessKey: "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY", | |
region: "us-west-1" | |
}); | |
// read html file from commandline and minify them | |
var fileContents = fs.readFileSync(process.argv[2], "utf8"); | |
// var minifiedHtml = minify(fileContents, { | |
// collapseWhitespace: true, | |
// minifyCSS: true, | |
// minifyJS: true | |
// }); | |
// escape the html code double quotes and special characters | |
var myEscapedJSONString = fileContents | |
.replace(/\\n/g, "\\n") | |
.replace(/\\"/g, "'") | |
.replace(/\\&/g, "\\&") | |
.replace(/\\r/g, "\\r") | |
.replace(/\\t/g, "\\t") | |
.replace(/\\b/g, "\\b") | |
.replace(/\\f/g, "\\f") | |
var params = { | |
Template: { | |
TemplateName: process.argv[5], | |
HtmlPart: myEscapedJSONString, | |
SubjectPart: process.argv[6] | |
} | |
}; | |
// write to template generation json file | |
fs.writeFileSync(process.argv[3], JSON.stringify(params), "utf8"); | |
//upload to AWS service to render it as template | |
if (isUpdate) { | |
let templatePromise = new AWS.SES({ apiVersion: "2010-12-01" }) | |
.updateTemplate(params) | |
.promise(); | |
templatePromise.then(function(data){ | |
console.log(data) | |
}).catch(err => console.log(err)) | |
} else { | |
let templatePromise = new AWS.SES({ apiVersion: "2010-12-01" }) | |
.createTemplate(params) | |
.promise(); | |
templatePromise.then(function(data){ | |
console.log(data) | |
}).catch(err => console.log(err)) | |
} |
Usage:
> node template-creater.js emailtemplate.html emailtemplate.json false emailtemplate-unique-name "Subject for the email"
This will automatically accept the templated HTML file and creates a valid JSON file and tries updating/creating templates in the AWS SES console based on configuration or command-line argument values.
1st Argument => Templated HTML File |
2nd Argument => Name of the auto-generated template json file |
3rd Argument => Update or Create template ( boolean ) |
4th Argument => Unique template name |
5th Argument => Subject of the email |
Final Words:
Feel free to use the script ( No license Restrictions for personal/commercial projects and No Warranty from Author ) and for bugs/hugs do comment below. Share is care.