0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-18 02:21:47 -05:00

Merge pull request #2912 from javorszky/iss2836

Removed API dependency from mailer and api/mail
This commit is contained in:
Hannah Wolfe 2014-06-08 19:04:56 +02:00
commit d83f745aa0
2 changed files with 48 additions and 45 deletions

View file

@ -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.
@ -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

View file

@ -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) {
@ -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();
@ -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();
});
@ -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,
@ -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();