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,eyJ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