Files
mintel.me/apps/web/src/lib/mail/mailer.ts
Marc Mintel 1f0de18755
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 8s
Build & Deploy / 🏗️ Build (push) Failing after 22m37s
Build & Deploy / 🚀 Deploy (push) Has been skipped
Build & Deploy / 🩺 Smoke Test (push) Has been skipped
Build & Deploy / 🔔 Notify (push) Successful in 2s
fix(form): resolve silent failures and cleanup deployment pipeline
2026-04-11 23:21:13 +02:00

98 lines
2.3 KiB
TypeScript

import nodemailer from "nodemailer";
import { env } from "../env";
let transporterInstance: nodemailer.Transporter | null = null;
function getTransporter() {
if (transporterInstance) return transporterInstance;
if (!env.MAIL_HOST) {
// In development, we might not have mail configured, so we log instead of throwing
if (process.env.NODE_ENV === "development") {
console.warn(
"MAIL_HOST is not configured. Emails will be logged to console.",
);
return null;
}
throw new Error(
"MAIL_HOST is not configured. Please check your environment variables.",
);
}
transporterInstance = nodemailer.createTransport({
host: env.MAIL_HOST,
port: env.MAIL_PORT,
secure: env.MAIL_PORT === 465,
auth: {
user: env.MAIL_USERNAME,
pass: env.MAIL_PASSWORD,
},
});
return transporterInstance;
}
interface SendEmailOptions {
to?: string | string[];
replyTo?: string;
subject: string;
html: string;
}
export async function sendEmail({
to,
replyTo,
subject,
html,
}: SendEmailOptions) {
let recipients = to || env.MAIL_RECIPIENTS;
let from = env.MAIL_FROM;
if (!from) {
from = "info@mintel.me";
console.warn("MAIL_FROM is empty. Using fallback: info@mintel.me");
}
if (!recipients) {
recipients = "marc@mintel.me";
console.warn("MAIL_RECIPIENTS is empty. Using fallback: marc@mintel.me");
}
const transporter = getTransporter();
const mailOptions = {
from,
to: recipients,
replyTo,
subject,
html,
};
if (!transporter) {
console.log("--- EMAIL SIMULATION ---");
console.log("To:", recipients);
console.log("Subject:", subject);
console.log("HTML Output suppressed");
console.log("--- END SIMULATION ---");
return { success: true, messageId: "simulated-id" };
}
try {
const info = await transporter.sendMail(mailOptions);
console.log("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);
console.error("Error sending email", {
error: errorMsg,
subject,
recipients,
});
return { success: false, error: errorMsg };
}
}