Added email sending endpoint to the API.

closes #2550

- Added new API module named 'mail'
- Added routes for the mail endpoint
- Added 'send a test email' button to the debug settigns page
- Added handler to this button which sends and AJAX request to the mail API endpoint
This commit is contained in:
Peter Szel 2014-04-04 03:59:09 +02:00
parent 37d13d3fbc
commit 2433116cc6
5 changed files with 90 additions and 2 deletions

View File

@ -6,7 +6,8 @@
events: {
"click .settings-menu a": "handleMenuClick",
"click #startupload": "handleUploadClick",
"click .js-delete": "handleDeleteClick"
"click .js-delete": "handleDeleteClick",
"click #sendtestmail": "handleSendTestMailClick"
},
initialize: function () {
@ -154,6 +155,35 @@
}
}
}));
}
},
handleSendTestMailClick: function (ev) {
ev.preventDefault();
$.ajax({
url: Ghost.paths.apiRoot + '/mail/test/',
type: 'POST',
headers: {
'X-CSRF-Token': $("meta[name='csrf-param']").attr('content')
},
success: function onSuccess(response) {
Ghost.notifications.addItem({
type: 'success',
message: ['Check your email for the test message: ', response.message].join(''),
status: 'passive'
});
},
error: function onError(response) {
var responseText = JSON.parse(response.responseText),
message = responseText && responseText.error ? responseText.error : 'unknown';
Ghost.notifications.addItem({
type: 'error',
message: ['A problem was encountered while sending the test email: ', message].join(''),
status: 'passive'
});
}
});
},
});
}());

View File

@ -10,6 +10,7 @@ var _ = require('lodash'),
posts = require('./posts'),
users = require('./users'),
tags = require('./tags'),
mail = require('./mail'),
requestHandler,
init;
@ -79,6 +80,7 @@ module.exports = {
notifications: notifications,
settings: settings,
db: db,
mail: mail,
requestHandler: requestHandler,
init: init
};

44
core/server/api/mail.js Normal file
View File

@ -0,0 +1,44 @@
var when = require("when"),
config = require('../config'),
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
};
// **returns:** a promise from the mailer with the number of successfully sent emails
return mailer.send(message)
.then(function (data) {
return when.resolve({ code: 200, message: data.message });
})
.otherwise(function (error) {
return when.reject({ code: 500, message: error.message });
});
},
// #### SendTest
// **takes:** nothing
sendTest: function () {
// **returns:** a promise
return mail.send({
subject: 'Test Ghost Email',
html: '<p><strong>Hello there!</strong></p>' +
'<p>Excellent! You\'ve successfully setup your email config for your Ghost blog over on ' + config().url + '</p>' +
'<p>If you hadn\'t, you wouldn\'t be reading this email, but you are, so it looks like all is well :)</p>' +
'<p>xoxo</p>' +
'<p>Team Ghost<br>' +
'<a href="https://ghost.org">https://ghost.org</a></p>'
});
}
};
module.exports = mail;

View File

@ -27,4 +27,7 @@ module.exports = function (server) {
server.get('/ghost/api/v0.1/db/', api.requestHandler(api.db.exportContent));
server.post('/ghost/api/v0.1/db/', middleware.busboy, api.requestHandler(api.db.importContent));
server.del('/ghost/api/v0.1/db/', api.requestHandler(api.db.deleteAllContent));
// #### Mail
server.post('/ghost/api/v0.1/mail', api.requestHandler(api.mail.send));
server.post('/ghost/api/v0.1/mail/test', api.requestHandler(api.mail.sendTest));
};

View File

@ -44,6 +44,15 @@
</div>
</fieldset>
</form>
<form id="settings-testmail">
<fieldset>
<div class="form-group">
<label>Send a test email</label>
<button type="submit" id="sendtestmail" class="button-save">Send</button>
<p>Sends a test email to your address.</p>
</div>
</fieldset>
</form>
</section>
</section>
</div>