Files
NodeBB/src/emailer.js

59 lines
1.5 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'),
2013-12-31 09:33:25 -05:00
Meta = require('./meta'),
2014-06-27 14:52:12 -04:00
translator = require('../public/src/translator'),
2014-03-13 00:49:32 -04:00
app = {},
Emailer = {};
2014-03-13 00:49:32 -04:00
Emailer.registerApp = function(expressApp) {
app = expressApp;
return Emailer;
};
Emailer.send = function(template, uid, params) {
async.parallel({
html: function(next) {
2014-03-13 00:49:32 -04:00
app.render('emails/' + template, params, next);
},
plaintext: function(next) {
2014-03-13 00:49:32 -04:00
app.render('emails/' + template + '_plaintext', params, next);
2014-06-27 14:52:12 -04:00
},
email: async.apply(User.getUserField, uid, 'email'),
settings: async.apply(User.getSettings, uid)
}, function(err, results) {
async.map([results.html, results.plaintext, params.subject], function(raw, next) {
2014-06-27 14:52:12 -04:00
translator.translate(raw, results.settings.language || meta.config.defaultLang || 'en_GB', function(translated) {
next(undefined, translated);
});
}, function(err, translated) {
2014-02-05 18:25:13 -05:00
if(err) {
return winston.error(err.message);
2014-06-27 14:52:12 -04:00
} else if (!results.email) {
2014-02-05 18:25:13 -05:00
return winston.warn('uid : ' + uid + ' has no email, not sending.');
}
Plugins.fireHook('action:email.send', {
2014-06-27 14:52:12 -04:00
to: results.email,
2014-02-05 18:25:13 -05:00
from: Meta.config['email:from'] || 'no-reply@localhost.lan',
subject: translated[2],
2014-06-27 14:52:12 -04:00
html: translated[0],
plaintext: translated[1],
2014-02-05 18:25:13 -05:00
template: template,
uid: uid
});
});
});
};
2014-04-10 20:31:57 +01:00
module.exports = Emailer;