@tasolutions/express-core
Version:
All libs for express
34 lines (31 loc) • 987 B
JavaScript
const config = require('../config');
const sgMail = require('@sendgrid/mail');
module.exports = {
/**
* This function sendMail
* @param receiver
* @param templateId
* @param data
*/
sendMail: async (receiver, templateId, data = {}, options = {}) => {
sgMail.setApiKey(config.sendGrid.apiKey);
const msg = {
to: receiver,
from: options.from ? options.from : config.sendGrid.from,
cc: options.cc ? options.cc : config.sendGrid.cc,
templateId: templateId,
dynamicTemplateData: data
};
console.log('[sendMail] Msg:', msg);
return sgMail.send(msg).then(() => {
console.log('[sendMail] Success');
return true;
}, error => {
console.log('[sendMail] Error:', error);
if (error.response) {
console.error(error.response.body)
return false;
}
});
}
}