Ghost/core/server/api/mail.js
Maurice Williams e30e29bf5d Implementing HTML emails
closes #3082
- no more in-line HTML strings
- adding files for "welcome", "reset password", and "invite user" emails
- added mail.generateContent() to create HTML and plain-text email content
- refactored methods that trigger emails to send both HTML and plain-text emails
2014-07-29 00:55:14 -04:00

119 lines
3.6 KiB
JavaScript

// # Mail API
// API for sending Mail
var when = require('when'),
_ = require('lodash'),
config = require('../config'),
canThis = require('../permissions').canThis,
errors = require('../errors'),
path = require('path'),
fs = require('fs'),
templatesDir = path.resolve(__dirname, '..', 'email-templates'),
htmlToText = require('html-to-text'),
mail;
/**
* ## 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 mailer = require('../mail');
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;
})
.otherwise(function (error) {
return when.reject(new errors.EmailError(error.message));
});
}, function () {
return when.reject(new errors.NoPermissionError('You do not have permission to send mail.'));
});
},
/**
* ### SendTest
* Send a test email
*
* @public
* @param {Object} required property 'to' which contains the recipient address
* @returns {Promise}
*/
sendTest: function (object, options) {
return mail.generateContent({template: 'test'}).then(function (emailContent) {
var payload = {mail: [{
message: {
to: object.to,
subject: 'Test Ghost Email',
html: emailContent.html,
text: emailContent.text
}
}]};
return mail.send(payload, options);
});
},
/**
*
* @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
return when.promise(function (resolve, reject) {
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);
resolve({ html: htmlContent,
text: textContent
});
});
});
}
};
module.exports = mail;