This repository has been archived on 2024-01-19. You can view files and clone it, but cannot push or open issues or pull requests.
DataHoard/src/models/MailModel.ts

50 lines
1.3 KiB
TypeScript
Raw Normal View History

import {getConfig, NodeConnectionMail} from "../helper/config";
import nodemailer from "nodemailer";
const config = getConfig();
/**
* Get the instance of the transporter
* @returns A transporter instance for sending out emails
*/
function getTransporter() : nodemailer.Transporter {
// Generate the transporter
const transporter = nodemailer.createTransport({
host: config?.smtp.host,
port: config?.smtp.port,
secure: config?.smtp.ssl,
auth: {
user: config?.smtp.username,
pass: config?.smtp.password,
}
});
// Return the transporter
return transporter;
}
/**
* Send out an email with the config details.
*/
export async function sendMail(mailContent: string, toAddress: string, nodeConfig: NodeConnectionMail, ccAddresses?: Array<string>) {
// Get the transporter
const transporter = getTransporter();
// Generate the mail options
const mailOptions : nodemailer.SendMailOptions = {
from: config?.smtp.email_from,
to: nodeConfig.email_to,
cc: nodeConfig.email_cc,
html: mailContent,
}
await new Promise((_r, _e) => {
transporter.sendMail(mailOptions, (err, info) => {
})
})
transporter.close();
}