Skip to content

Instantly share code, notes, and snippets.

@shivasurya
Created May 28, 2020 04:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shivasurya/85bb10bb501851d7ff231ed03bb50b77 to your computer and use it in GitHub Desktop.
Save shivasurya/85bb10bb501851d7ff231ed03bb50b77 to your computer and use it in GitHub Desktop.
AWS SES Email Template Create/Update Script Wrapper
// 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))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment