0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00

Merge pull request #2715 from javorszky/iss2650

Move mail api to json/api format
This commit is contained in:
Hannah Wolfe 2014-05-13 23:27:22 +01:00
commit cdb98241cf
5 changed files with 103 additions and 38 deletions

View file

@ -1,36 +1,35 @@
var when = require("when"), var when = require("when"),
config = require('../config'), config = require('../config'),
errors = require('../errors'), errors = require('../errors'),
mail; mail;
// ## Mail // ## Mail
mail = { mail = {
// #### Send // #### Send
// **takes:** a json object representing an email. // **takes:** a json object representing an email.
send: function (postData) { send: function (postData) {
var mailer = require('../mail'), var mailer = require('../mail');
message = {
to: postData.to,
subject: postData.subject,
html: postData.html
};
// **returns:** a promise from the mailer with the number of successfully sent emails // **returns:** a promise from the mailer with the number of successfully sent emails
return mailer.send(message) return mailer.send(postData.mail[0].message)
.then(function (data) { .then(function (data) {
return when.resolve({message: data.message }); delete postData.mail[0].options;
postData.mail[0].status = {
message: data.message
};
return postData;
}) })
.otherwise(function (error) { .otherwise(function (error) {
return when.reject(new errors.EmailError(error.message)); return when.reject(new errors.EmailError(error.message));
}); });
}, },
// #### SendTest // #### SendTest
// **takes:** nothing // **takes:** nothing
sendTest: function () { sendTest: function () {
// **returns:** a promise // **returns:** a promise
return mail.send({ return mail.send({
subject: 'Test Ghost Email', subject: 'Test Ghost Email',
html: '<p><strong>Hello there!</strong></p>' + html: '<p><strong>Hello there!</strong></p>' +

View file

@ -3,11 +3,9 @@ var config = require('../config'),
path = require('path'), path = require('path'),
when = require('when'), when = require('when'),
api = require('../api'), api = require('../api'),
mailer = require('../mail'),
errors = require('../errors'), errors = require('../errors'),
storage = require('../storage'), storage = require('../storage'),
updateCheck = require('../update-check'), updateCheck = require('../update-check'),
adminNavbar, adminNavbar,
adminControllers, adminControllers,
loginSecurity = []; loginSecurity = [];
@ -265,19 +263,26 @@ adminControllers = {
api.users.register({users: users}).then(function (user) { api.users.register({users: users}).then(function (user) {
api.settings.edit.call({user: 1}, 'email', email).then(function () { api.settings.edit.call({user: 1}, 'email', email).then(function () {
var message = { var message = {
to: email, to: email,
subject: 'Your New Ghost Blog', subject: 'Your New Ghost Blog',
html: '<p><strong>Hello!</strong></p>' + html: '<p><strong>Hello!</strong></p>' +
'<p>Good news! You\'ve successfully created a brand new Ghost blog over on ' + config().url + '</p>' + '<p>Good news! You\'ve successfully created a brand new Ghost blog over on ' + config().url + '</p>' +
'<p>You can log in to your admin account with the following details:</p>' + '<p>You can log in to your admin account with the following details:</p>' +
'<p> Email Address: ' + email + '<br>' + '<p> Email Address: ' + email + '<br>' +
'Password: The password you chose when you signed up</p>' + 'Password: The password you chose when you signed up</p>' +
'<p>Keep this email somewhere safe for future reference, and have fun!</p>' + '<p>Keep this email somewhere safe for future reference, and have fun!</p>' +
'<p>xoxo</p>' + '<p>xoxo</p>' +
'<p>Team Ghost<br>' + '<p>Team Ghost<br>' +
'<a href="https://ghost.org">https://ghost.org</a></p>' '<a href="https://ghost.org">https://ghost.org</a></p>'
}; },
mailer.send(message).otherwise(function (error) { payload = {
mail: [{
message: message,
options: {}
}]
};
api.mail.send(payload).otherwise(function (error) {
errors.logError( errors.logError(
error.message, error.message,
"Unable to send welcome email, your blog will continue to function.", "Unable to send welcome email, your blog will continue to function.",
@ -320,17 +325,24 @@ adminControllers = {
siteLink = '<a href="' + baseUrl + '">' + baseUrl + '</a>', siteLink = '<a href="' + baseUrl + '">' + baseUrl + '</a>',
resetUrl = baseUrl.replace(/\/$/, '') + '/ghost/reset/' + token + '/', resetUrl = baseUrl.replace(/\/$/, '') + '/ghost/reset/' + token + '/',
resetLink = '<a href="' + resetUrl + '">' + resetUrl + '</a>', resetLink = '<a href="' + resetUrl + '">' + resetUrl + '</a>',
message = { payload = {
to: email, mail: [{
subject: 'Reset Password', message: {
html: '<p><strong>Hello!</strong></p>' + to: email,
'<p>A request has been made to reset the password on the site ' + siteLink + '.</p>' + subject: 'Reset Password',
'<p>Please follow the link below to reset your password:<br><br>' + resetLink + '</p>' + html: '<p><strong>Hello!</strong></p>' +
'<p>Ghost</p>' '<p>A request has been made to reset the password on the site ' + siteLink + '.</p>' +
'<p>Please follow the link below to reset your password:<br><br>' + resetLink + '</p>' +
'<p>Ghost</p>'
},
options: {}
}]
}; };
return mailer.send(message); return api.mail.send(payload);
}).then(function success() { }).then(function success() {
// TODO: note that this function takes a response as an
// argument but jshint complains of it not being used
var notification = { var notification = {
type: 'success', type: 'success',
message: 'Check your email for further instructions', message: 'Check your email for further instructions',

View file

@ -12,6 +12,7 @@ var _ = require('lodash'),
RequestEntityTooLargeError = require('./requesttoolargeerror'), RequestEntityTooLargeError = require('./requesttoolargeerror'),
UnauthorizedError = require('./unauthorizederror'), UnauthorizedError = require('./unauthorizederror'),
ValidationError = require('./validationerror'), ValidationError = require('./validationerror'),
EmailError = require('./emailerror'),
errors, errors,
// Paths for views // Paths for views
@ -257,3 +258,4 @@ module.exports.NoPermissionError = NoPermissionError;
module.exports.UnauthorizedError = UnauthorizedError; module.exports.UnauthorizedError = UnauthorizedError;
module.exports.ValidationError = ValidationError; module.exports.ValidationError = ValidationError;
module.exports.RequestEntityTooLargeError = RequestEntityTooLargeError; module.exports.RequestEntityTooLargeError = RequestEntityTooLargeError;
module.exports.EmailError = EmailError;

View file

@ -94,8 +94,10 @@ GhostMailer.prototype.fromAddress = function () {
}; };
// Sends an e-mail message enforcing `to` (blog owner) and `from` fields // Sends an e-mail message enforcing `to` (blog owner) and `from` fields
GhostMailer.prototype.send = function (message) { // GhostMailer.prototype.send = function (message) {
var self = this; GhostMailer.prototype.send = function (payload) {
var self = this,
message = payload;
if (!this.transport) { if (!this.transport) {
return when.reject(new Error('Email Error: No e-mail transport configured.')); return when.reject(new Error('Email Error: No e-mail transport configured.'));

View file

@ -0,0 +1,50 @@
/*globals describe, before, beforeEach, afterEach, it */
var testUtils = require('../../utils'),
should = require('should'),
// Stuff we are testing
MailAPI = require('../../../server/api/mail');
describe('Mail API', function () {
var mailData = {
mail: [{
message: {
to: 'joe@example.com',
subject: 'testemail',
html: '<p>This</p>'
},
options: {}
}]
};
before(function (done) {
testUtils.clearData()
.then(function () {
return testUtils.initData();
})
.then(function () {
return testUtils.insertDefaultFixtures();
})
.then(function () {
done();
}).catch(done);
});
afterEach(function (done) {
testUtils.clearData().then(function () {
done();
}).catch(done);
});
it('return correct failure message', function (done) {
MailAPI.send(mailData).then(function (response) {
done();
}).catch(function (error) {
error.type.should.eql('EmailError');
done();
});
});
});