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