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.
nodebb/src/emailer.js

164 lines
4.3 KiB
JavaScript

"use strict";
var async = require('async');
9 years ago
var winston = require('winston');
var nconf = require('nconf');
var templates = require('templates.js');
var nodemailer = require('nodemailer');
var sendmailTransport = require('nodemailer-sendmail-transport');
var smtpTransport = require('nodemailer-smtp-transport');
9 years ago
var htmlToText = require('html-to-text');
var url = require('url');
var User = require('./user');
var Plugins = require('./plugins');
var meta = require('./meta');
var translator = require('../public/src/modules/translator');
var transports = {
sendmail: nodemailer.createTransport(sendmailTransport()),
gmail: undefined
};
var app;
var fallbackTransport;
(function (Emailer) {
Emailer.registerApp = function (expressApp) {
app = expressApp;
// Enable Gmail transport if enabled in ACP
if (parseInt(meta.config['email:GmailTransport:enabled'], 10) === 1) {
fallbackTransport = transports.gmail = nodemailer.createTransport(smtpTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: meta.config['email:GmailTransport:user'],
pass: meta.config['email:GmailTransport:pass']
}
}));
} else {
fallbackTransport = transports.sendmail;
}
return Emailer;
};
Emailer.send = function (template, uid, params, callback) {
callback = callback || function () {};
if (!app) {
winston.warn('[emailer] App not ready!');
return callback();
}
10 years ago
async.waterfall([
function (next) {
10 years ago
async.parallel({
email: async.apply(User.getUserField, uid, 'email'),
settings: async.apply(User.getSettings, uid)
}, next);
},
function (results, next) {
10 years ago
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) {
callback = callback || function () {};
var lang = language || meta.config.defaultLang || 'en-GB';
10 years ago
async.waterfall([
function (next) {
async.parallel({
html: function (next) {
9 years ago
renderAndTranslate('emails/' + template, params, lang, next);
},
subject: function (next) {
translator.translate(params.subject, lang, function (translated) {
next(null, translated);
});
}
}, next);
},
function (results, next) {
var data = {
_raw: params,
to: email,
9 years ago
from: meta.config['email:from'] || 'no-reply@' + getHostname(),
from_name: meta.config['email:from_name'] || 'NodeBB',
subject: results.subject,
html: results.html,
plaintext: htmlToText.fromString(results.html, {
ignoreImage: true
}),
template: template,
uid: params.uid,
pid: params.pid,
fromUid: params.fromUid
};
9 years ago
Plugins.fireHook('filter:email.modify', data, next);
},
function (data, next) {
9 years ago
if (Plugins.hasListeners('filter:email.send')) {
Plugins.fireHook('filter:email.send', data, next);
} else {
Emailer.sendViaFallback(data, next);
}
}
9 years ago
], function (err) {
9 years ago
if (err && err.code === 'ENOENT') {
callback(new Error('[[error:sendmail-not-found]]'));
} else {
callback(err);
}
});
};
10 years ago
Emailer.sendViaFallback = function (data, callback) {
9 years ago
// Some minor alterations to the data to conform to nodemailer standard
data.text = data.plaintext;
delete data.plaintext;
10 years ago
// NodeMailer uses a combined "from"
data.from = data.from_name + '<' + data.from + '>';
delete data.from_name;
winston.verbose('[emailer] Sending email to uid ' + data.uid);
fallbackTransport.sendMail(data, callback);
9 years ago
};
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) {
render(tpl, params, function (err, html) {
translator.translate(html, lang, function (translated) {
9 years ago
callback(err, translated);
});
});
9 years ago
}
10 years ago
9 years ago
function getHostname() {
9 years ago
var configUrl = nconf.get('url');
var parsed = url.parse(configUrl);
9 years ago
return parsed.hostname;
9 years ago
}
9 years ago
}(module.exports));