|
|
@ -109,22 +109,33 @@ EmailService.prototype.start = function(opts, cb) { |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
EmailService.prototype._compileTemplate = function(template) { |
|
|
|
var lines = template.split('\n'); |
|
|
|
return { |
|
|
|
subject: lines[0], |
|
|
|
body: _.rest(lines).join('\n'), |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
// TODO: cache for X minutes
|
|
|
|
EmailService.prototype._readTemplate = function(filename, language, cb) { |
|
|
|
EmailService.prototype._readTemplateFile = function(language, filename, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
var fullFilename = path.join(self.templatePath, language, filename + '.plain'); |
|
|
|
var fullFilename = path.join(self.templatePath, language, filename); |
|
|
|
fs.readFile(fullFilename, 'utf8', function(err, template) { |
|
|
|
if (err) { |
|
|
|
log.error('Could not read template file ' + fullFilename, err); |
|
|
|
return cb(err); |
|
|
|
return cb(new Error('Could not read template file ' + fullFilename, err)); |
|
|
|
} |
|
|
|
var lines = template.split('\n'); |
|
|
|
return cb(null, { |
|
|
|
subject: lines[0], |
|
|
|
body: _.rest(lines).join('\n'), |
|
|
|
}); |
|
|
|
return cb(null, template); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
// TODO: cache for X minutes
|
|
|
|
EmailService.prototype._loadTemplate = function(emailType, recipient, extension, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
self._readTemplateFile(recipient.language, emailType.filename + extension, function(err, template) { |
|
|
|
if (err) return cb(err); |
|
|
|
return cb(null, self._compileTemplate(template)); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
@ -213,8 +224,11 @@ EmailService.prototype._send = function(email, cb) { |
|
|
|
from: email.from, |
|
|
|
to: email.to, |
|
|
|
subject: email.subject, |
|
|
|
text: email.body, |
|
|
|
text: email.bodyPlain, |
|
|
|
}; |
|
|
|
if (email.bodyHtml) { |
|
|
|
mailOptions.html = email.bodyHtml; |
|
|
|
} |
|
|
|
self.mailer.sendMail(mailOptions, function(err, result) { |
|
|
|
if (err) { |
|
|
|
log.error('An error occurred when trying to send email to ' + email.to, err); |
|
|
@ -233,20 +247,23 @@ EmailService.prototype._readAndApplyTemplates = function(notification, emailType |
|
|
|
async.waterfall([ |
|
|
|
|
|
|
|
function(next) { |
|
|
|
async.parallel([ |
|
|
|
|
|
|
|
function(next) { |
|
|
|
self._readTemplate(emailType.filename, recipient.language, next); |
|
|
|
}, |
|
|
|
function(next) { |
|
|
|
self._getDataForTemplate(notification, recipient, next); |
|
|
|
}, |
|
|
|
], next); |
|
|
|
self._getDataForTemplate(notification, recipient, next); |
|
|
|
}, |
|
|
|
function(data, next) { |
|
|
|
async.map(['plain', 'html'], function(type, next) { |
|
|
|
self._loadTemplate(emailType, recipient, '.' + type, function(err, template) { |
|
|
|
if (err && type == 'html') return next(); |
|
|
|
if (err) return next(err); |
|
|
|
self._applyTemplate(template, data, function(err, res) { |
|
|
|
return next(err, [type, res]); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}, function(err, res) { |
|
|
|
return next(err, _.zipObject(res)); |
|
|
|
}); |
|
|
|
}, |
|
|
|
function(result, next) { |
|
|
|
var template = result[0]; |
|
|
|
var data = result[1]; |
|
|
|
self._applyTemplate(template, data, next); |
|
|
|
next(null, result); |
|
|
|
}, |
|
|
|
], function(err, res) { |
|
|
|
next(err, [recipient.language, res]); |
|
|
@ -288,8 +305,9 @@ EmailService.prototype.sendEmail = function(notification, cb) { |
|
|
|
copayerId: recipient.copayerId, |
|
|
|
from: self.from, |
|
|
|
to: recipient.emailAddress, |
|
|
|
subject: content.subject, |
|
|
|
body: content.body, |
|
|
|
subject: content.plain.subject, |
|
|
|
bodyPlain: content.plain.body, |
|
|
|
bodyHtml: content.html ? content.html.body : null, |
|
|
|
notificationId: notification.id, |
|
|
|
}); |
|
|
|
self.storage.storeEmail(email, function(err) { |
|
|
|