2017-11-01 19:17:35 +00:00
|
|
|
'use strict';
|
2017-11-21 05:04:07 +00:00
|
|
|
const jsdom = require('jsdom');
|
2017-11-15 10:46:54 +00:00
|
|
|
var JSDOM = jsdom.JSDOM;
|
2017-11-01 19:17:35 +00:00
|
|
|
|
2017-10-31 23:05:50 +00:00
|
|
|
module.exports = {
|
2017-11-15 10:46:54 +00:00
|
|
|
send: function(emailSettings, emailTemplateVars, smtpTransport, cb){
|
2017-11-15 11:46:40 +00:00
|
|
|
var parsedTemplate = this.parseTemplate(emailSettings.htmlTemplate, emailTemplateVars, false);
|
|
|
|
var parsedSubject = this.parseTemplate(emailSettings.subject, emailTemplateVars, true);
|
|
|
|
|
2017-10-31 23:05:50 +00:00
|
|
|
var mailOptions = {
|
2017-11-01 23:44:05 +00:00
|
|
|
replyTo: emailSettings.fromEmails,
|
|
|
|
from: 'noreply@tellform.com',
|
2017-11-01 22:13:31 +00:00
|
|
|
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
|
|
|
|
2017-11-15 10:46:54 +00:00
|
|
|
smtpTransport.sendMail(mailOptions, function(err){
|
|
|
|
cb(err);
|
2017-11-02 21:29:02 +00:00
|
|
|
});
|
2017-10-31 23:05:50 +00:00
|
|
|
},
|
|
|
|
|
2017-11-15 11:46:40 +00:00
|
|
|
parseTemplate: function(emailTemplate, emailTemplateVars, onlyText){
|
2017-11-15 10:46:54 +00:00
|
|
|
var dom = new JSDOM('<!doctype html>'+emailTemplate);
|
|
|
|
|
|
|
|
Object.keys(emailTemplateVars).forEach(function (key) {
|
2017-11-21 05:04:07 +00:00
|
|
|
var elem = dom.window.document.querySelector('span.placeholder-tag[data-id=\'' + key + '\']');
|
2017-11-15 10:46:54 +00:00
|
|
|
if(elem !== null){
|
|
|
|
elem.outerHTML = emailTemplateVars[key];
|
|
|
|
}
|
2017-10-31 23:05:50 +00:00
|
|
|
});
|
|
|
|
|
2017-11-15 11:46:40 +00:00
|
|
|
if(onlyText){
|
|
|
|
return dom.window.document.documentElement.textContent;
|
|
|
|
}
|
2017-11-15 10:46:54 +00:00
|
|
|
return dom.serialize();
|
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){
|
2017-11-01 23:44:05 +00:00
|
|
|
if(field.hasOwnProperty('globalId') && field.hasOwnProperty('fieldValue')){
|
2017-11-15 10:46:54 +00:00
|
|
|
formFieldDict[field.globalId+''] = field.fieldValue+'';
|
2017-11-01 19:17:35 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return formFieldDict;
|
2017-10-31 23:05:50 +00:00
|
|
|
}
|
2017-11-01 19:46:15 +00:00
|
|
|
};
|