2014-06-03 17:05:25 +04:00
|
|
|
// # Mail API
|
|
|
|
// API for sending Mail
|
2016-06-28 21:13:01 +03:00
|
|
|
|
2018-09-10 15:30:48 +03:00
|
|
|
const Promise = require('bluebird'),
|
2018-09-27 17:06:57 +03:00
|
|
|
pipeline = require('../../lib/promise/pipeline'),
|
2017-12-14 00:14:19 +03:00
|
|
|
localUtils = require('./utils'),
|
2018-09-27 17:06:57 +03:00
|
|
|
models = require('../../models'),
|
|
|
|
common = require('../../lib/common'),
|
|
|
|
mail = require('../../services/mail'),
|
2017-09-12 18:31:14 +03:00
|
|
|
notificationsAPI = require('./notifications'),
|
2018-09-10 15:30:48 +03:00
|
|
|
docName = 'mail';
|
|
|
|
|
|
|
|
let mailer;
|
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
|
|
|
}
|
|
|
|
|
2018-09-10 15:30:48 +03:00
|
|
|
return mailer.send(object.mail[0].message).catch((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'),
|
2019-01-17 09:57:37 +03:00
|
|
|
common.i18n.t('common.seeLinkForInstructions', {link: 'https://docs.ghost.org/mail/'})
|
2017-12-12 00:47:46 +03:00
|
|
|
].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-14 16:13:40 +03:00
|
|
|
* **See:** [API Methods](constants.js.html#api%20methods)
|
2014-06-03 17:05:25 +04:00
|
|
|
* @typedef Mail
|
|
|
|
* @param mail
|
|
|
|
*/
|
2018-09-10 15:30:48 +03:00
|
|
|
const 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}
|
|
|
|
*/
|
2018-09-18 16:59:56 +03:00
|
|
|
send(object, options) {
|
2018-09-10 15:30:48 +03:00
|
|
|
let tasks;
|
2015-11-02 16:17:28 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ### 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}
|
|
|
|
*/
|
2018-09-18 16:59:56 +03:00
|
|
|
sendTest(options) {
|
2018-09-10 15:30:48 +03:00
|
|
|
let tasks;
|
2015-11-02 16:17:28 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ### 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) {
|
2018-09-10 15:30:48 +03:00
|
|
|
return mail.utils.generateContent({template: 'test'}).then((content) => {
|
|
|
|
const payload = {
|
2015-11-02 16:17:28 +03:00
|
|
|
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;
|