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.
78 lines
2.0 KiB
JavaScript
78 lines
2.0 KiB
JavaScript
11 years ago
|
"use strict";
|
||
|
|
||
11 years ago
|
var fs = require('fs'),
|
||
|
async = require('async'),
|
||
|
path = require('path'),
|
||
11 years ago
|
winston = require('winston'),
|
||
11 years ago
|
templates = require('templates.js'),
|
||
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
|
if (!callback) { callback = function() {}; }
|
||
10 years ago
|
if (!app) {
|
||
|
winston.warn('[emailer] App not ready!');
|
||
10 years ago
|
return callback();
|
||
10 years ago
|
}
|
||
|
|
||
|
async.parallel({
|
||
|
html: function(next) {
|
||
|
app.render('emails/' + template, params, next);
|
||
|
},
|
||
|
plaintext: function(next) {
|
||
|
app.render('emails/' + template + '_plaintext', params, next);
|
||
|
},
|
||
|
email: async.apply(User.getUserField, uid, 'email'),
|
||
|
settings: async.apply(User.getSettings, uid)
|
||
|
}, function(err, results) {
|
||
10 years ago
|
if (err) {
|
||
10 years ago
|
winston.error('[emailer] Error sending digest : ' + err.stack);
|
||
|
return callback(err);
|
||
10 years ago
|
}
|
||
10 years ago
|
async.map([results.html, results.plaintext, params.subject], function(raw, next) {
|
||
10 years ago
|
translator.translate(raw, results.settings.userLang || meta.config.defaultLang || 'en_GB', function(translated) {
|
||
10 years ago
|
next(undefined, translated);
|
||
10 years ago
|
});
|
||
10 years ago
|
}, function(err, translated) {
|
||
|
if (err) {
|
||
10 years ago
|
winston.error(err.message);
|
||
|
return callback(err);
|
||
10 years ago
|
} else if (!results.email) {
|
||
10 years ago
|
winston.warn('uid : ' + uid + ' has no email, not sending.');
|
||
|
return callback();
|
||
10 years ago
|
}
|
||
|
|
||
|
if (Plugins.hasListeners('action:email.send')) {
|
||
|
Plugins.fireHook('action:email.send', {
|
||
|
to: results.email,
|
||
|
from: meta.config['email:from'] || '[email protected]',
|
||
|
subject: translated[2],
|
||
|
html: translated[0],
|
||
|
plaintext: translated[1],
|
||
|
template: template,
|
||
10 years ago
|
uid: uid,
|
||
10 years ago
|
pid: params.pid,
|
||
|
fromUid: params.fromUid
|
||
10 years ago
|
});
|
||
10 years ago
|
callback();
|
||
10 years ago
|
} else {
|
||
|
winston.warn('[emailer] No active email plugin found!');
|
||
10 years ago
|
callback();
|
||
10 years ago
|
}
|
||
|
});
|
||
11 years ago
|
});
|
||
10 years ago
|
};
|
||
|
}(module.exports));
|
||
11 years ago
|
|