Ghost/core/server/mail/index.js

106 lines
3.3 KiB
JavaScript
Raw Normal View History

// # Mail
// Handles sending email for Ghost
var _ = require('lodash'),
Promise = require('bluebird'),
nodemailer = require('nodemailer'),
validator = require('validator'),
config = require('../config'),
i18n = require('../i18n');
2013-08-21 00:19:47 +04:00
function GhostMailer() {
var transport = config.mail && config.mail.transport || 'direct',
options = config.mail && _.clone(config.mail.options) || {};
2013-08-21 00:19:47 +04:00
this.state = {};
this.transport = nodemailer.createTransport(transport, options);
2013-08-21 00:19:47 +04:00
this.state.usingDirect = transport === 'direct';
}
2013-08-21 00:19:47 +04:00
GhostMailer.prototype.from = function () {
var from = config.mail && (config.mail.from || config.mail.fromaddress);
// 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)) {
if (!config.theme.title) {
config.theme.title = i18n.t('common.mail.title', {domain: this.getDomain()});
}
from = '"' + config.theme.title + '" <' + from + '>';
}
return from;
};
// Moved it to its own module
GhostMailer.prototype.getDomain = function () {
var domain = config.url.match(new RegExp('^https?://([^/:?#]+)(?:[/:?#]|$)', 'i'));
return domain && domain[1];
};
2015-10-16 13:05:24 +03:00
// Sends an email message enforcing `to` (blog owner) and `from` fields
2015-03-15 00:50:00 +03:00
// This assumes that api.settings.read('email') was already done on the API level
GhostMailer.prototype.send = function (message) {
var self = this,
to;
// important to clone message as we modify it
message = _.clone(message) || {};
to = message.to || false;
if (!(message && message.subject && message.html && message.to)) {
return Promise.reject(new Error(i18n.t('errors.mail.incompleteMessageData.error')));
2013-08-21 00:19:47 +04:00
}
message = _.extend(message, {
from: self.from(),
to: to,
generateTextFromHTML: true,
2014-11-16 21:14:18 +03:00
encoding: 'base64'
2013-08-21 00:19:47 +04:00
});
return new Promise(function (resolve, reject) {
self.transport.sendMail(message, function (error, response) {
if (error) {
return reject(new Error(error));
}
if (self.transport.transportType !== 'DIRECT') {
return resolve(response);
}
response.statusHandler.once('failed', function (data) {
var reason = i18n.t('errors.mail.failedSendingEmail.error');
if (data.error && data.error.errno === 'ENOTFOUND') {
reason += i18n.t('errors.mail.noMailServerAtAddress.error', {domain: data.domain});
}
reason += '.';
return reject(new Error(reason));
});
response.statusHandler.once('requeue', function (data) {
var errorMessage = i18n.t('errors.mail.messageNotSent.error');
if (data.error && data.error.message) {
errorMessage += i18n.t('errors.general.moreInfo', {info: data.error.message});
}
return reject(new Error(errorMessage));
});
response.statusHandler.once('sent', function () {
return resolve(i18n.t('notices.mail.messageSent'));
});
});
});
2013-08-21 00:19:47 +04:00
};
module.exports = GhostMailer;