2016-06-28 21:13:01 +03:00
|
|
|
// # Mail
|
|
|
|
// Handles sending email for Ghost
|
2017-09-19 16:24:20 +03:00
|
|
|
var _ = require('lodash'),
|
|
|
|
Promise = require('bluebird'),
|
|
|
|
validator = require('validator'),
|
|
|
|
config = require('../config'),
|
2017-12-12 00:47:46 +03:00
|
|
|
common = require('../lib/common'),
|
2017-02-27 18:53:04 +03:00
|
|
|
settingsCache = require('../settings/cache'),
|
2017-12-11 21:14:05 +03:00
|
|
|
urlService = require('../services/url');
|
2016-06-28 21:13:01 +03:00
|
|
|
|
|
|
|
function GhostMailer() {
|
2017-03-13 23:07:12 +03:00
|
|
|
var nodemailer = require('nodemailer'),
|
|
|
|
transport = config.get('mail') && config.get('mail').transport || 'direct',
|
2016-09-13 18:41:14 +03:00
|
|
|
options = config.get('mail') && _.clone(config.get('mail').options) || {};
|
2016-06-28 21:13:01 +03:00
|
|
|
|
|
|
|
this.state = {};
|
|
|
|
this.transport = nodemailer.createTransport(transport, options);
|
|
|
|
this.state.usingDirect = transport === 'direct';
|
|
|
|
}
|
|
|
|
|
|
|
|
GhostMailer.prototype.from = function () {
|
2017-07-27 11:27:38 +03:00
|
|
|
var from = config.get('mail') && config.get('mail').from,
|
2016-08-19 11:22:07 +03:00
|
|
|
defaultBlogTitle;
|
2016-06-28 21:13:01 +03:00
|
|
|
|
|
|
|
// If we don't have a from address at all
|
|
|
|
if (!from) {
|
|
|
|
// Default to ghost@[blog.url]
|
|
|
|
from = 'ghost@' + this.getDomain();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we do have a from address, and it's just an email
|
|
|
|
if (validator.isEmail(from)) {
|
2017-12-12 00:47:46 +03:00
|
|
|
defaultBlogTitle = settingsCache.get('title') ? settingsCache.get('title').replace(/"/g, '\\"') : common.i18n.t('common.mail.title', {domain: this.getDomain()});
|
2016-08-19 11:22:07 +03:00
|
|
|
from = '"' + defaultBlogTitle + '" <' + from + '>';
|
2016-06-28 21:13:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return from;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Moved it to its own module
|
|
|
|
GhostMailer.prototype.getDomain = function () {
|
2017-12-11 21:14:05 +03:00
|
|
|
var domain = urlService.utils.urlFor('home', true).match(new RegExp('^https?://([^/:?#]+)(?:[/:?#]|$)', 'i'));
|
2016-06-28 21:13:01 +03:00
|
|
|
return domain && domain[1];
|
|
|
|
};
|
|
|
|
|
|
|
|
// Sends an email message enforcing `to` (blog owner) and `from` fields
|
|
|
|
// This assumes that api.settings.read('email') was already done on the API level
|
|
|
|
GhostMailer.prototype.send = function (message) {
|
|
|
|
var self = this,
|
2017-09-19 16:24:20 +03:00
|
|
|
to,
|
2017-12-12 00:47:46 +03:00
|
|
|
help = common.i18n.t('errors.api.authentication.checkEmailConfigInstructions', {url: 'http://docs.ghost.org/v1/docs/mail-config'}),
|
|
|
|
errorMessage = common.i18n.t('errors.mail.failedSendingEmail.error');
|
2016-06-28 21:13:01 +03:00
|
|
|
|
|
|
|
// important to clone message as we modify it
|
|
|
|
message = _.clone(message) || {};
|
|
|
|
to = message.to || false;
|
|
|
|
|
|
|
|
if (!(message && message.subject && message.html && message.to)) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return Promise.reject(new common.errors.EmailError({
|
|
|
|
message: common.i18n.t('errors.mail.incompleteMessageData.error'),
|
2017-09-19 16:24:20 +03:00
|
|
|
help: help
|
|
|
|
}));
|
2016-06-28 21:13:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
message = _.extend(message, {
|
|
|
|
from: self.from(),
|
|
|
|
to: to,
|
|
|
|
generateTextFromHTML: true,
|
|
|
|
encoding: 'base64'
|
|
|
|
});
|
|
|
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
2017-09-19 16:24:20 +03:00
|
|
|
self.transport.sendMail(message, function (err, response) {
|
|
|
|
if (err) {
|
2017-12-12 00:47:46 +03:00
|
|
|
errorMessage += common.i18n.t('errors.mail.reason', {reason: err.message || err});
|
2017-09-19 16:24:20 +03:00
|
|
|
|
2017-12-12 00:47:46 +03:00
|
|
|
return reject(new common.errors.EmailError({
|
2017-09-19 16:24:20 +03:00
|
|
|
message: errorMessage,
|
|
|
|
err: err,
|
|
|
|
help: help
|
|
|
|
}));
|
2016-06-28 21:13:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (self.transport.transportType !== 'DIRECT') {
|
|
|
|
return resolve(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
response.statusHandler.once('failed', function (data) {
|
|
|
|
if (data.error && data.error.errno === 'ENOTFOUND') {
|
2017-12-12 00:47:46 +03:00
|
|
|
errorMessage += common.i18n.t('errors.mail.noMailServerAtAddress.error', {domain: data.domain});
|
2016-06-28 21:13:01 +03:00
|
|
|
}
|
2017-09-19 16:24:20 +03:00
|
|
|
|
2017-12-12 00:47:46 +03:00
|
|
|
return reject(new common.errors.EmailError({
|
2017-09-19 16:24:20 +03:00
|
|
|
message: errorMessage,
|
|
|
|
help: help
|
|
|
|
}));
|
2016-06-28 21:13:01 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
response.statusHandler.once('requeue', function (data) {
|
|
|
|
if (data.error && data.error.message) {
|
2017-12-12 00:47:46 +03:00
|
|
|
errorMessage += common.i18n.t('errors.mail.reason', {reason: data.error.message});
|
2016-06-28 21:13:01 +03:00
|
|
|
}
|
|
|
|
|
2017-12-12 00:47:46 +03:00
|
|
|
return reject(new common.errors.EmailError({
|
2017-09-19 16:24:20 +03:00
|
|
|
message: errorMessage,
|
|
|
|
help: help
|
|
|
|
}));
|
2016-06-28 21:13:01 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
response.statusHandler.once('sent', function () {
|
2017-12-12 00:47:46 +03:00
|
|
|
return resolve(common.i18n.t('notices.mail.messageSent'));
|
2016-06-28 21:13:01 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = GhostMailer;
|