Skip to content

Commit

Permalink
Removed API dependency from mailer and api/mail
Browse files Browse the repository at this point in the history
Fixes #2836.

* Mailer now only handles sending email and initializing settings. Instead of adding new notifications there, it just sets flags on its own object.
* Mailer now checks for the presence of "to". If there is none, it fails. You should really pass a full mail object complete with "to", "subject" and "html", otherwise it's partial content.
* Therefore Mail API doesn't check for the existence of the "to", and doesn't get the email from settings and substitute that
* index.js now has a method that adds the notifications. I figured adding those THERE is probably better than individually in every module. It is, as the comments say, can be made extensible
  • Loading branch information
javorszky committed Jun 8, 2014
1 parent fc63166 commit 5229999
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 45 deletions.
30 changes: 30 additions & 0 deletions core/server/index.js
Expand Up @@ -162,6 +162,34 @@ function ghostStartMessages() {
}
}


// This is run after every initialization is done, right before starting server.
// Its main purpose is to move adding notifications here, so none of the submodules
// should need to include api, which previously resulted in circular dependencies.
// This is also a "one central repository" of adding startup notifications in case
// in the future apps will want to hook into here
function initNotifications() {
if (mailer.state && mailer.state.usingSendmail) {
api.notifications.add({
type: 'info',
message: [
"Ghost is attempting to use your server's <b>sendmail</b> to send e-mail.",
"It is recommended that you explicitly configure an e-mail service,",
"See <a href=\"http://docs.ghost.org/mail\">http://docs.ghost.org/mail</a> for instructions"
].join(' ')
});
}
if (mailer.state && mailer.state.emailDisabled) {
api.notifications.add({
type: 'warn',
message: [
"Ghost is currently unable to send e-mail.",
"See <a href=\"http://docs.ghost.org/mail\">http://docs.ghost.org/mail</a> for instructions"
].join(' ')
});
}
}

// ## Initializes the ghost application.
// Sets up the express server instance.
// Instantiates the ghost singleton, helpers, routes, middleware, and apps.
Expand Down Expand Up @@ -215,6 +243,8 @@ function init(server) {
var adminHbs = hbs.create(),
deferred = when.defer();

// Output necessary notifications on init
initNotifications();
// ##Configuration

// return the correct mime type for woff filess
Expand Down
63 changes: 18 additions & 45 deletions core/server/mail.js
Expand Up @@ -3,7 +3,6 @@ var cp = require('child_process'),
when = require('when'),
nodefn = require('when/node/function'),
nodemailer = require('nodemailer'),
api = require('./api'),
config = require('./config');

function GhostMailer(opts) {
Expand All @@ -15,6 +14,7 @@ function GhostMailer(opts) {
// *This promise should always resolve to avoid halting Ghost::init*.
GhostMailer.prototype.init = function () {
var self = this;
self.state = {};
if (config().mail && config().mail.transport) {
this.createTransport();
return when.resolve();
Expand All @@ -25,9 +25,10 @@ GhostMailer.prototype.init = function () {
self.transport = nodemailer.createTransport('sendmail', {
path: binpath
});
self.usingSendmail();
self.state.usingSendmail = true;
}, function () {
self.emailDisabled();
self.state.emailDisabled = true;
self.transport = null;
}).ensure(function () {
return when.resolve();
});
Expand Down Expand Up @@ -55,27 +56,6 @@ GhostMailer.prototype.createTransport = function () {
this.transport = nodemailer.createTransport(config().mail.transport, _.clone(config().mail.options) || {});
};

GhostMailer.prototype.usingSendmail = function () {
api.notifications.add({
type: 'info',
message: [
"Ghost is attempting to use your server's <b>sendmail</b> to send e-mail.",
"It is recommended that you explicitly configure an e-mail service,",
"See <a href=\"http://docs.ghost.org/mail\">http://docs.ghost.org/mail</a> for instructions"
].join(' ')
});
};

GhostMailer.prototype.emailDisabled = function () {
api.notifications.add({
type: 'warn',
message: [
"Ghost is currently unable to send e-mail.",
"See <a href=\"http://docs.ghost.org/mail\">http://docs.ghost.org/mail</a> for instructions"
].join(' ')
});
this.transport = null;
};

GhostMailer.prototype.fromAddress = function () {
var from = config().mail && config().mail.fromaddress,
Expand All @@ -94,36 +74,29 @@ GhostMailer.prototype.fromAddress = function () {
};

// Sends an e-mail message enforcing `to` (blog owner) and `from` fields
// GhostMailer.prototype.send = function (message) {
GhostMailer.prototype.send = function (payload) {
// This assumes that api.settings.read('email') was aready done on the API level
GhostMailer.prototype.send = function (message) {
var self = this,
message = payload;
to,
sendMail;

message = message || {};
to = message.to || false;

if (!this.transport) {
return when.reject(new Error('Email Error: No e-mail transport configured.'));
}
if (!(message && message.subject && message.html)) {
if (!(message && message.subject && message.html && message.to)) {
return when.reject(new Error('Email Error: Incomplete message data.'));
}
sendMail = nodefn.lift(self.transport.sendMail.bind(self.transport));

return api.settings.read('email').then(function (response) {

var email = response.settings[0],
to = message.to || email.value;

message = _.extend(message, {
from: self.fromAddress(),
to: to,
generateTextFromHTML: true
});
}).then(function () {
var sendMail = nodefn.lift(self.transport.sendMail.bind(self.transport));
return sendMail(message);
}).otherwise(function (error) {
// Proxy the error message so we can add 'Email Error:' to the beginning to make it clearer.
error = _.isString(error) ? 'Email Error:' + error : (_.isObject(error) ? 'Email Error: ' + error.message : 'Email Error: Unknown Email Error');
return when.reject(new Error(error));
message = _.extend(message, {
from: self.fromAddress(),
to: to,
generateTextFromHTML: true
});
return sendMail(message);
};

module.exports = new GhostMailer();

0 comments on commit 5229999

Please sign in to comment.