Skip to content

Instantly share code, notes, and snippets.

@mcianni
Forked from shivasurya/template-creator.js
Last active April 26, 2024 17:38
Show Gist options
  • Save mcianni/3ab495e98285828225624301a50f3cbd to your computer and use it in GitHub Desktop.
Save mcianni/3ab495e98285828225624301a50f3cbd to your computer and use it in GitHub Desktop.
AWS SES Email Template Create/Update Script Wrapper
const minify = require("html-minifier").minify;
const fs = require("fs");
const { SESClient, CreateTemplateCommand, UpdateTemplateCommand } = require('@aws-sdk/client-ses');
const sesClient = new SESClient({
region: "us-east-2",
credentials: {
accessKeyId: "XXXXXXXXXXXXXXXXXXX",
secretAccessKey: "XXXXXXXXXXXXXXXXXXXX"
},
});
// 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
const isUpdate = process.argv[4] === 'true';
// read html file from commandline and minify them
const 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
const 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")
const 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");
const command = isUpdate ? new UpdateTemplateCommand(params) : new CreateTemplateCommand(params);
const response = sesClient
.send(command)
.then(data => {
console.log(data);
})
.catch(error => {
console.log(error);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment