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');
|
2020-05-27 20:47:53 +03:00
|
|
|
const config = require('../../../shared/config');
|
2020-04-30 22:26:12 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
|
|
|
const {i18n} = require('../../lib/common');
|
2019-10-06 14:22:56 +03:00
|
|
|
const settingsCache = require('../settings/cache');
|
2020-05-28 13:57:02 +03:00
|
|
|
const urlUtils = require('../../../shared/url-utils');
|
2016-06-28 21:13:01 +03:00
|
|
|
|
2020-04-30 22:26:12 +03:00
|
|
|
const helpMessage = i18n.t('errors.api.authentication.checkEmailConfigInstructions', {url: 'https://ghost.org/docs/concepts/config/#mail'});
|
|
|
|
const defaultErrorMessage = i18n.t('errors.mail.failedSendingEmail.error');
|
2019-10-06 14:28:36 +03:00
|
|
|
|
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:32:41 +03:00
|
|
|
function getFromAddress(requestedFromAddress) {
|
2019-10-06 14:26:14 +03:00
|
|
|
const configAddress = config.get('mail') && config.get('mail').from;
|
2016-06-28 21:13:01 +03:00
|
|
|
|
2019-10-06 14:32:41 +03:00
|
|
|
const address = requestedFromAddress || 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
|
2020-01-13 10:28:53 +03:00
|
|
|
if (validator.isEmail(address, {require_tld: false})) {
|
2020-04-30 22:26:12 +03:00
|
|
|
const defaultBlogTitle = settingsCache.get('title') ? settingsCache.get('title').replace(/"/g, '\\"') : i18n.t('common.mail.title', {domain: getDomain()});
|
2019-10-06 14:26:14 +03:00
|
|
|
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
|
|
|
|
2019-10-06 14:28:36 +03:00
|
|
|
function createMessage(message) {
|
2019-10-10 16:09:23 +03:00
|
|
|
const encoding = 'base64';
|
|
|
|
const generateTextFromHTML = !message.forceTextContent;
|
2019-10-06 14:28:36 +03:00
|
|
|
return Object.assign({}, message, {
|
2019-10-11 07:21:53 +03:00
|
|
|
from: getFromAddress(message.from),
|
2019-10-10 16:09:23 +03:00
|
|
|
generateTextFromHTML,
|
|
|
|
encoding
|
2016-06-28 21:13:01 +03:00
|
|
|
});
|
2019-10-06 14:28:36 +03:00
|
|
|
}
|
2016-06-28 21:13:01 +03:00
|
|
|
|
2019-10-06 14:28:36 +03:00
|
|
|
function createMailError({message, err, ignoreDefaultMessage} = {message: ''}) {
|
|
|
|
const fullErrorMessage = defaultErrorMessage + message;
|
2020-07-20 22:38:19 +03:00
|
|
|
let statusCode = (err && err.name === 'RecipientError') ? 400 : 500;
|
2020-04-30 22:26:12 +03:00
|
|
|
return new errors.EmailError({
|
2019-10-06 14:28:36 +03:00
|
|
|
message: ignoreDefaultMessage ? message : fullErrorMessage,
|
|
|
|
err: err,
|
2020-07-20 22:35:06 +03:00
|
|
|
statusCode,
|
2019-10-06 14:28:36 +03:00
|
|
|
help: helpMessage
|
|
|
|
});
|
|
|
|
}
|
2017-09-19 16:24:20 +03:00
|
|
|
|
2019-10-06 14:28:36 +03:00
|
|
|
module.exports = class GhostMailer {
|
|
|
|
constructor() {
|
|
|
|
const nodemailer = require('nodemailer');
|
|
|
|
const transport = config.get('mail') && config.get('mail').transport || 'direct';
|
|
|
|
// nodemailer mutates the options passed to createTransport
|
|
|
|
const options = config.get('mail') && _.clone(config.get('mail').options) || {};
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
usingDirect: transport === 'direct'
|
|
|
|
};
|
|
|
|
this.transport = nodemailer.createTransport(transport, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
send(message) {
|
|
|
|
if (!(message && message.subject && message.html && message.to)) {
|
|
|
|
return Promise.reject(createMailError({
|
2020-04-30 22:26:12 +03:00
|
|
|
message: i18n.t('errors.mail.incompleteMessageData.error'),
|
2019-10-06 14:28:36 +03:00
|
|
|
ignoreDefaultMessage: true
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
const messageToSend = createMessage(message);
|
2016-06-28 21:13:01 +03:00
|
|
|
|
2019-10-06 14:28:36 +03:00
|
|
|
return this.sendMail(messageToSend).then((response) => {
|
|
|
|
if (this.transport.transportType === 'DIRECT') {
|
|
|
|
return this.handleDirectTransportResponse(response);
|
2016-06-28 21:13:01 +03:00
|
|
|
}
|
2019-10-06 14:28:36 +03:00
|
|
|
return response;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
sendMail(message) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.transport.sendMail(message, (err, response) => {
|
|
|
|
if (err) {
|
|
|
|
reject(createMailError({
|
2020-04-30 22:26:12 +03:00
|
|
|
message: i18n.t('errors.mail.reason', {reason: err.message || err}),
|
2019-10-06 14:28:36 +03:00
|
|
|
err
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
resolve(response);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2016-06-28 21:13:01 +03:00
|
|
|
|
2019-10-06 14:28:36 +03:00
|
|
|
handleDirectTransportResponse(response) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2016-06-28 21:13:01 +03:00
|
|
|
response.statusHandler.once('failed', function (data) {
|
|
|
|
if (data.error && data.error.errno === 'ENOTFOUND') {
|
2019-10-06 14:28:36 +03:00
|
|
|
reject(createMailError({
|
2020-04-30 22:26:12 +03:00
|
|
|
message: i18n.t('errors.mail.noMailServerAtAddress.error', {domain: data.domain})
|
2019-10-06 14:28:36 +03:00
|
|
|
}));
|
2016-06-28 21:13:01 +03:00
|
|
|
}
|
2017-09-19 16:24:20 +03:00
|
|
|
|
2019-10-06 14:28:36 +03:00
|
|
|
reject(createMailError());
|
2016-06-28 21:13:01 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
response.statusHandler.once('requeue', function (data) {
|
|
|
|
if (data.error && data.error.message) {
|
2019-10-06 14:28:36 +03:00
|
|
|
reject(createMailError({
|
2020-04-30 22:26:12 +03:00
|
|
|
message: i18n.t('errors.mail.reason', {reason: data.error.message})
|
2019-10-06 14:28:36 +03:00
|
|
|
}));
|
2016-06-28 21:13:01 +03:00
|
|
|
}
|
|
|
|
|
2019-10-06 14:28:36 +03:00
|
|
|
reject(createMailError());
|
2016-06-28 21:13:01 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
response.statusHandler.once('sent', function () {
|
2020-04-30 22:26:12 +03:00
|
|
|
resolve(i18n.t('notices.mail.messageSent'));
|
2016-06-28 21:13:01 +03:00
|
|
|
});
|
|
|
|
});
|
2019-10-06 14:28:36 +03:00
|
|
|
}
|
2016-06-28 21:13:01 +03:00
|
|
|
};
|