2014-06-03 17:05:25 +04:00
|
|
|
// # Mail API
|
|
|
|
// API for sending Mail
|
2016-06-28 21:13:01 +03:00
|
|
|
|
|
|
|
var Promise = require('bluebird'),
|
2016-02-08 02:35:47 +03:00
|
|
|
pipeline = require('../utils/pipeline'),
|
|
|
|
errors = require('../errors'),
|
2016-06-28 21:13:01 +03:00
|
|
|
mail = require('../mail'),
|
2016-02-08 02:35:47 +03:00
|
|
|
Models = require('../models'),
|
|
|
|
utils = require('./utils'),
|
|
|
|
notifications = require('./notifications'),
|
|
|
|
i18n = require('../i18n'),
|
2016-10-11 15:53:52 +03:00
|
|
|
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) {
|
2016-06-11 22:25:15 +03:00
|
|
|
notifications.add(
|
|
|
|
{notifications: [{
|
|
|
|
type: 'warn',
|
|
|
|
message: [
|
|
|
|
i18n.t('warnings.index.unableToSendEmail'),
|
|
|
|
i18n.t('common.seeLinkForInstructions',
|
2017-06-15 20:04:48 +03:00
|
|
|
{link: '<a href=\'https://docs.ghost.org/v1.0.0/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
|
|
|
|
2016-10-06 15:27:35 +03:00
|
|
|
return Promise.reject(new errors.EmailError({err: 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 = [
|
2016-06-28 21:13:01 +03:00
|
|
|
utils.handlePermissions(docName, 'send'),
|
|
|
|
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() {
|
|
|
|
return Models.User.findOne({id: options.context.user});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### 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;
|