2021-08-21 02:26:59 +03:00
|
|
|
import {getConfig, NodeConnectionMail} from "../helper/config";
|
2021-08-21 01:56:37 +03:00
|
|
|
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,
|
2021-08-21 02:26:59 +03:00
|
|
|
port: config?.smtp.port,
|
|
|
|
secure: config?.smtp.ssl,
|
2021-08-21 01:56:37 +03:00
|
|
|
auth: {
|
|
|
|
user: config?.smtp.username,
|
|
|
|
pass: config?.smtp.password,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Return the transporter
|
|
|
|
return transporter;
|
|
|
|
}
|
|
|
|
|
2021-08-21 02:26:59 +03:00
|
|
|
/**
|
|
|
|
* Send out an email with the config details.
|
|
|
|
*/
|
|
|
|
export async function sendMail(mailContent: string, toAddress: string, nodeConfig: NodeConnectionMail, ccAddresses?: Array<string>) {
|
2021-08-21 01:56:37 +03:00
|
|
|
|
|
|
|
// Get the transporter
|
|
|
|
const transporter = getTransporter();
|
|
|
|
|
2021-08-21 02:26:59 +03:00
|
|
|
// Generate the mail options
|
|
|
|
const mailOptions : nodemailer.SendMailOptions = {
|
|
|
|
from: config?.smtp.email_from,
|
|
|
|
to: nodeConfig.email_to,
|
|
|
|
cc: nodeConfig.email_cc,
|
|
|
|
html: mailContent,
|
|
|
|
}
|
|
|
|
|
2021-08-21 01:56:37 +03:00
|
|
|
await new Promise((_r, _e) => {
|
2021-08-21 02:26:59 +03:00
|
|
|
transporter.sendMail(mailOptions, (err, info) => {
|
2021-08-21 01:56:37 +03:00
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
transporter.close();
|
|
|
|
}
|