2016-06-28 21:13:01 +03:00
|
|
|
var _ = require('lodash').runInContext(),
|
2017-12-13 21:15:29 +03:00
|
|
|
fs = require('fs-extra'),
|
2016-06-28 21:13:01 +03:00
|
|
|
path = require('path'),
|
|
|
|
htmlToText = require('html-to-text'),
|
2019-06-18 16:13:55 +03:00
|
|
|
urlUtils = require('../../lib/url-utils'),
|
2016-06-28 21:13:01 +03:00
|
|
|
templatesDir = path.resolve(__dirname, '..', 'mail', 'templates');
|
|
|
|
|
|
|
|
_.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
|
|
|
|
|
|
|
|
exports.generateContent = function generateContent(options) {
|
|
|
|
var defaults,
|
|
|
|
data;
|
|
|
|
|
|
|
|
defaults = {
|
2019-06-18 16:13:55 +03:00
|
|
|
siteUrl: urlUtils.urlFor('home', true)
|
2016-06-28 21:13:01 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
data = _.defaults(defaults, options.data);
|
|
|
|
|
|
|
|
// read the proper email body template
|
2017-12-13 22:03:07 +03:00
|
|
|
return fs.readFile(path.join(templatesDir, options.template + '.html'), 'utf8')
|
2016-06-28 21:13:01 +03:00
|
|
|
.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
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|