2014-08-31 19:36:32 +04:00
|
|
|
var _ = require('lodash'),
|
2014-08-17 10:17:23 +04:00
|
|
|
Promise = require('bluebird'),
|
2013-12-06 12:51:35 +04:00
|
|
|
nodemailer = require('nodemailer'),
|
2014-09-21 01:11:30 +04:00
|
|
|
validator = require('validator'),
|
2013-12-06 12:51:35 +04:00
|
|
|
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();
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.resolve();
|
2013-08-21 00:19:47 +04:00
|
|
|
}
|
|
|
|
|
2014-08-31 19:36:32 +04:00
|
|
|
self.transport = nodemailer.createTransport('direct');
|
|
|
|
self.state.usingDirect = true;
|
2014-08-17 10:17:23 +04:00
|
|
|
|
2014-08-31 19:36:32 +04:00
|
|
|
return Promise.resolve();
|
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-09-21 01:11:30 +04:00
|
|
|
GhostMailer.prototype.from = function () {
|
|
|
|
var from = config.mail && (config.mail.from || config.mail.fromaddress);
|
2014-02-24 20:28:07 +04:00
|
|
|
|
2014-09-21 01:11:30 +04:00
|
|
|
// If we don't have a from address at all
|
2014-02-24 20:28:07 +04:00
|
|
|
if (!from) {
|
2014-03-06 15:03:00 +04:00
|
|
|
// Default to ghost@[blog.url]
|
2014-09-21 01:11:30 +04:00
|
|
|
from = 'ghost@' + this.getDomain();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we do have a from address, and it's just an email
|
|
|
|
if (validator.isEmail(from)) {
|
|
|
|
if (!config.theme.title) {
|
|
|
|
config.theme.title = 'Ghost at ' + this.getDomain();
|
|
|
|
}
|
|
|
|
from = config.theme.title + ' <' + from + '>';
|
2014-02-24 20:28:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return from;
|
|
|
|
};
|
|
|
|
|
2014-09-21 01:11:30 +04:00
|
|
|
// Moved it to its own module
|
|
|
|
GhostMailer.prototype.getDomain = function () {
|
|
|
|
var domain = config.url.match(new RegExp('^https?://([^/:?#]+)(?:[/:?#]|$)', 'i'));
|
|
|
|
return domain && domain[1];
|
|
|
|
};
|
|
|
|
|
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) {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.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)) {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.reject(new Error('Email Error: Incomplete message data.'));
|
2013-08-21 00:19:47 +04:00
|
|
|
}
|
2014-08-17 10:17:23 +04:00
|
|
|
sendMail = Promise.promisify(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, {
|
2014-09-21 01:11:30 +04:00
|
|
|
from: self.from(),
|
2014-06-08 16:39:28 +04:00
|
|
|
to: to,
|
2014-10-28 16:03:32 +03:00
|
|
|
generateTextFromHTML: true,
|
2014-11-16 21:14:18 +03:00
|
|
|
encoding: 'base64'
|
2013-08-21 00:19:47 +04:00
|
|
|
});
|
2014-08-31 19:36:32 +04:00
|
|
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
sendMail(message, function (error, response) {
|
|
|
|
if (error) {
|
|
|
|
return reject(new Error(error));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self.transport.transportType !== 'DIRECT') {
|
|
|
|
return resolve(response);
|
|
|
|
}
|
|
|
|
|
2014-09-10 08:06:24 +04:00
|
|
|
response.statusHandler.once('failed', function (data) {
|
2014-08-31 19:36:32 +04:00
|
|
|
var reason = 'Email Error: Failed sending email';
|
2014-09-10 08:06:24 +04:00
|
|
|
if (data.error.errno === 'ENOTFOUND') {
|
2014-08-31 19:36:32 +04:00
|
|
|
reason += ': there is no mail server at this address: ' + data.domain;
|
|
|
|
}
|
|
|
|
reason += '.';
|
|
|
|
return reject(new Error(reason));
|
|
|
|
});
|
|
|
|
|
2014-09-10 08:06:24 +04:00
|
|
|
response.statusHandler.once('requeue', function (data) {
|
|
|
|
return reject(new Error('Email Error: message was not sent, requeued. Probably will not be sent. :( \nMore info: ' + data.error.message));
|
2014-08-31 19:36:32 +04:00
|
|
|
});
|
|
|
|
|
2014-09-10 08:06:24 +04:00
|
|
|
response.statusHandler.once('sent', function () {
|
|
|
|
return resolve('Message was accepted by the mail server. Make sure to check inbox and spam folders. :)');
|
2014-08-31 19:36:32 +04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-08-21 00:19:47 +04:00
|
|
|
};
|
|
|
|
|
2013-11-28 06:45:01 +04:00
|
|
|
module.exports = new GhostMailer();
|