2020-04-22 15:52:17 +02:00
|
|
|
import {Options} from "nodemailer/lib/mailer";
|
2020-09-25 23:42:15 +02:00
|
|
|
import {ParsedUrlQueryInput} from "querystring";
|
2021-05-03 19:29:22 +02:00
|
|
|
|
|
|
|
import MailError from "./MailError.js";
|
|
|
|
import MailTemplate from "./MailTemplate.js";
|
2020-04-22 15:52:17 +02:00
|
|
|
|
|
|
|
export default class Mail {
|
|
|
|
private readonly options: Options = {};
|
|
|
|
|
2020-11-03 10:29:36 +01:00
|
|
|
public constructor(
|
|
|
|
private readonly template: MailTemplate,
|
|
|
|
private readonly data: ParsedUrlQueryInput = {},
|
|
|
|
) {
|
2020-04-22 15:52:17 +02:00
|
|
|
this.options.subject = this.template.getSubject(data);
|
|
|
|
|
|
|
|
this.verifyData();
|
|
|
|
}
|
|
|
|
|
|
|
|
private verifyData() {
|
|
|
|
for (const forbiddenField of [
|
|
|
|
'to',
|
|
|
|
]) {
|
|
|
|
if (this.data[forbiddenField] !== undefined) {
|
|
|
|
throw new MailError(`Can't use reserved data.${forbiddenField}.`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-28 14:53:46 +02:00
|
|
|
public getTemplate(): MailTemplate {
|
|
|
|
return this.template;
|
2020-04-22 15:52:17 +02:00
|
|
|
}
|
|
|
|
|
2021-04-28 14:53:46 +02:00
|
|
|
public getData(): ParsedUrlQueryInput {
|
|
|
|
return {...this.data};
|
2020-04-22 15:52:17 +02:00
|
|
|
}
|
|
|
|
|
2021-04-28 14:53:46 +02:00
|
|
|
public getOptions(): Options {
|
|
|
|
return {...this.options};
|
2020-04-22 15:52:17 +02:00
|
|
|
}
|
2020-09-25 23:42:15 +02:00
|
|
|
}
|