2022-05-04 10:08:35 +03:00
|
|
|
const _ = require('lodash').runInContext();
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const path = require('path');
|
|
|
|
const htmlToText = require('html-to-text');
|
|
|
|
|
|
|
|
_.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
|
|
|
|
|
|
|
|
class EmailContentGenerator {
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {Object} options
|
|
|
|
* @param {function} options.getSiteUrl
|
|
|
|
* @param {function} options.getSiteTitle
|
|
|
|
* @param {string} options.templatesDir - path to the directory containing email templates
|
|
|
|
*/
|
|
|
|
constructor({getSiteUrl, getSiteTitle, templatesDir}) {
|
|
|
|
this.getSiteUrl = getSiteUrl;
|
|
|
|
this.getSiteTitle = getSiteTitle;
|
|
|
|
this.templatesDir = templatesDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {Object} options
|
|
|
|
* @param {string} options.template - HTML template name to use for generation
|
|
|
|
* @param {Object} [options.data] - variable data to use during HTML template compilation
|
2022-05-05 07:17:03 +03:00
|
|
|
* @returns {Promise<{html: String, text: String}>} resolves with an object containing html and text properties
|
2022-05-04 10:08:35 +03:00
|
|
|
*/
|
|
|
|
async getContent(options) {
|
|
|
|
const defaults = {
|
|
|
|
siteUrl: this.getSiteUrl(),
|
|
|
|
siteTitle: this.getSiteTitle()
|
|
|
|
};
|
|
|
|
|
|
|
|
const data = _.defaults(defaults, options.data);
|
|
|
|
|
|
|
|
// read the proper email body template
|
2022-05-04 10:16:39 +03:00
|
|
|
const content = await fs.readFile(path.join(this.templatesDir, options.template + '.html'), 'utf8');
|
|
|
|
|
|
|
|
// insert user-specific data into the email
|
|
|
|
const compiled = _.template(content);
|
|
|
|
const htmlContent = compiled(data);
|
|
|
|
|
|
|
|
// generate a plain-text version of the same email
|
|
|
|
const textContent = htmlToText.fromString(htmlContent);
|
|
|
|
|
|
|
|
return {
|
|
|
|
html: htmlContent,
|
|
|
|
text: textContent
|
|
|
|
};
|
2022-05-04 10:08:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = EmailContentGenerator;
|