45 lines
1.0 KiB
TypeScript
45 lines
1.0 KiB
TypeScript
// utils/mail.ts – simple wrapper around nodemailer
|
||
import nodemailer from 'nodemailer';
|
||
|
||
// Load SMTP configuration from environment variables
|
||
const smtpConfig = {
|
||
host: process.env.SMTP_HOST,
|
||
port: Number(process.env.SMTP_PORT),
|
||
secure: process.env.SMTP_SECURE === 'true' || process.env.SMTP_SECURE === '1', // true for 465, false for other ports
|
||
auth: {
|
||
user: process.env.SMTP_USER,
|
||
pass: process.env.SMTP_PASS,
|
||
},
|
||
};
|
||
|
||
// Create a reusable transporter. If any required var is missing, nodemailer will throw on send.
|
||
const transporter = nodemailer.createTransport(smtpConfig);
|
||
|
||
/**
|
||
* Send an email.
|
||
* @param to Recipient address
|
||
* @param subject Subject line
|
||
* @param text Plain‑text body
|
||
* @param html Optional HTML body
|
||
*/
|
||
export async function sendMail({
|
||
to,
|
||
subject,
|
||
text,
|
||
html,
|
||
}: {
|
||
to: string;
|
||
subject: string;
|
||
text: string;
|
||
html?: string;
|
||
}) {
|
||
const info = await transporter.sendMail({
|
||
from: smtpConfig.auth.user,
|
||
to,
|
||
subject,
|
||
text,
|
||
html,
|
||
});
|
||
return info;
|
||
}
|