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'),
|
|
|
|
pipeline = require('../utils/pipeline'),
|
|
|
|
apiUtils = require('./utils'),
|
|
|
|
models = require('../models'),
|
|
|
|
i18n = require('../i18n'),
|
|
|
|
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(
|
2016-06-11 22:25:15 +03:00
|
|
|
{notifications: [{
|
|
|
|
type: 'warn',
|
|
|
|
message: [
|
|
|
|
i18n.t('warnings.index.unableToSendEmail'),
|
|
|
|
i18n.t('common.seeLinkForInstructions',
|
2017-08-31 12:45:59 +03:00
|
|
|
{link: '<a href=\'https://docs.ghost.org/v1/docs/mail-config\' target=\'_blank\'>Checkout our mail configuration docs!</a>'})
|
2016-06-11 22:25:15 +03:00
|
|
|
].join(' ')
|
|
|
|
}]},
|
|
|
|
{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
|
|
|
|
*
|
|
|
|
* **See:** [API Methods](index.js.html#api%20methods)
|
|
|
|
* @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-09-12 18:31:14 +03:00
|
|
|
apiUtils.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'),
|
2015-11-12 15:29:45 +03:00
|
|
|
subject: 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;
|