This commit is contained in:
Julian Lam
2015-11-06 14:01:34 -05:00
parent 18bd4c1cda
commit 48b5bab849
6 changed files with 48 additions and 62 deletions

View File

@@ -1,10 +1,9 @@
"use strict";
var fs = require('fs'),
async = require('async'),
path = require('path'),
var async = require('async'),
winston = require('winston'),
templates = require('templates.js'),
nodemailer = require('nodemailer'),
User = require('./user'),
Plugins = require('./plugins'),
@@ -45,49 +44,18 @@ var fs = require('fs'),
};
Emailer.sendToEmail = function(template, email, language, params, callback) {
function renderAndTranslate(tpl, params, callback) {
async.waterfall([
function(next) {
render('emails/partials/footer' + (tpl.indexOf('_plaintext') !== -1 ? '_plaintext' : ''), params, next);
},
function(footer, next) {
params.footer = footer;
render(tpl, params, next);
},
function(html, next) {
translator.translate(html, lang, function(translated) {
next(null, translated);
});
}
], callback);
}
function render(tpl, params, next) {
if (meta.config['email:custom:' + tpl.replace('emails/', '')]) {
var text = templates.parse(meta.config['email:custom:' + tpl.replace('emails/', '')], params);
next(null, text);
} else {
app.render(tpl, params, next);
}
}
callback = callback || function() {};
if (!Plugins.hasListeners('filter:email.send')) {
winston.warn('[emailer] No active email plugin found to send "' + template + '" email');
return callback();
}
var lang = language || meta.config.defaultLang || 'en_GB';
async.waterfall([
function (next) {
async.parallel({
html: function(next) {
renderAndTranslate('emails/' + template, params, next);
renderAndTranslate('emails/' + template, params, lang, next);
},
plaintext: function(next) {
renderAndTranslate('emails/' + template + '_plaintext', params, next);
renderAndTranslate('emails/' + template + '_plaintext', params, lang, next);
},
subject: function(next) {
translator.translate(params.subject, lang, function(translated) {
@@ -112,14 +80,51 @@ var fs = require('fs'),
Plugins.fireHook('filter:email.modify', data, next);
},
function (data, next) {
Plugins.fireHook('filter:email.send', data, next);
if (Plugins.hasListeners('filter:email.send')) {
Plugins.fireHook('filter:email.send', data, next);
} else {
Emailer.sendViaFallback(data, next);
}
}
], function (err, data) {
], function (err) {
callback(err);
});
};
Emailer.sendViaFallback = function(data, callback) {
// Some minor alterations to the data to conform to nodemailer standard
data.text = data.plaintext;
delete data.plaintext;
nodemailer.mail(data);
callback(null);
};
function render(tpl, params, next) {
if (meta.config['email:custom:' + tpl.replace('emails/', '')]) {
var text = templates.parse(meta.config['email:custom:' + tpl.replace('emails/', '')], params);
next(null, text);
} else {
app.render(tpl, params, next);
}
}
function renderAndTranslate(tpl, params, lang, callback) {
async.waterfall([
function(next) {
render('emails/partials/footer' + (tpl.indexOf('_plaintext') !== -1 ? '_plaintext' : ''), params, next);
},
function(footer, next) {
params.footer = footer;
render(tpl, params, next);
},
function(html, next) {
translator.translate(html, lang, function(translated) {
next(null, translated);
});
}
], callback);
}
}(module.exports));