import { env } from "~/env"; import { Unsend } from "unsend"; let unsend: Unsend | undefined; const getClient = () => { if (!unsend) { unsend = new Unsend(env.UNSEND_API_KEY); } return unsend; }; export async function sendSignUpEmail( email: string, token: string, url: string ) { const { host } = new URL(url); if (env.NODE_ENV === "development") { console.log("Sending sign in email", email, url, token); return; } const subject = "Sign in to Unsend"; const text = `Hey,\n\nYou can sign in to Unsend by clicking the below URL:\n${url}\n\nYou can also use this OTP: ${token}\n\nThanks,\nUnsend Team`; const html = `
Hey,
You can sign in to Unsend by clicking the below URL:
You can also use this OTP: ${token}
Thanks,
Unsend Team
`; await sendMail(email, subject, text, html); } async function sendMail( email: string, subject: string, text: string, html: string ) { if (env.UNSEND_API_KEY && env.UNSEND_URL && env.FROM_EMAIL) { const resp = await getClient().emails.send({ to: email, from: env.FROM_EMAIL, subject, text, html, }); if (resp.data) { console.log("Email sent using unsend"); return; } else { console.log( "Error sending email using unsend, so fallback to resend", resp.error?.code, resp.error?.message ); } } else { throw new Error("UNSEND_API_KEY or UNSEND_URL not found"); } }