import nodemailer, { type Transporter } from 'nodemailer'; import { getSettings } from './settings'; /** * E-Mail-Versand über SMTP (Einstellungen in der .env). * Ohne SMTP-Konfiguration wird die Nachricht nur ins Server-Log geschrieben. */ const host = process.env.SMTP_HOST; let transport: Transporter | null = null; export const mailConfigured = () => !!host; function transporter() { if (!host) return null; transport ??= nodemailer.createTransport({ host, port: Number(process.env.SMTP_PORT ?? 587), secure: process.env.SMTP_SECURE === 'true' || process.env.SMTP_PORT === '465', auth: process.env.SMTP_USER ? { user: process.env.SMTP_USER, pass: process.env.SMTP_PASS ?? '' } : undefined }); return transport; } export async function sendMail({ to, subject, text }: { to: string; subject: string; text: string }): Promise { const t = transporter(); const from = process.env.SMTP_FROM ?? `${getSettings().appName} `; if (!t) { console.log(`[mail] SMTP nicht eingerichtet – E-Mail an ${to} nicht versendet:\n Betreff: ${subject}\n ${text.replace(/\n/g, '\n ')}`); return false; } try { await t.sendMail({ from, to, subject, text }); return true; } catch (e) { console.error('[mail] Versand fehlgeschlagen:', (e as Error).message); return false; } }