2014-06-03 17:05:25 +04:00
|
|
|
// # Mail API
|
|
|
|
// API for sending Mail
|
2016-06-28 21:13:01 +03:00
|
|
|
|
2017-09-12 18:31:14 +03:00
|
|
|
var Promise = require('bluebird'),
|
2017-12-14 00:20:02 +03:00
|
|
|
pipeline = require('../lib/promise/pipeline'),
|
2017-12-14 00:14:19 +03:00
|
|
|
localUtils = require('./utils'),
|
2017-09-12 18:31:14 +03:00
|
|
|
models = require('../models'),
|
2017-12-12 00:47:46 +03:00
|
|
|
common = require('../lib/common'),
|
2017-09-12 18:31:14 +03:00
|
|
|
mail = require('../mail'),
|
|
|
|
notificationsAPI = require('./notifications'),
|
|
|
|
docName = 'mail',
|
2016-02-08 02:35:47 +03:00
|
|
|
mailer,
|
2016-06-28 21:13:01 +03:00
|
|
|
apiMail;
|
2015-11-02 16:17:28 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send mail helper
|
|
|
|
*/
|
|
|
|
function sendMail(object) {
|
2017-02-03 21:25:39 +03:00
|
|
|
if (!(mailer instanceof mail.GhostMailer)) {
|
2016-06-28 21:13:01 +03:00
|
|
|
mailer = new mail.GhostMailer();
|
2016-02-08 02:35:47 +03:00
|
|
|
}
|
|
|
|
|
2015-11-02 16:17:28 +03:00
|
|
|
return mailer.send(object.mail[0].message).catch(function (err) {
|
2016-02-08 02:35:47 +03:00
|
|
|
if (mailer.state.usingDirect) {
|
2017-09-12 18:31:14 +03:00
|
|
|
notificationsAPI.add(
|
2017-12-12 00:47:46 +03:00
|
|
|
{
|
|
|
|
notifications: [{
|
|
|
|
type: 'warn',
|
|
|
|
message: [
|
|
|
|
common.i18n.t('warnings.index.unableToSendEmail'),
|
|
|
|
common.i18n.t('common.seeLinkForInstructions',
|
|
|
|
{link: '<a href=\'https://docs.ghost.org/v1/docs/mail-config\' target=\'_blank\'>Checkout our mail configuration docs!</a>'})
|
|
|
|
].join(' ')
|
|
|
|
}]
|
|
|
|
},
|
2016-06-11 22:25:15 +03:00
|
|
|
{context: {internal: true}}
|
|
|
|
);
|
2016-02-08 02:35:47 +03:00
|
|
|
}
|
2015-11-02 16:17:28 +03:00
|
|
|
|
2017-09-19 16:24:20 +03:00
|
|
|
return Promise.reject(err);
|
2015-11-02 16:17:28 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-06-03 17:05:25 +04:00
|
|
|
/**
|
|
|
|
* ## Mail API Methods
|
|
|
|
*
|
2017-12-12 00:47:46 +03:00
|
|
|
* **See:** [API Methods](index.js.html#api%20methods)
|
2014-06-03 17:05:25 +04:00
|
|
|
* @typedef Mail
|
|
|
|
* @param mail
|
|
|
|
*/
|
2016-06-28 21:13:01 +03:00
|
|
|
apiMail = {
|
2014-06-03 17:05:25 +04:00
|
|
|
/**
|
|
|
|
* ### Send
|
|
|
|
* Send an email
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
* @param {Mail} object details of the email to send
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
2014-07-17 12:48:39 +04:00
|
|
|
send: function (object, options) {
|
2015-11-02 16:17:28 +03:00
|
|
|
var tasks;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Format Response
|
|
|
|
* @returns {Mail} mail
|
|
|
|
*/
|
|
|
|
|
|
|
|
function formatResponse(data) {
|
|
|
|
delete object.mail[0].options;
|
|
|
|
// Sendmail returns extra details we don't need and that don't convert to JSON
|
|
|
|
delete object.mail[0].message.transport;
|
|
|
|
object.mail[0].status = {
|
|
|
|
message: data.message
|
|
|
|
};
|
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Send Mail
|
|
|
|
*/
|
|
|
|
|
|
|
|
function send() {
|
|
|
|
return sendMail(object, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
tasks = [
|
2017-12-14 00:14:19 +03:00
|
|
|
localUtils.handlePermissions(docName, 'send'),
|
2016-06-28 21:13:01 +03:00
|
|
|
send,
|
|
|
|
formatResponse
|
2015-11-02 16:17:28 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
return pipeline(tasks, options || {});
|
2014-04-04 05:59:09 +04:00
|
|
|
},
|
2014-06-26 22:22:52 +04:00
|
|
|
|
2014-06-03 17:05:25 +04:00
|
|
|
/**
|
|
|
|
* ### SendTest
|
|
|
|
* Send a test email
|
|
|
|
*
|
|
|
|
* @public
|
2014-09-10 08:06:24 +04:00
|
|
|
* @param {Object} options required property 'to' which contains the recipient address
|
2014-06-03 17:05:25 +04:00
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
2014-08-08 21:41:14 +04:00
|
|
|
sendTest: function (options) {
|
2015-11-02 16:17:28 +03:00
|
|
|
var tasks;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Model Query
|
|
|
|
*/
|
|
|
|
|
|
|
|
function modelQuery() {
|
2017-09-12 18:31:14 +03:00
|
|
|
return models.User.findOne({id: options.context.user});
|
2015-11-02 16:17:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Generate content
|
|
|
|
*/
|
|
|
|
|
|
|
|
function generateContent(result) {
|
2016-06-28 21:13:01 +03:00
|
|
|
return mail.utils.generateContent({template: 'test'}).then(function (content) {
|
2015-11-02 16:17:28 +03:00
|
|
|
var payload = {
|
|
|
|
mail: [{
|
|
|
|
message: {
|
|
|
|
to: result.get('email'),
|
2017-12-12 00:47:46 +03:00
|
|
|
subject: common.i18n.t('common.api.mail.testGhostEmail'),
|
2015-11-02 16:17:28 +03:00
|
|
|
html: content.html,
|
|
|
|
text: content.text
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
|
|
|
|
return payload;
|
2014-08-08 21:41:14 +04:00
|
|
|
});
|
2015-11-02 16:17:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Send mail
|
|
|
|
*/
|
|
|
|
|
|
|
|
function send(payload) {
|
|
|
|
return sendMail(payload, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
tasks = [
|
|
|
|
modelQuery,
|
|
|
|
generateContent,
|
|
|
|
send
|
|
|
|
];
|
|
|
|
|
|
|
|
return pipeline(tasks);
|
2014-04-04 05:59:09 +04:00
|
|
|
}
|
|
|
|
};
|
2014-06-26 22:22:52 +04:00
|
|
|
|
2016-06-28 21:13:01 +03:00
|
|
|
module.exports = apiMail;
|