Added more explicit type for SMTP config and refactored transport initialization.

This commit is contained in:
baldo 2022-07-28 14:45:28 +02:00
commit 13e4895b81
6 changed files with 84 additions and 43 deletions

29
server/mail/index.ts Normal file
View file

@ -0,0 +1,29 @@
import {createTransport, Transporter} from "nodemailer";
import {config} from "../config";
import * as MailTemplateService from "../services/mailTemplateService";
import Mail from "nodemailer/lib/mailer";
import SMTPTransport from "nodemailer/lib/smtp-transport";
let transporterSingleton: Transporter | null = null;
function transporter() {
if (!transporterSingleton) {
const options = {
...config.server.email.smtp,
pool: true,
};
transporterSingleton = createTransport(new SMTPTransport(options));
MailTemplateService.configureTransporter(transporterSingleton);
}
return transporterSingleton;
}
export function init(): void {
transporter();
}
export async function send(options: Mail.Options): Promise<void> {
await transporter().sendMail(options);
}