122 lines
16 KiB
JavaScript
122 lines
16 KiB
JavaScript
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||
|
return new (P || (P = Promise))(function (resolve, reject) {
|
||
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||
|
});
|
||
|
};
|
||
|
import nodemailer from "nodemailer";
|
||
|
import config from "config";
|
||
|
import nunjucks from 'nunjucks';
|
||
|
import * as util from "util";
|
||
|
import { WrappingError } from "./Utils";
|
||
|
import mjml2html from "mjml";
|
||
|
import * as querystring from "querystring";
|
||
|
import Logger from "./Logger";
|
||
|
export function mailRoute(template) {
|
||
|
return `/mail/${template}`;
|
||
|
}
|
||
|
export default class Mail {
|
||
|
constructor(template, data = {}) {
|
||
|
this.options = {};
|
||
|
this.template = template;
|
||
|
this.data = data;
|
||
|
this.options.subject = this.template.getSubject(data);
|
||
|
this.verifyData();
|
||
|
}
|
||
|
static getTransporter() {
|
||
|
if (!this.transporter)
|
||
|
throw new MailError('Mail system was not prepared.');
|
||
|
return this.transporter;
|
||
|
}
|
||
|
static prepare() {
|
||
|
return __awaiter(this, void 0, void 0, function* () {
|
||
|
const transporter = nodemailer.createTransport({
|
||
|
host: config.get('mail.host'),
|
||
|
port: config.get('mail.port'),
|
||
|
secure: config.get('mail.secure'),
|
||
|
auth: {
|
||
|
user: config.get('mail.username'),
|
||
|
pass: config.get('mail.password'),
|
||
|
},
|
||
|
tls: {
|
||
|
rejectUnauthorized: !config.get('mail.allow_invalid_tls')
|
||
|
}
|
||
|
});
|
||
|
try {
|
||
|
yield util.promisify(transporter.verify)();
|
||
|
this.transporter = transporter;
|
||
|
}
|
||
|
catch (e) {
|
||
|
throw new MailError('Connection to mail service unsuccessful.', e);
|
||
|
}
|
||
|
Logger.info(`Mail ready to be distributed via ${config.get('mail.host')}:${config.get('mail.port')}`);
|
||
|
});
|
||
|
}
|
||
|
static end() {
|
||
|
this.transporter.close();
|
||
|
}
|
||
|
static parse(template, data, textOnly) {
|
||
|
data.text = textOnly;
|
||
|
const nunjucksResult = nunjucks.render(template, data);
|
||
|
if (textOnly)
|
||
|
return nunjucksResult;
|
||
|
const mjmlResult = mjml2html(nunjucksResult, {});
|
||
|
if (mjmlResult.errors.length > 0) {
|
||
|
throw new MailError(`Error while parsing mail template ${template}: ${JSON.stringify(mjmlResult.errors, null, 4)}`);
|
||
|
}
|
||
|
return mjmlResult.html;
|
||
|
}
|
||
|
verifyData() {
|
||
|
for (const forbiddenField of [
|
||
|
'to',
|
||
|
]) {
|
||
|
if (this.data[forbiddenField] !== undefined) {
|
||
|
throw new MailError(`Can't use reserved data.${forbiddenField}.`);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
send(...to) {
|
||
|
return __awaiter(this, void 0, void 0, function* () {
|
||
|
const results = [];
|
||
|
for (const destEmail of to) {
|
||
|
// Reset options
|
||
|
this.options.html = this.options.text = undefined;
|
||
|
// Set options
|
||
|
this.options.to = destEmail;
|
||
|
// Set data
|
||
|
this.data.mail_subject = this.options.subject;
|
||
|
this.data.mail_to = this.options.to;
|
||
|
this.data.mail_link = `${config.get('public_url')}${mailRoute(this.template.template)}?${querystring.stringify(this.data)}`;
|
||
|
// Log
|
||
|
Logger.dev('Send mail', this.options);
|
||
|
// Render email
|
||
|
this.options.html = Mail.parse('mails/' + this.template.template + '.mjml.njk', this.data, false);
|
||
|
this.options.text = Mail.parse('mails/' + this.template.template + '.mjml.njk', this.data, true);
|
||
|
// Send email
|
||
|
results.push(yield Mail.getTransporter().sendMail(this.options));
|
||
|
}
|
||
|
return results;
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
export class MailTemplate {
|
||
|
constructor(template, subject) {
|
||
|
this._template = template;
|
||
|
this.subject = subject;
|
||
|
}
|
||
|
get template() {
|
||
|
return this._template;
|
||
|
}
|
||
|
getSubject(data) {
|
||
|
return 'Watch My Stream - ' + this.subject(data);
|
||
|
}
|
||
|
}
|
||
|
class MailError extends WrappingError {
|
||
|
constructor(message = 'An error occurred while sending mail.', cause) {
|
||
|
super(message, cause);
|
||
|
}
|
||
|
}
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiTWFpbC5qcyIsInNvdXJjZVJvb3QiOiIuLyIsInNvdXJjZXMiOlsiTWFpbC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7QUFBQSxPQUFPLFVBQTBDLE1BQU0sWUFBWSxDQUFDO0FBQ3BFLE9BQU8sTUFBTSxNQUFNLFFBQVEsQ0FBQztBQUU1QixPQUFPLFFBQVEsTUFBTSxVQUFVLENBQUM7QUFDaEMsT0FBTyxLQUFLLElBQUksTUFBTSxNQUFNLENBQUM7QUFDN0IsT0FBTyxFQUFDLGFBQWEsRUFBQyxNQUFNLFNBQVMsQ0FBQztBQUN0QyxPQUFPLFNBQVMsTUFBTSxNQUFNLENBQUM7QUFDN0IsT0FBTyxLQUFLLFdBQVcsTUFBTSxhQUFhLENBQUM7QUFDM0MsT0FBTyxNQUFNLE1BQU0sVUFBVSxDQUFDO0FBRTlCLE1BQU0sVUFBVSxTQUFTLENBQUMsUUFBZ0I7SUFDdEMsT0FBTyxTQUFTLFFBQVEsRUFBRSxDQUFDO0FBQy9CLENBQUM7QUFFRCxNQUFNLENBQUMsT0FBTyxPQUFPLElBQUk7SUFzRHJCLFlBQVksUUFBc0IsRUFBRSxPQUE2QixFQUFFO1FBSGxELFlBQU8sR0FBWSxFQUFFLENBQUM7UUFJbkMsSUFBSSxDQUFDLFFBQVEsR0FBRyxRQUFRLENBQUM7UUFDekIsSUFBSSxDQUFDLElBQUksR0FBRyxJQUFJL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