2013-09-24 14:46:30 +04:00
|
|
|
var cp = require('child_process'),
|
2014-02-05 12:40:30 +04:00
|
|
|
_ = require('lodash'),
|
2013-09-24 14:46:30 +04:00
|
|
|
when = require('when'),
|
2014-06-16 07:33:25 +04:00
|
|
|
nodefn = require('when/node'),
|
2013-12-06 12:51:35 +04:00
|
|
|
nodemailer = require('nodemailer'),
|
|
|
|
config = require('./config');
|
2013-08-21 00:19:47 +04:00
|
|
|
|
|
|
|
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-12-06 12:51:35 +04:00
|
|
|
GhostMailer.prototype.init = function () {
|
|
|
|
var self = this;
|
2014-06-08 16:39:28 +04:00
|
|
|
self.state = {};
|
2014-07-17 18:33:21 +04:00
|
|
|
if (config.mail && config.mail.transport) {
|
2013-12-20 16:57:21 +04:00
|
|
|
this.createTransport();
|
2013-08-21 00:19:47 +04:00
|
|
|
return when.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to detect and fallback to `sendmail`
|
|
|
|
return this.detectSendmail().then(function (binpath) {
|
|
|
|
self.transport = nodemailer.createTransport('sendmail', {
|
|
|
|
path: binpath
|
|
|
|
});
|
2014-06-08 16:39:28 +04:00
|
|
|
self.state.usingSendmail = true;
|
2013-08-21 00:19:47 +04:00
|
|
|
}, function () {
|
2014-06-08 16:39:28 +04:00
|
|
|
self.state.emailDisabled = true;
|
|
|
|
self.transport = null;
|
2013-08-21 00:19:47 +04:00
|
|
|
}).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
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-12-20 16:57:21 +04:00
|
|
|
GhostMailer.prototype.createTransport = function () {
|
2014-07-17 18:33:21 +04:00
|
|
|
this.transport = nodemailer.createTransport(config.mail.transport, _.clone(config.mail.options) || {});
|
2013-08-21 00:19:47 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-02-24 20:28:07 +04:00
|
|
|
GhostMailer.prototype.fromAddress = function () {
|
2014-07-17 18:33:21 +04:00
|
|
|
var from = config.mail && config.mail.fromaddress,
|
2014-02-24 20:28:07 +04:00
|
|
|
domain;
|
|
|
|
|
|
|
|
if (!from) {
|
|
|
|
// Extract the domain name from url set in config.js
|
2014-07-17 18:33:21 +04:00
|
|
|
domain = config.url.match(new RegExp("^https?://([^/:?#]+)(?:[/:?#]|$)", "i"));
|
2014-02-24 20:28:07 +04:00
|
|
|
domain = domain && domain[1];
|
|
|
|
|
2014-03-06 15:03:00 +04:00
|
|
|
// Default to ghost@[blog.url]
|
|
|
|
from = 'ghost@' + domain;
|
2014-02-24 20:28:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return from;
|
|
|
|
};
|
|
|
|
|
2013-08-21 00:19:47 +04:00
|
|
|
// Sends an e-mail message enforcing `to` (blog owner) and `from` fields
|
2014-06-08 16:39:28 +04:00
|
|
|
// This assumes that api.settings.read('email') was aready done on the API level
|
|
|
|
GhostMailer.prototype.send = function (message) {
|
2014-05-09 03:12:18 +04:00
|
|
|
var self = this,
|
2014-06-08 16:39:28 +04:00
|
|
|
to,
|
|
|
|
sendMail;
|
|
|
|
|
|
|
|
message = message || {};
|
|
|
|
to = message.to || false;
|
2013-12-20 16:57:21 +04:00
|
|
|
|
2013-08-21 00:19:47 +04:00
|
|
|
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
|
|
|
}
|
2014-06-08 16:39:28 +04:00
|
|
|
if (!(message && message.subject && message.html && message.to)) {
|
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
|
|
|
}
|
2014-06-08 16:39:28 +04:00
|
|
|
sendMail = nodefn.lift(self.transport.sendMail.bind(self.transport));
|
2013-12-20 16:57:21 +04:00
|
|
|
|
2014-06-08 16:39:28 +04:00
|
|
|
message = _.extend(message, {
|
|
|
|
from: self.fromAddress(),
|
|
|
|
to: to,
|
|
|
|
generateTextFromHTML: true
|
2013-08-21 00:19:47 +04:00
|
|
|
});
|
2014-06-08 16:39:28 +04:00
|
|
|
return sendMail(message);
|
2013-08-21 00:19:47 +04:00
|
|
|
};
|
|
|
|
|
2013-11-28 06:45:01 +04:00
|
|
|
module.exports = new GhostMailer();
|