import 'server-only'; import { getCloudflareContext } from '@opennextjs/cloudflare'; import { env } from './env'; // Cloudflare Email Service (2026-11-27: everything moved off Resend). lumengesso.com is // onboarded for Email Sending; wrangler.jsonc binds it as EMAIL. Sign-in links and every // order email go through sendEmail below. type SendArgs = { to: string; subject: string; html: string; text?: string; /** Override default reply-to (defaults to hello@lumengesso.com). */ replyTo?: string; }; type Address = string | { email: string; name?: string }; type Binding = { send: (m: Record) => Promise<{ messageId?: string }> }; // Default reply-to. Real, monitored address routed through Cloudflare // Email Routing. Setting a Reply-To is a strong "real business" signal for // Gmail / Outlook spam filters and lets customers actually respond to receipts. const DEFAULT_REPLY_TO = 'hello@lumengesso.com'; // "Name " -> { email, name } const addr = (a: string): Address => { const m = a.match(/^\s*"?([^"<]*?)"?\s*<([^>]+)>\s*$/); return m ? (m[1] ? { email: m[2].trim(), name: m[1].trim() } : m[2].trim()) : a.trim(); }; export async function sendEmail({ to, subject, html, text, replyTo }: SendArgs) { let binding: Binding | undefined; try { binding = (getCloudflareContext().env as unknown as { EMAIL?: Binding }).EMAIL; } catch {} if (!binding) throw new Error('Email error: the EMAIL binding is not available'); try { const r = await binding.send({ from: addr(env.email.from()), to: addr(to), replyTo: addr(replyTo ?? DEFAULT_REPLY_TO), subject, html, ...(text ? { text } : {}), }); return { id: r?.messageId }; } catch (e) { throw new Error(`Email error: ${e instanceof Error ? e.message : String(e)}`); } }