Skip to content

Instantly share code, notes, and snippets.

@gmanolache
Last active February 3, 2020 18:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gmanolache/2ae4aed15301e58a29e62b0ecad0ea4c to your computer and use it in GitHub Desktop.
Save gmanolache/2ae4aed15301e58a29e62b0ecad0ea4c to your computer and use it in GitHub Desktop.
Email Client Mandrill
const mandrill = require('mandrill-api/mandrill');
const mandrill_client = new mandrill.Mandrill('YOUR_API_KEY');
const arrayFromObject = (content) => {
const val = [];
for (const key in content) {
val.push({name: key, content: content[key]})
}
return val
};
const sendEmail = async (emailContent, targetEmailAddress) => {
const message = {
'subject': 'Hello Template email subject',
'from_email': process.env.EMAIL_FROM,
'from_name': process.env.EMAIL_NAME_FROM,
'to': [{
'email': targetEmailAddress,
'type': 'to'
}],
'merge_language': 'handlebars',
'headers': {
'Reply-To': process.env.EMAIL_FROM
},
'global_merge_vars': arrayFromObject(emailContent)
};
const template = {
template_name: process.env.MANDRILL_TEMPLATE_ECONOMY,
template_content: [],
message: message,
async: false,
ip_pool: 'Main Pool'
};
return new Promise((resolve, reject) => {
mandrill_client.messages.sendTemplate(template, (result) => {
resolve(result)
}, (e) => {
console.error(e);
reject(e)
});
});
};
//////// Running the code
try {
await sendEmail(emailContent, 'blog@gmanolache.com');
} catch (error) {
console.log("Cannot send the email, error:" + error);
}
////////
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment