Files
klz-cables.com/lib/mail/mailer.ts
2026-02-26 01:32:22 +01:00

67 lines
2.0 KiB
TypeScript

import nodemailer from 'nodemailer';
import { getServerAppServices } from '@/lib/services/create-services.server';
import { config } from '../config';
let transporterInstance: nodemailer.Transporter | null = null;
function getTransporter() {
if (transporterInstance) return transporterInstance;
if (!config.mail.host) {
throw new Error('MAIL_HOST is not configured. Please check your environment variables.');
}
transporterInstance = nodemailer.createTransport({
host: config.mail.host,
port: config.mail.port,
secure: config.mail.port === 465,
auth: {
user: config.mail.user,
pass: config.mail.pass,
},
});
return transporterInstance;
}
interface SendEmailOptions {
to?: string | string[];
replyTo?: string;
subject: string;
html: string;
}
export async function sendEmail({ to, replyTo, subject, html }: SendEmailOptions) {
const recipients = to || config.mail.recipients;
const logger = getServerAppServices().logger.child({ component: 'mailer' });
if (!recipients) {
logger.error('No email recipients configured (MAIL_RECIPIENTS is empty and no "to" provided)', { subject });
return { success: false as const, error: 'No recipients configured' };
}
if (!config.mail.from) {
logger.error('MAIL_FROM is not configured — cannot send email', { subject, recipients });
return { success: false as const, error: 'MAIL_FROM is not configured' };
}
const mailOptions = {
from: config.mail.from,
to: recipients,
replyTo,
subject,
html,
};
try {
const info = await getTransporter().sendMail(mailOptions);
logger.info('Email sent successfully', { messageId: info.messageId, subject, recipients });
return { success: true, messageId: info.messageId };
} catch (error) {
const errorMsg = error instanceof Error ? error.message : String(error);
logger.error('Error sending email', { error: errorMsg, subject, recipients });
return { success: false, error: errorMsg };
}
}