import type { ReactElement } from 'react' import { env } from '~/env' import { resendAdapter } from './adapters/resend' import { createMailer } from './factory' import type { MailAddress, Mailer } from './port' export type EmailRecipient = MailAddress function required(value: string | undefined, name: string): string { if (!value) throw new Error(`${name} is required to send email`) return value } let mailer: Mailer | null = null function getMailer(): Mailer { if (!mailer) { mailer = createMailer({ from: env.EMAIL_FROM, adapter: resendAdapter({ apiKey: required(env.RESEND_API_KEY, 'RESEND_API_KEY') }), }) } return mailer } export async function sendEmail(params: { to: EmailRecipient subject: string template: ReactElement }) { return getMailer().send({ to: params.to, subject: params.subject, react: params.template, }) }