0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00
ghost/core/server/api/mail.js
rfpe 7abcc43907 Harvest server side strings
closes #5617
- Replace all hard-coded server-side strings with i18n translations
2015-12-19 12:12:16 +01:00

182 lines
4.5 KiB
JavaScript

// # Mail API
// API for sending Mail
var _ = require('lodash').runInContext(),
Promise = require('bluebird'),
pipeline = require('../utils/pipeline'),
config = require('../config'),
errors = require('../errors'),
mailer = require('../mail'),
Models = require('../models'),
utils = require('./utils'),
path = require('path'),
fs = require('fs'),
templatesDir = path.resolve(__dirname, '..', 'mail', 'templates'),
htmlToText = require('html-to-text'),
readFile = Promise.promisify(fs.readFile),
docName = 'mail',
i18n = require('../i18n'),
mail;
_.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
/**
* Send mail helper
*/
function sendMail(object) {
return mailer.send(object.mail[0].message).catch(function (err) {
err = new errors.EmailError(err.message);
return Promise.reject(err);
});
}
/**
* ## Mail API Methods
*
* **See:** [API Methods](index.js.html#api%20methods)
* @typedef Mail
* @param mail
*/
mail = {
/**
* ### Send
* Send an email
*
* @public
* @param {Mail} object details of the email to send
* @returns {Promise}
*/
send: function (object, options) {
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 = [
utils.handlePermissions(docName, 'send'),
send,
formatResponse
];
return pipeline(tasks, options || {});
},
/**
* ### SendTest
* Send a test email
*
* @public
* @param {Object} options required property 'to' which contains the recipient address
* @returns {Promise}
*/
sendTest: function (options) {
var tasks;
/**
* ### Model Query
*/
function modelQuery() {
return Models.User.findOne({id: options.context.user});
}
/**
* ### Generate content
*/
function generateContent(result) {
return mail.generateContent({template: 'test'}).then(function (content) {
var payload = {
mail: [{
message: {
to: result.get('email'),
subject: i18n.t('common.api.mail.testGhostEmail'),
html: content.html,
text: content.text
}
}]
};
return payload;
});
}
/**
* ### Send mail
*/
function send(payload) {
return sendMail(payload, options);
}
tasks = [
modelQuery,
generateContent,
send
];
return pipeline(tasks);
},
/**
*
* @param {Object} options {
* data: JSON object representing the data that will go into the email
* template: which email template to load (files are stored in /core/server/mail/templates/)
* }
* @returns {*}
*/
generateContent: function (options) {
var defaults,
data;
defaults = {
siteUrl: config.forceAdminSSL ? (config.urlSSL || config.url) : config.url
};
data = _.defaults(defaults, options.data);
// read the proper email body template
return readFile(path.join(templatesDir, options.template + '.html'), 'utf8').then(function (content) {
var compiled,
htmlContent,
textContent;
// insert user-specific data into the email
compiled = _.template(content);
htmlContent = compiled(data);
// generate a plain-text version of the same email
textContent = htmlToText.fromString(htmlContent);
return {
html: htmlContent,
text: textContent
};
});
}
};
module.exports = mail;