36 lines
865 B
JavaScript
36 lines
865 B
JavaScript
import nodemailer from 'nodemailer';
|
|
|
|
let transporter = null;
|
|
|
|
function getTransporter() {
|
|
if (transporter) return transporter;
|
|
|
|
const host = process.env.SMTP_HOST;
|
|
const port = Number(process.env.SMTP_PORT) || 587;
|
|
const user = process.env.SMTP_USER;
|
|
const pass = process.env.SMTP_PASS;
|
|
|
|
if (!host || !user || !pass) {
|
|
throw new Error('SMTP configuration manquante (SMTP_HOST, SMTP_USER, SMTP_PASS)');
|
|
}
|
|
|
|
transporter = nodemailer.createTransport({
|
|
host,
|
|
port,
|
|
secure: port === 465,
|
|
auth: { user, pass }
|
|
});
|
|
|
|
return transporter;
|
|
}
|
|
|
|
/**
|
|
* Send an email.
|
|
* @param {{ to: string, subject: string, html: string }} options
|
|
*/
|
|
export async function sendEmail({ to, subject, html }) {
|
|
const from = process.env.SMTP_FROM || process.env.SMTP_USER;
|
|
const t = getTransporter();
|
|
await t.sendMail({ from, to, subject, html });
|
|
}
|