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"),
config = require('../config'),
errors = require('../errors'),
errors = require('../errors'),
mail;
// ## Mail
mail = {
// #### Send
// **takes:** a json object representing an email.
send: function (postData) {
var mailer = require('../mail'),
message = {
to: postData.to,
subject: postData.subject,
html: postData.html
};
var mailer = require('../mail');
// **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) {
return when.resolve({message: data.message });
delete postData.mail[0].options;
postData.mail[0].status = {
message: data.message
};
return postData;
})
.otherwise(function (error) {
return when.reject(new errors.EmailError(error.message));
});
},
// #### SendTest
// **takes:** nothing
sendTest: function () {
// **returns:** a promise
return mail.send({
subject: 'Test Ghost Email',
html: '<p><strong>Hello there!</strong></p>' +

View file

@ -3,11 +3,9 @@ var config = require('../config'),
path = require('path'),
when = require('when'),
api = require('../api'),
mailer = require('../mail'),
errors = require('../errors'),
storage = require('../storage'),
updateCheck = require('../update-check'),
adminNavbar,
adminControllers,
loginSecurity = [];
@ -265,19 +263,26 @@ adminControllers = {
api.users.register({users: users}).then(function (user) {
api.settings.edit.call({user: 1}, 'email', email).then(function () {
var message = {
to: email,
subject: 'Your New Ghost Blog',
html: '<p><strong>Hello!</strong></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> Email Address: ' + email + '<br>' +
'Password: The password you chose when you signed up</p>' +
'<p>Keep this email somewhere safe for future reference, and have fun!</p>' +
'<p>xoxo</p>' +
'<p>Team Ghost<br>' +
'<a href="https://ghost.org">https://ghost.org</a></p>'
};
mailer.send(message).otherwise(function (error) {
to: email,
subject: 'Your New Ghost Blog',
html: '<p><strong>Hello!</strong></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> Email Address: ' + email + '<br>' +
'Password: The password you chose when you signed up</p>' +
'<p>Keep this email somewhere safe for future reference, and have fun!</p>' +
'<p>xoxo</p>' +
'<p>Team Ghost<br>' +
'<a href="https://ghost.org">https://ghost.org</a></p>'
},
payload = {
mail: [{
message: message,
options: {}
}]
};
api.mail.send(payload).otherwise(function (error) {
errors.logError(
error.message,
"Unable to send welcome email, your blog will continue to function.",
@ -320,17 +325,24 @@ adminControllers = {
siteLink = '<a href="' + baseUrl + '">' + baseUrl + '</a>',
resetUrl = baseUrl.replace(/\/$/, '') + '/ghost/reset/' + token + '/',
resetLink = '<a href="' + resetUrl + '">' + resetUrl + '</a>',
message = {
to: email,
subject: 'Reset Password',
html: '<p><strong>Hello!</strong></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>'
payload = {
mail: [{
message: {
to: email,
subject: 'Reset Password',
html: '<p><strong>Hello!</strong></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() {
// TODO: note that this function takes a response as an
// argument but jshint complains of it not being used
var notification = {
type: 'success',
message: 'Check your email for further instructions',

View file

@ -12,6 +12,7 @@ var _ = require('lodash'),
RequestEntityTooLargeError = require('./requesttoolargeerror'),
UnauthorizedError = require('./unauthorizederror'),
ValidationError = require('./validationerror'),
EmailError = require('./emailerror'),
errors,
// Paths for views
@ -257,3 +258,4 @@ module.exports.NoPermissionError = NoPermissionError;
module.exports.UnauthorizedError = UnauthorizedError;
module.exports.ValidationError = ValidationError;
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
GhostMailer.prototype.send = function (message) {
var self = this;
// GhostMailer.prototype.send = function (message) {
GhostMailer.prototype.send = function (payload) {
var self = this,
message = payload;
if (!this.transport) {
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();
});
});
});