import { Resend } from 'resend' export interface SendContactEmailConfig { /** Resend API key. Empty/missing → no-op "mock" send (handy in dev). */ apiKey: string /** Sender, e.g. `"Acme "`. The domain must be verified in Resend. */ from: string /** Recipient(s) of the notification. */ to: string | string[] /** Reply-To, typically the submitter's email so the team can reply directly. */ replyTo?: string } export interface SendResult { success: boolean /** True when no API key was configured and the send was skipped. */ mock?: boolean error?: unknown } // Reuse one Resend client per API key across invocations. let cached: { key: string; client: Resend } | null = null function getClient(apiKey: string): Resend { if (!cached || cached.key !== apiKey) { cached = { key: apiKey, client: new Resend(apiKey) } } return cached.client } /** Send the notification email via Resend. Never throws — returns a result. */ export async function sendContactEmail( config: SendContactEmailConfig, email: { subject: string; html: string } ): Promise { if (!config.apiKey) { return { success: true, mock: true } } try { const resend = getClient(config.apiKey) const { error } = await resend.emails.send({ from: config.from, to: config.to, subject: email.subject, html: email.html, ...(config.replyTo ? { replyTo: config.replyTo } : {}), }) if (error) { console.error('[contact-form] resend error:', error) return { success: false, error } } return { success: true } } catch (err) { console.error('[contact-form] send threw:', err) return { success: false, error: err } } }