import nodemailer from "nodemailer"; export interface ContactMessage { email: string; message: string; name?: string; /** Optional context, e.g. the search dates that had no availability. */ context?: string; } function getTransport() { const { SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASSWORD } = process.env; if (!SMTP_HOST || !SMTP_USER || !SMTP_PASSWORD) { return null; } return nodemailer.createTransport({ host: SMTP_HOST, port: Number(SMTP_PORT) || 587, secure: Number(SMTP_PORT) === 465, auth: { user: SMTP_USER, pass: SMTP_PASSWORD, }, }); } export async function sendContactMessage(data: ContactMessage): Promise { const transport = getTransport(); const to = process.env.CONTACT_INBOX_EMAIL || "info@bb-apartment.de"; const from = process.env.SMTP_FROM || "noreply@bb-apartment.de"; const subject = `Neue Kontaktanfrage von ${data.name || data.email}`; const text = [ data.context ? `Kontext: ${data.context}` : null, `Name: ${data.name || "-"}`, `E-Mail: ${data.email}`, "", data.message, ] .filter(Boolean) .join("\n"); if (!transport) { // No SMTP configured yet (development) — log instead of failing so the // form still "works" locally. console.log("[contact] SMTP not configured, would have sent:", { to, from, subject, text, }); return; } await transport.sendMail({ to, from, replyTo: data.email, subject, text, }); }