import { render } from "@react-email/render"; import JoinWorkspaceTemplate from "./templates/join-workspace"; import MagicLinkTemplate from "./templates/magic-link"; type Templates = "MAGIC_LINK" | "JOIN_WORKSPACE"; const emailTemplates: Record = { MAGIC_LINK: MagicLinkTemplate, JOIN_WORKSPACE: JoinWorkspaceTemplate, }; export const sendEmail = async ( to: string, subject: string, template: Templates, data: Record, ) => { const EmailTemplate = emailTemplates[template]; const html = await render(, { pretty: true }); const response = await fetch(process.env.EMAIL_URL!, { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${process.env.EMAIL_TOKEN}`, }, body: JSON.stringify({ from: process.env.EMAIL_FROM, to, subject, html, }), }); if (!response.ok) { throw new Error(`Failed to send email: ${response.statusText}`); } return response; };