2020-04-29 18:44:27 +03:00
|
|
|
const _ = require('lodash').runInContext();
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const path = require('path');
|
|
|
|
const htmlToText = require('html-to-text');
|
2020-05-28 13:57:02 +03:00
|
|
|
const urlUtils = require('../../../shared/url-utils');
|
2021-06-30 16:56:57 +03:00
|
|
|
const settingsCache = require('../../../shared/settings-cache');
|
2020-04-29 18:44:27 +03:00
|
|
|
const templatesDir = path.resolve(__dirname, '..', 'mail', 'templates');
|
2016-06-28 21:13:01 +03:00
|
|
|
|
|
|
|
_.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
|
|
|
|
|
|
|
|
exports.generateContent = function generateContent(options) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const defaults = {
|
2020-05-06 15:19:47 +03:00
|
|
|
siteUrl: urlUtils.urlFor('home', true),
|
|
|
|
siteTitle: settingsCache.get('title')
|
2016-06-28 21:13:01 +03:00
|
|
|
};
|
|
|
|
|
2020-04-29 18:44:27 +03:00
|
|
|
const data = _.defaults(defaults, options.data);
|
2016-06-28 21:13:01 +03:00
|
|
|
|
|
|
|
// 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) {
|
|
|
|
// insert user-specific data into the email
|
2020-04-29 18:44:27 +03:00
|
|
|
const compiled = _.template(content);
|
|
|
|
const htmlContent = compiled(data);
|
2016-06-28 21:13:01 +03:00
|
|
|
|
|
|
|
// generate a plain-text version of the same email
|
2020-04-29 18:44:27 +03:00
|
|
|
const textContent = htmlToText.fromString(htmlContent);
|
2016-06-28 21:13:01 +03:00
|
|
|
|
|
|
|
return {
|
|
|
|
html: htmlContent,
|
|
|
|
text: textContent
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|