2024-08-25 13:36:19 +02:00
|
|
|
import { Transporter, createTransport, TransportOptions } from "nodemailer";
|
2024-12-30 14:47:00 +01:00
|
|
|
import { Attachment } from "nodemailer/lib/mailer";
|
2025-04-19 16:51:37 +02:00
|
|
|
import SettingHelper from "./settingsHelper";
|
2024-08-25 13:36:19 +02:00
|
|
|
|
2024-12-28 18:03:33 +01:00
|
|
|
export default abstract class MailHelper {
|
2025-04-19 16:51:37 +02:00
|
|
|
private static transporter: Transporter;
|
|
|
|
|
|
|
|
static createTransport() {
|
2025-04-20 15:32:57 +02:00
|
|
|
this.transporter?.close();
|
|
|
|
|
2025-04-19 16:51:37 +02:00
|
|
|
this.transporter = createTransport({
|
|
|
|
host: SettingHelper.getSetting("mail.host"),
|
|
|
|
port: SettingHelper.getSetting("mail.port"),
|
2025-04-20 15:32:57 +02:00
|
|
|
secure: SettingHelper.getSetting("mail.secure"),
|
2025-04-19 16:51:37 +02:00
|
|
|
auth: {
|
|
|
|
user: SettingHelper.getSetting("mail.username"),
|
|
|
|
pass: SettingHelper.getSetting("mail.password"),
|
|
|
|
},
|
|
|
|
} as TransportOptions);
|
|
|
|
}
|
2024-08-25 13:36:19 +02:00
|
|
|
|
2025-04-20 15:32:57 +02:00
|
|
|
static initialize() {
|
|
|
|
SettingHelper.onSettingTopicChanged("mail", () => {
|
|
|
|
this.createTransport();
|
|
|
|
});
|
|
|
|
this.createTransport();
|
|
|
|
}
|
|
|
|
|
2024-08-25 13:36:19 +02:00
|
|
|
/**
|
|
|
|
* @description send mail
|
|
|
|
* @param {string} target
|
|
|
|
* @param {string} subject
|
|
|
|
* @param {string} content
|
|
|
|
* @returns {Prmose<*>}
|
|
|
|
*/
|
2024-12-30 14:47:00 +01:00
|
|
|
static async sendMail(
|
|
|
|
target: string,
|
|
|
|
subject: string,
|
|
|
|
content: string,
|
|
|
|
attach: Array<Attachment> = []
|
|
|
|
): Promise<any> {
|
2024-08-25 13:36:19 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.transporter
|
|
|
|
.sendMail({
|
2025-04-19 16:51:37 +02:00
|
|
|
from: `"${SettingHelper.getSetting("club.name")}" <${SettingHelper.getSetting("mail.username")}>`,
|
2024-08-25 13:36:19 +02:00
|
|
|
to: target,
|
|
|
|
subject,
|
|
|
|
text: content,
|
2024-12-28 18:03:33 +01:00
|
|
|
html: content,
|
2024-12-30 14:47:00 +01:00
|
|
|
attachments: attach,
|
2024-08-25 13:36:19 +02:00
|
|
|
})
|
|
|
|
.then((info) => resolve(info.messageId))
|
|
|
|
.catch((e) => reject(e));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|