import type { ContactData, ContactField } from '../types' export interface BrandConfig { /** Brand name shown in the email header + footer. */ name: string /** Brand URL shown in the footer. */ url?: string /** Accent color for the header name and the reply button. Default `#09090b`. */ accent?: string } export interface RenderContactEmailOptions { fields: ReadonlyArray data: ContactData brand?: BrandConfig /** Subject line, or a builder. Defaults to `New contact message: `. */ subject?: string | ((data: ContactData) => string) /** Lead paragraph above the fields. */ intro?: string /** Audit metadata appended in a small footer. */ meta?: { ip?: string | null; userAgent?: string | null } /** Address the "Reply" button targets (omit to hide the button). */ replyToEmail?: string } const escapeHtml = (value: string) => value.replace(/&/g, '&').replace(//g, '>') function layout(content: string, brand: BrandConfig): string { const accent = brand.accent ?? '#09090b' const footer = brand.url ? `${escapeHtml(brand.name)} — ${escapeHtml(brand.url)}` : escapeHtml(brand.name) return `
${escapeHtml(brand.name)}
${content}
${footer}
` } function displayValue(field: ContactField, value: string): string { if (field.type === 'select' && field.options) { return field.options.find((option) => option.value === value)?.label ?? value } return value } /** * Render the internal notification email for a contact submission. * * Inline fields render as a label/value table; `textarea` fields (or any * field with `block: true`) render as full-width boxes preserving line breaks. * All values are HTML-escaped. */ export function renderContactEmail( options: RenderContactEmailOptions ): { subject: string; html: string } { const brand: BrandConfig = options.brand ?? { name: 'Contact form' } const accent = brand.accent ?? '#09090b' const isBlock = (field: ContactField) => field.block === true || field.type === 'textarea' const rows = options.fields .filter((field) => !isBlock(field)) .map((field) => { const value = options.data[field.name] if (!value) return '' return ` ${escapeHtml(field.label)} ${escapeHtml(displayValue(field, value))} ` }) .join('') const blocks = options.fields .filter(isBlock) .map((field) => { const value = options.data[field.name] if (!value) return '' return `

${escapeHtml(field.label)}

${escapeHtml(value)}

` }) .join('') const intro = options.intro ?? 'New message received through the contact form.' const replyButton = options.replyToEmail ? `

Reply

` : '' const footer = options.meta?.ip || options.meta?.userAgent ? `

${options.meta.ip ? `IP: ${escapeHtml(options.meta.ip)}
` : ''} ${options.meta.userAgent ? `UA: ${escapeHtml(options.meta.userAgent)}` : ''}

` : '' const content = `

${escapeHtml(intro)}

${rows ? `${rows}
` : ''} ${blocks}${replyButton}${footer}` let subject: string if (typeof options.subject === 'function') { subject = options.subject(options.data) } else if (typeof options.subject === 'string') { subject = options.subject } else { const firstFilled = options.fields.find((field) => options.data[field.name]) const who = firstFilled ? options.data[firstFilled.name] : '' subject = `New contact message${who ? `: ${who}` : ''}` } return { subject, html: layout(content, brand) } }