2014-06-03 17:05:25 +04:00
|
|
|
// # Mail API
|
|
|
|
// API for sending Mail
|
2014-07-29 17:35:48 +04:00
|
|
|
var _ = require('lodash'),
|
2014-08-17 10:17:23 +04:00
|
|
|
Promise = require('bluebird'),
|
2014-07-29 17:35:48 +04:00
|
|
|
config = require('../config'),
|
|
|
|
canThis = require('../permissions').canThis,
|
|
|
|
errors = require('../errors'),
|
2014-08-14 01:58:12 +04:00
|
|
|
Models = require('../models'),
|
2014-07-29 17:35:48 +04:00
|
|
|
path = require('path'),
|
|
|
|
fs = require('fs'),
|
|
|
|
templatesDir = path.resolve(__dirname, '..', 'email-templates'),
|
|
|
|
htmlToText = require('html-to-text'),
|
2014-04-04 05:59:09 +04:00
|
|
|
mail;
|
2014-05-09 03:12:18 +04:00
|
|
|
|
2014-06-03 17:05:25 +04:00
|
|
|
/**
|
|
|
|
* ## Mail API Methods
|
|
|
|
*
|
|
|
|
* **See:** [API Methods](index.js.html#api%20methods)
|
|
|
|
* @typedef Mail
|
|
|
|
* @param mail
|
|
|
|
*/
|
2014-04-04 05:59:09 +04:00
|
|
|
mail = {
|
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) {
|
2014-05-09 03:12:18 +04:00
|
|
|
var mailer = require('../mail');
|
|
|
|
|
2014-07-17 12:48:39 +04:00
|
|
|
return canThis(options.context).send.mail().then(function () {
|
|
|
|
return mailer.send(object.mail[0].message)
|
|
|
|
.then(function (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;
|
|
|
|
})
|
2014-08-17 10:17:23 +04:00
|
|
|
.catch(function (error) {
|
|
|
|
return Promise.reject(new errors.EmailError(error.message));
|
2014-07-17 12:48:39 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
}, function () {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.reject(new errors.NoPermissionError('You do not have permission to send mail.'));
|
2014-07-17 12:48:39 +04:00
|
|
|
});
|
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-06-26 22:22:52 +04:00
|
|
|
* @param {Object} 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) {
|
2014-08-14 01:58:12 +04:00
|
|
|
return Models.User.findOne({id: options.context.user}).then(function (result) {
|
2014-08-08 21:41:14 +04:00
|
|
|
return mail.generateContent({template: 'test'}).then(function (emailContent) {
|
|
|
|
var payload = {mail: [{
|
|
|
|
message: {
|
|
|
|
to: result.get('email'),
|
|
|
|
subject: 'Test Ghost Email',
|
|
|
|
html: emailContent.html,
|
|
|
|
text: emailContent.text
|
|
|
|
}
|
|
|
|
}]};
|
|
|
|
return mail.send(payload, options);
|
|
|
|
});
|
|
|
|
}, function () {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.reject(new errors.NotFoundError('Could not find the current user'));
|
2014-07-23 05:22:13 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {
|
|
|
|
* data: JSON object representing the data that will go into the email
|
|
|
|
* template: which email template to load (files are stored in /core/server/email-templates/)
|
|
|
|
* }
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
generateContent: function (options) {
|
|
|
|
|
|
|
|
var defaultData = {
|
|
|
|
siteUrl: config.forceAdminSSL ? (config.urlSSL || config.url) : config.url
|
|
|
|
},
|
|
|
|
emailData = _.defaults(defaultData, options.data);
|
|
|
|
|
|
|
|
_.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
|
|
|
|
|
|
|
|
//read the proper email body template
|
2014-08-17 10:17:23 +04:00
|
|
|
return new Promise(function (resolve, reject) {
|
2014-07-23 05:22:13 +04:00
|
|
|
fs.readFile(templatesDir + '/' + options.template + '.html', {encoding: 'utf8'}, function (err, fileContent) {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
//insert user-specific data into the email
|
|
|
|
var htmlContent = _.template(fileContent, emailData),
|
|
|
|
textContent;
|
|
|
|
|
|
|
|
//generate a plain-text version of the same email
|
|
|
|
textContent = htmlToText.fromString(htmlContent);
|
|
|
|
|
2014-07-29 17:35:48 +04:00
|
|
|
resolve({
|
|
|
|
html: htmlContent,
|
|
|
|
text: textContent
|
|
|
|
});
|
2014-07-23 05:22:13 +04:00
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-04-04 05:59:09 +04:00
|
|
|
}
|
|
|
|
};
|
2014-06-26 22:22:52 +04:00
|
|
|
|
2014-07-23 05:22:13 +04:00
|
|
|
module.exports = mail;
|