Files
NodeBB/src/emailer.js

125 lines
3.2 KiB
JavaScript
Raw Normal View History

2014-03-13 00:49:32 -04:00
"use strict";
var fs = require('fs'),
async = require('async'),
path = require('path'),
2014-02-14 11:58:51 -05:00
winston = require('winston'),
2014-06-25 18:55:22 -04:00
templates = require('templates.js'),
User = require('./user'),
Plugins = require('./plugins'),
2014-10-08 12:08:35 -04:00
meta = require('./meta'),
translator = require('../public/src/modules/translator'),
2015-09-21 17:57:05 -04:00
tjs = require('templates.js'),
2014-11-29 12:12:02 -05:00
app;
(function(Emailer) {
Emailer.registerApp = function(expressApp) {
app = expressApp;
return Emailer;
};
Emailer.send = function(template, uid, params, callback) {
2015-06-28 21:54:21 -04:00
callback = callback || function() {};
2014-11-29 12:12:02 -05:00
if (!app) {
winston.warn('[emailer] App not ready!');
return callback();
2014-11-29 12:12:02 -05:00
}
2015-06-28 21:54:21 -04:00
async.waterfall([
function(next) {
async.parallel({
email: async.apply(User.getUserField, uid, 'email'),
settings: async.apply(User.getSettings, uid)
}, next);
},
function(results, next) {
if (!results.email) {
winston.warn('uid : ' + uid + ' has no email, not sending.');
return next();
}
params.uid = uid;
Emailer.sendToEmail(template, results.email, results.settings.userLang, params, next);
}
], callback);
};
Emailer.sendToEmail = function(template, email, language, params, callback) {
2015-09-14 16:22:36 -04:00
function renderAndTranslate(tpl, params, callback) {
2015-09-14 21:01:13 -04:00
async.waterfall([
2015-09-21 16:49:59 -04:00
function(next) {
2015-09-21 17:57:05 -04:00
render('emails/partials/footer' + (tpl.indexOf('_plaintext') !== -1 ? '_plaintext' : ''), params, next);
2015-09-21 16:49:59 -04:00
},
function(footer, next) {
params.footer = footer;
2015-09-21 17:57:05 -04:00
render(tpl, params, next);
2015-09-14 16:22:36 -04:00
},
2015-09-21 16:49:59 -04:00
function(html, next) {
2015-09-14 16:22:36 -04:00
translator.translate(html, lang, function(translated) {
next(null, translated);
2014-11-29 12:12:02 -05:00
});
}
2015-09-14 16:22:36 -04:00
], callback);
}
2015-09-21 17:57:05 -04:00
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);
}
}
2015-09-14 16:22:36 -04:00
callback = callback || function() {};
2015-09-22 17:22:49 -04:00
if (!Plugins.hasListeners('filter:email.send')) {
winston.warn('[emailer] No active email plugin found to send "' + template + '" email');
2015-09-14 16:22:36 -04:00
return callback();
}
var lang = language || meta.config.defaultLang || 'en_GB';
2015-06-28 21:54:21 -04:00
2015-09-14 16:22:36 -04:00
async.waterfall([
function (next) {
async.parallel({
html: function(next) {
renderAndTranslate('emails/' + template, params, next);
},
plaintext: function(next) {
renderAndTranslate('emails/' + template + '_plaintext', params, next);
},
subject: function(next) {
translator.translate(params.subject, lang, function(translated) {
next(null, translated);
});
}
}, next);
},
function (results, next) {
var data = {
to: email,
from: meta.config['email:from'] || 'no-reply@localhost.lan',
from_name: meta.config['email:from_name'] || 'NodeBB',
subject: results.subject,
html: results.html,
plaintext: results.plaintext,
template: template,
uid: params.uid,
pid: params.pid,
fromUid: params.fromUid
};
2015-09-22 17:22:49 -04:00
Plugins.fireHook('filter:email.modify', data, next);
2015-09-14 16:22:36 -04:00
},
function (data, next) {
2015-09-22 17:22:49 -04:00
Plugins.fireHook('filter:email.send', data, next);
2015-09-14 16:22:36 -04:00
}
], callback);
2014-11-29 12:12:02 -05:00
};
2015-06-28 21:54:21 -04:00
2014-11-29 12:12:02 -05:00
}(module.exports));