2013-09-24 14:46:30 +04:00
|
|
|
var cp = require('child_process'),
|
|
|
|
url = require('url'),
|
|
|
|
_ = require('underscore'),
|
|
|
|
when = require('when'),
|
|
|
|
nodefn = require('when/node/function'),
|
2013-08-21 00:19:47 +04:00
|
|
|
nodemailer = require('nodemailer');
|
|
|
|
|
|
|
|
function GhostMailer(opts) {
|
|
|
|
opts = opts || {};
|
|
|
|
this.transport = opts.transport || null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ## E-mail transport setup
|
|
|
|
// *This promise should always resolve to avoid halting Ghost::init*.
|
2013-11-20 17:58:52 +04:00
|
|
|
GhostMailer.prototype.init = function (ghost, configModule) {
|
2013-08-21 00:19:47 +04:00
|
|
|
this.ghost = ghost;
|
|
|
|
// TODO: fix circular reference ghost -> mail -> api -> ghost, remove this late require
|
|
|
|
this.api = require('./api');
|
2013-11-20 17:58:52 +04:00
|
|
|
// We currently pass in the config module to avoid
|
|
|
|
// circular references, similar to above.
|
|
|
|
this.config = configModule;
|
2013-08-21 00:19:47 +04:00
|
|
|
|
|
|
|
var self = this,
|
2013-11-20 17:58:52 +04:00
|
|
|
config = this.config();
|
2013-08-21 00:19:47 +04:00
|
|
|
|
|
|
|
if (config.mail && config.mail.transport && config.mail.options) {
|
|
|
|
this.createTransport(config);
|
|
|
|
return when.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to detect and fallback to `sendmail`
|
|
|
|
return this.detectSendmail().then(function (binpath) {
|
|
|
|
self.transport = nodemailer.createTransport('sendmail', {
|
|
|
|
path: binpath
|
|
|
|
});
|
|
|
|
self.usingSendmail();
|
|
|
|
}, function () {
|
|
|
|
self.emailDisabled();
|
|
|
|
}).ensure(function () {
|
|
|
|
return when.resolve();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
GhostMailer.prototype.isWindows = function () {
|
|
|
|
return process.platform === 'win32';
|
|
|
|
};
|
|
|
|
|
|
|
|
GhostMailer.prototype.detectSendmail = function () {
|
|
|
|
if (this.isWindows()) {
|
|
|
|
return when.reject();
|
|
|
|
}
|
|
|
|
return when.promise(function (resolve, reject) {
|
|
|
|
cp.exec('which sendmail', function (err, stdout) {
|
|
|
|
if (err && !/bin\/sendmail/.test(stdout)) {
|
|
|
|
return reject();
|
|
|
|
}
|
2013-09-26 17:07:52 +04:00
|
|
|
resolve(stdout.toString().replace(/(\n|\r|\r\n)$/, ''));
|
2013-08-21 00:19:47 +04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
GhostMailer.prototype.createTransport = function (config) {
|
|
|
|
this.transport = nodemailer.createTransport(config.mail.transport, _.clone(config.mail.options));
|
|
|
|
};
|
|
|
|
|
|
|
|
GhostMailer.prototype.usingSendmail = function () {
|
|
|
|
this.api.notifications.add({
|
|
|
|
type: 'info',
|
|
|
|
message: [
|
|
|
|
"Ghost is attempting to use your server's <b>sendmail</b> to send e-mail.",
|
|
|
|
"It is recommended that you explicitly configure an e-mail service,",
|
2013-09-16 21:34:20 +04:00
|
|
|
"See <a href=\"http://docs.ghost.org/mail\">http://docs.ghost.org/mail</a> for instructions"
|
2013-08-21 00:19:47 +04:00
|
|
|
].join(' '),
|
|
|
|
status: 'persistent',
|
|
|
|
id: 'ghost-mail-fallback'
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
GhostMailer.prototype.emailDisabled = function () {
|
|
|
|
this.api.notifications.add({
|
|
|
|
type: 'warn',
|
|
|
|
message: [
|
|
|
|
"Ghost is currently unable to send e-mail.",
|
2013-09-16 21:34:20 +04:00
|
|
|
"See <a href=\"http://docs.ghost.org/mail\">http://docs.ghost.org/mail</a> for instructions"
|
2013-08-21 00:19:47 +04:00
|
|
|
].join(' '),
|
|
|
|
status: 'persistent',
|
|
|
|
id: 'ghost-mail-disabled'
|
|
|
|
});
|
|
|
|
this.transport = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Sends an e-mail message enforcing `to` (blog owner) and `from` fields
|
|
|
|
GhostMailer.prototype.send = function (message) {
|
|
|
|
if (!this.transport) {
|
2013-09-04 17:57:41 +04:00
|
|
|
return when.reject(new Error('Email Error: No e-mail transport configured.'));
|
2013-08-21 00:19:47 +04:00
|
|
|
}
|
|
|
|
if (!(message && message.subject && message.html)) {
|
2013-09-04 17:57:41 +04:00
|
|
|
return when.reject(new Error('Email Error: Incomplete message data.'));
|
2013-08-21 00:19:47 +04:00
|
|
|
}
|
|
|
|
|
2013-11-20 17:58:52 +04:00
|
|
|
var from = this.config().mail.fromaddress || this.ghost.settings('email'),
|
2013-09-15 21:52:37 +04:00
|
|
|
to = message.to || this.ghost.settings('email'),
|
2013-08-21 00:19:47 +04:00
|
|
|
sendMail = nodefn.lift(this.transport.sendMail.bind(this.transport));
|
|
|
|
|
|
|
|
message = _.extend(message, {
|
|
|
|
from: from,
|
|
|
|
to: to,
|
|
|
|
generateTextFromHTML: true
|
|
|
|
});
|
|
|
|
|
2013-09-04 17:57:41 +04:00
|
|
|
return sendMail(message).otherwise(function (error) {
|
|
|
|
// Proxy the error message so we can add 'Email Error:' to the beginning to make it clearer.
|
|
|
|
error = _.isString(error) ? 'Email Error:' + error : (_.isObject(error) ? 'Email Error: ' + error.message : 'Email Error: Unknown Email Error');
|
|
|
|
return when.reject(new Error(error));
|
2013-08-21 00:19:47 +04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = GhostMailer;
|