Skip to content

Instantly share code, notes, and snippets.

@TalhaAwan
Last active August 3, 2017 19:14
Show Gist options
  • Save TalhaAwan/72e1ab46e83358987fac55e078282c00 to your computer and use it in GitHub Desktop.
Save TalhaAwan/72e1ab46e83358987fac55e078282c00 to your computer and use it in GitHub Desktop.
Node js script to send email with mandrill
const mandrill = require('mandrill-api/mandrill');
const config = require('./config');
const mandrill_client = new mandrill.Mandrill(config.mandrill.apiKey);
const mandrill_client_test = new mandrill.Mandrill(config.mandrill.testApiKey); // test client to emulate email sending
// some data
const noOftasks = 12;
const perfomance = "improved";
const name = "Talha Awan";
const email = "testemail@talha.com";
const templateName = "monthly-email";
const templateContent = [ //merge tags
{
"name": "name",
"content": name
},
{
"name": "duration",
"content": moment().subtract(1, 'month').format('MMM Do') + " - " + moment().subtract(1, 'days').format('MMM Do') // Jul 3rd - Aug 2nd
},
{
"name": "tasksPerfomed",
"content": noOftasks
},
{
"name": "perfomance",
"content": perfomance
}
];
const message = {
"text": "The Company",
"subject": "Your monthly performance report from The Company!",
"from_email": "no-reply@thecompany.com",
"from_name": "The Company",
"to": [{
"email": email,
"name": name,
"type": "to"
}],
"merge_language": "mailchimp",
"merge_vars": [{
"rcpt": email,
"vars": templateContent
}]
};
mandrill_client.messages.sendTemplate({"template_name": templateName, "template_content": templateContent, "message": message, "async": false},
function() {
console.log("email sent");
}, function(err) {
console.log(err)
});
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>*|SUBJECT|*</title>
<div>
<p>
Hi *|name|*
</p>
<p>
Your performance status for duration *|duratiion|* is "*|performance|*". In this period you performed *|noOftasks|* tasks.
</p>
<p>
Great work! Keep it up
</p>
</div>
</head>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment