2016-06-28 21:13:01 +03:00
|
|
|
// # Mail
|
|
|
|
// Handles sending email for Ghost
|
2019-10-06 14:22:56 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
const Promise = require('bluebird');
|
|
|
|
const validator = require('validator');
|
|
|
|
const config = require('../../config');
|
|
|
|
const common = require('../../lib/common');
|
|
|
|
const settingsCache = require('../settings/cache');
|
|
|
|
const urlUtils = require('../../lib/url-utils');
|
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';
|
|
|
|
}
|
2019-10-06 14:26:14 +03:00
|
|
|
function getDomain() {
|
|
|
|
const domain = urlUtils.urlFor('home', true).match(new RegExp('^https?://([^/:?#]+)(?:[/:?#]|$)', 'i'));
|
|
|
|
return domain && domain[1];
|
|
|
|
}
|
2016-06-28 21:13:01 +03:00
|
|
|
|
2019-10-06 14:26:14 +03:00
|
|
|
function getFromAddress() {
|
|
|
|
const configAddress = config.get('mail') && config.get('mail').from;
|
2016-06-28 21:13:01 +03:00
|
|
|
|
2019-10-06 14:26:14 +03:00
|
|
|
const address = configAddress;
|
2016-06-28 21:13:01 +03:00
|
|
|
// If we don't have a from address at all
|
2019-10-06 14:26:14 +03:00
|
|
|
if (!address) {
|
2019-10-06 15:02:10 +03:00
|
|
|
// Default to noreply@[blog.url]
|
2019-10-06 14:26:14 +03:00
|
|
|
return getFromAddress(`noreply@${getDomain()}`);
|
2016-06-28 21:13:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we do have a from address, and it's just an email
|
2019-10-06 14:26:14 +03:00
|
|
|
if (validator.isEmail(address)) {
|
|
|
|
const defaultBlogTitle = settingsCache.get('title') ? settingsCache.get('title').replace(/"/g, '\\"') : common.i18n.t('common.mail.title', {domain: getDomain()});
|
|
|
|
return `"${defaultBlogTitle}" <${address}>`;
|
2016-06-28 21:13:01 +03:00
|
|
|
}
|
|
|
|
|
2019-10-06 14:26:14 +03:00
|
|
|
return address;
|
|
|
|
}
|
2016-06-28 21:13:01 +03:00
|
|
|
|
|
|
|
// 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,
|
2019-07-22 13:17:50 +03:00
|
|
|
help = common.i18n.t('errors.api.authentication.checkEmailConfigInstructions', {url: 'https://ghost.org/docs/concepts/config/#mail'}),
|
2017-12-12 00:47:46 +03:00
|
|
|
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;
|