import nodemailer from 'nodemailer'; const { SMTP_HOST, SMTP_PORT = '587', SMTP_USER, SMTP_PASSWORD, SMTP_FROM, NOTIFY_EMAIL } = process.env; const transporter = SMTP_HOST ? nodemailer.createTransport({ host: SMTP_HOST, port: Number(SMTP_PORT), secure: Number(SMTP_PORT) === 465, auth: SMTP_USER ? { user: SMTP_USER, pass: SMTP_PASSWORD } : undefined, }) : null; /** Fire-and-forget notification sent whenever a visitor submits data. Never blocks the request. */ export async function notifySubmission({ event, lead, roi, submittedAt }) { if (!transporter || !NOTIFY_EMAIL) { console.info('[mail] SMTP not configured — skipping submission notification'); return; } const subject = event === 'roi_report_downloaded' ? `ROI report downloaded — ${lead.email}` : `New toolkit registration — ${lead.email}`; const lines = [ `Event: ${event}`, `Submitted at: ${submittedAt || new Date().toISOString()}`, '', `Name: ${[lead.first, lead.last].filter(Boolean).join(' ') || '—'}`, `Email: ${lead.email}`, `Company: ${lead.company || '—'}`, `Title: ${lead.title || '—'}`, `Region: ${lead.region || '—'}`, `Subscriber band: ${lead.subs || '—'}`, ]; if (roi?.results) { lines.push( '', 'ROI results:', ` Total: ${roi.formatted?.total ?? '—'}`, ` Bandwidth savings: ${roi.formatted?.bw ?? '—'}`, ` Support calls savings: ${roi.formatted?.calls ?? '—'}`, ` Churn savings: ${roi.formatted?.churn ?? '—'}`, ` Equipment savings: ${roi.formatted?.equip ?? '—'}`, ` DDoS savings: ${roi.formatted?.ddos ?? '—'}` ); } try { await transporter.sendMail({ from: SMTP_FROM || SMTP_USER, to: NOTIFY_EMAIL, subject, text: lines.join('\n'), }); } catch (err) { console.error('[mail] failed to send submission notification:', err); } }