2014-03-13 00:49:32 -04:00
|
|
|
"use strict";
|
|
|
|
|
|
2014-01-25 16:39:27 -05:00
|
|
|
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'),
|
2014-01-25 16:39:27 -05:00
|
|
|
|
|
|
|
|
User = require('./user'),
|
2013-12-30 14:02:43 -05:00
|
|
|
Plugins = require('./plugins'),
|
2013-12-31 09:33:25 -05:00
|
|
|
Meta = require('./meta'),
|
2013-12-30 14:02:43 -05:00
|
|
|
|
2014-03-13 00:49:32 -04:00
|
|
|
app = {},
|
2014-01-04 18:03:54 -05:00
|
|
|
Emailer = {};
|
2013-12-30 14:02:43 -05:00
|
|
|
|
2014-01-25 16:39:27 -05:00
|
|
|
|
2014-03-13 00:49:32 -04:00
|
|
|
Emailer.registerApp = function(expressApp) {
|
|
|
|
|
app = expressApp;
|
|
|
|
|
return Emailer;
|
|
|
|
|
};
|
2013-12-30 14:02:43 -05:00
|
|
|
|
|
|
|
|
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);
|
2013-12-30 14:02:43 -05:00
|
|
|
},
|
|
|
|
|
plaintext: function(next) {
|
2014-03-13 00:49:32 -04:00
|
|
|
app.render('emails/' + template + '_plaintext', params, next);
|
2013-12-30 14:02:43 -05:00
|
|
|
}
|
|
|
|
|
}, function(err, results) {
|
|
|
|
|
User.getUserField(uid, 'email', function(err, email) {
|
2014-02-05 18:25:13 -05:00
|
|
|
if(err) {
|
|
|
|
|
return winston.error(err.message);
|
2013-12-30 14:02:43 -05:00
|
|
|
}
|
2014-02-05 18:25:13 -05:00
|
|
|
|
|
|
|
|
if(!email) {
|
|
|
|
|
return winston.warn('uid : ' + uid + ' has no email, not sending.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Plugins.fireHook('action:email.send', {
|
|
|
|
|
to: email,
|
|
|
|
|
from: Meta.config['email:from'] || 'no-reply@localhost.lan',
|
|
|
|
|
subject: params.subject,
|
|
|
|
|
html: results.html,
|
|
|
|
|
plaintext: results.plaintext,
|
|
|
|
|
template: template,
|
|
|
|
|
uid: uid
|
|
|
|
|
});
|
2013-12-30 14:02:43 -05:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-04-10 20:31:57 +01:00
|
|
|
module.exports = Emailer;
|