2019-10-29 06:46:44 +00:00
|
|
|
const path = require('path');
|
2019-01-22 08:55:35 +00:00
|
|
|
const smtp = require('./smtp');
|
2019-10-29 06:46:44 +00:00
|
|
|
const Component = require('./component');
|
|
|
|
const Report = require('./report');
|
2019-01-22 08:55:35 +00:00
|
|
|
|
|
|
|
if (!process.env.OPENSSL_CONF)
|
|
|
|
process.env.OPENSSL_CONF = '/etc/ssl/';
|
|
|
|
|
2019-10-29 06:46:44 +00:00
|
|
|
class Email extends Component {
|
|
|
|
constructor(name, args) {
|
|
|
|
super(name);
|
|
|
|
|
|
|
|
this.args = args;
|
|
|
|
}
|
|
|
|
|
|
|
|
get path() {
|
|
|
|
return `../templates/email/${this.name}`;
|
|
|
|
}
|
|
|
|
|
2019-10-31 11:43:04 +00:00
|
|
|
async getSubject() {
|
2022-01-05 13:48:21 +00:00
|
|
|
return (await this.getUserLocale())['subject'];
|
2019-10-31 11:43:04 +00:00
|
|
|
}
|
|
|
|
|
2021-10-01 10:41:31 +00:00
|
|
|
/**
|
|
|
|
* @param {Object} [options] - Additional options
|
|
|
|
* @param {Boolean} [options.overrideAttachments] - Overrides default PDF attachments
|
|
|
|
* @param {Array} [options.attachments] - Array containing attachment objects
|
|
|
|
* @return {Promise} SMTP Promise
|
|
|
|
*/
|
|
|
|
async send(options = {}) {
|
2019-10-29 06:46:44 +00:00
|
|
|
const instance = this.build();
|
|
|
|
const rendered = await this.render();
|
|
|
|
const attachments = [];
|
|
|
|
const getAttachments = async(componentPath, files) => {
|
|
|
|
for (file of files) {
|
|
|
|
const fileCopy = Object.assign({}, file);
|
2021-10-01 10:41:31 +00:00
|
|
|
const fileName = fileCopy.filename;
|
|
|
|
|
|
|
|
if (options.overrideAttachments && !fileName.includes('.png')) continue;
|
|
|
|
|
2019-10-29 06:46:44 +00:00
|
|
|
if (fileCopy.cid) {
|
|
|
|
const templatePath = `${componentPath}/${file.path}`;
|
|
|
|
const fullFilePath = path.resolve(__dirname, templatePath);
|
|
|
|
|
|
|
|
fileCopy.path = path.resolve(__dirname, fullFilePath);
|
|
|
|
} else {
|
2021-10-01 10:41:31 +00:00
|
|
|
const reportName = fileName.replace('.pdf', '');
|
2019-10-29 06:46:44 +00:00
|
|
|
const report = new Report(reportName, this.args);
|
|
|
|
fileCopy.content = await report.toPdfStream();
|
2022-01-05 13:48:21 +00:00
|
|
|
fileCopy.filename = await report.getFileName();
|
2019-10-29 06:46:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
attachments.push(fileCopy);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (instance.components) {
|
|
|
|
const components = instance.components;
|
|
|
|
for (let componentName in components) {
|
|
|
|
const component = components[componentName];
|
2019-10-31 11:43:04 +00:00
|
|
|
const componentPath = `./components/${componentName}`;
|
2019-10-29 06:46:44 +00:00
|
|
|
await getAttachments(componentPath, component.attachments);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.attachments)
|
|
|
|
await getAttachments(this.path, this.attachments);
|
|
|
|
|
2021-10-01 10:41:31 +00:00
|
|
|
if (options.attachments) {
|
|
|
|
for (let attachment of options.attachments)
|
|
|
|
attachments.push(attachment);
|
|
|
|
}
|
|
|
|
|
2019-10-31 11:43:04 +00:00
|
|
|
const localeSubject = await this.getSubject();
|
2021-10-01 10:41:31 +00:00
|
|
|
const mailOptions = {
|
2019-10-29 06:46:44 +00:00
|
|
|
to: this.args.recipient,
|
2022-09-19 10:59:32 +00:00
|
|
|
replyTo: this.args.replyTo || '',
|
2019-10-31 11:43:04 +00:00
|
|
|
subject: localeSubject,
|
2019-10-29 06:46:44 +00:00
|
|
|
html: rendered,
|
|
|
|
attachments: attachments
|
|
|
|
};
|
|
|
|
|
2021-10-01 10:41:31 +00:00
|
|
|
return smtp.send(mailOptions);
|
2019-10-29 06:46:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Email;
|