2017-11-01 19:17:35 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const constants = require('./constants');
|
|
|
|
|
2017-10-31 23:05:50 +00:00
|
|
|
module.exports = {
|
2017-11-01 18:25:24 +00:00
|
|
|
send: function(emailSettings, emailTemplateVars, smtpTransport, varFormat, cb){
|
2017-11-01 19:17:35 +00:00
|
|
|
var parsedTemplate = this.parseTemplate(emailSettings.htmlTemplate, emailTemplateVars);
|
|
|
|
var parsedSubject = this.parseTemplate(emailSettings.body, emailTemplateVars);
|
2017-10-31 23:05:50 +00:00
|
|
|
var mailOptions = {
|
2017-11-01 22:13:31 +00:00
|
|
|
from: emailSettings.fromEmails,
|
|
|
|
cc: emailSettings.toEmails,
|
2017-10-31 23:05:50 +00:00
|
|
|
subject: parsedSubject,
|
|
|
|
html: parsedTemplate
|
2017-11-01 19:46:15 +00:00
|
|
|
};
|
2017-10-31 23:05:50 +00:00
|
|
|
|
|
|
|
smtpTransport.sendMail(mailOptions, cb);
|
|
|
|
},
|
|
|
|
|
2017-11-01 18:25:24 +00:00
|
|
|
parseTemplate: function(emailTemplate, emailAttrs, varFormat){
|
2017-10-31 23:05:50 +00:00
|
|
|
var resolvedTemplate = emailTemplate;
|
2017-11-01 19:46:15 +00:00
|
|
|
var that = this;
|
2017-10-31 23:05:50 +00:00
|
|
|
Object.keys(emailAttrs).forEach(function (key) {
|
2017-11-01 20:38:42 +00:00
|
|
|
resolvedTemplate = that.replaceTemplateVal(key, emailAttrs[key], resolvedTemplate, varFormat);
|
2017-10-31 23:05:50 +00:00
|
|
|
});
|
|
|
|
return resolvedTemplate;
|
|
|
|
},
|
|
|
|
|
2017-11-01 18:25:24 +00:00
|
|
|
replaceTemplateVal: function(key, val, template, varFormat){
|
2017-11-01 19:46:15 +00:00
|
|
|
return template.replace( new RegExp(varFormat[0] + key + varFormat[1], 'g'), val);
|
2017-11-01 19:17:35 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
createFieldDict: function(form_fields){
|
2017-11-01 19:46:15 +00:00
|
|
|
var formFieldDict = {};
|
2017-11-01 19:17:35 +00:00
|
|
|
form_fields.forEach(function(field){
|
|
|
|
if(field.hasOwnProperty('_id') && field.hasOwnProperty('fieldValue')){
|
|
|
|
formFieldDict[field._id] = field.fieldValue;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return formFieldDict;
|
2017-10-31 23:05:50 +00:00
|
|
|
}
|
2017-11-01 19:46:15 +00:00
|
|
|
};
|