You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
3.3 KiB
JavaScript
131 lines
3.3 KiB
JavaScript
11 years ago
|
"use strict";
|
||
|
|
||
9 years ago
|
var async = require('async'),
|
||
11 years ago
|
winston = require('winston'),
|
||
11 years ago
|
templates = require('templates.js'),
|
||
9 years ago
|
nodemailer = require('nodemailer'),
|
||
11 years ago
|
|
||
|
User = require('./user'),
|
||
11 years ago
|
Plugins = require('./plugins'),
|
||
10 years ago
|
meta = require('./meta'),
|
||
10 years ago
|
translator = require('../public/src/modules/translator'),
|
||
11 years ago
|
|
||
10 years ago
|
app;
|
||
|
|
||
|
(function(Emailer) {
|
||
|
Emailer.registerApp = function(expressApp) {
|
||
|
app = expressApp;
|
||
|
return Emailer;
|
||
|
};
|
||
|
|
||
10 years ago
|
Emailer.send = function(template, uid, params, callback) {
|
||
10 years ago
|
callback = callback || function() {};
|
||
10 years ago
|
if (!app) {
|
||
|
winston.warn('[emailer] App not ready!');
|
||
10 years ago
|
return callback();
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
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) {
|
||
10 years ago
|
callback = callback || function() {};
|
||
|
|
||
|
var lang = language || meta.config.defaultLang || 'en_GB';
|
||
10 years ago
|
|
||
10 years ago
|
async.waterfall([
|
||
|
function (next) {
|
||
|
async.parallel({
|
||
|
html: function(next) {
|
||
9 years ago
|
renderAndTranslate('emails/' + template, params, lang, next);
|
||
10 years ago
|
},
|
||
|
plaintext: function(next) {
|
||
9 years ago
|
renderAndTranslate('emails/' + template + '_plaintext', params, lang, next);
|
||
10 years ago
|
},
|
||
|
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'] || '[email protected]',
|
||
|
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
|
||
|
};
|
||
9 years ago
|
Plugins.fireHook('filter:email.modify', data, next);
|
||
10 years ago
|
},
|
||
|
function (data, next) {
|
||
9 years ago
|
if (Plugins.hasListeners('filter:email.send')) {
|
||
|
Plugins.fireHook('filter:email.send', data, next);
|
||
|
} else {
|
||
|
Emailer.sendViaFallback(data, next);
|
||
|
}
|
||
10 years ago
|
}
|
||
9 years ago
|
], function (err) {
|
||
9 years ago
|
callback(err);
|
||
|
});
|
||
10 years ago
|
};
|
||
10 years ago
|
|
||
9 years ago
|
Emailer.sendViaFallback = function(data, callback) {
|
||
|
// Some minor alterations to the data to conform to nodemailer standard
|
||
|
data.text = data.plaintext;
|
||
|
delete data.plaintext;
|
||
10 years ago
|
|
||
9 years ago
|
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);
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
}(module.exports));
|
||
11 years ago
|
|