ff-admin-server/src/helpers/mailHelper.ts

43 lines
1.2 KiB
TypeScript

import { Transporter, createTransport, TransportOptions } from "nodemailer";
import { CLUB_NAME, MAIL_HOST, MAIL_PASSWORD, MAIL_PORT, MAIL_SECURE, MAIL_USERNAME } from "../env.defaults";
import { Attachment } from "nodemailer/lib/mailer";
export default abstract class MailHelper {
private static readonly transporter: Transporter = createTransport({
host: MAIL_HOST,
port: MAIL_PORT,
secure: (MAIL_SECURE as "true" | "false") == "true",
auth: {
user: MAIL_USERNAME,
pass: MAIL_PASSWORD,
},
} as TransportOptions);
/**
* @description send mail
* @param {string} target
* @param {string} subject
* @param {string} content
* @returns {Prmose<*>}
*/
static async sendMail(
target: string,
subject: string,
content: string,
attach: Array<Attachment> = []
): Promise<any> {
return new Promise((resolve, reject) => {
this.transporter
.sendMail({
from: `"${CLUB_NAME}" <${MAIL_USERNAME}>`,
to: target,
subject,
text: content,
html: content,
attachments: attach,
})
.then((info) => resolve(info.messageId))
.catch((e) => reject(e));
});
}
}