import { Resend } from "resend"; const FONT_STACK = "'Barlow', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif"; const FROM_DEFAULT = "Brackt "; // Lazily initialized so import doesn't throw in test environments without RESEND_API_KEY. let _resend: Resend | null = null; function getResend(): Resend { if (!_resend) { const key = process.env.RESEND_API_KEY; if (!key) throw new Error("RESEND_API_KEY is not set"); _resend = new Resend(key); } return _resend; } export async function sendEmail(opts: { to: string; subject: string; html: string; text?: string; replyTo?: string; from?: string; }): Promise<{ error: Error | null }> { const resend = getResend(); const { error } = await resend.emails.send({ from: opts.from ?? FROM_DEFAULT, to: opts.to, subject: opts.subject, html: opts.html, ...(opts.text ? { text: opts.text } : {}), ...(opts.replyTo ? { replyTo: opts.replyTo } : {}), }); return { error: error ? new Error(error.message) : null }; } export function escapeHtml(s: string): string { return s .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'"); } export function emailParagraph(html: string): string { return `

${html}

`; } export function emailButton(href: string, text: string): string { return `
${text}
`; } export function wrapInEmailTemplate( content: string, preheader?: string ): string { const appUrl = process.env.APP_URL ?? "https://brackt.com"; const logoUrl = `${appUrl}/email-logo.png`; const year = new Date().getFullYear(); const preheaderHtml = preheader ? `${escapeHtml(preheader)} ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌` : ""; return ` ${preheaderHtml}
Brackt
${content}

© ${year} Brackt. All rights reserved.

`; }