Openstatus
www.openstatus.dev
1import type React from "react";
2import { Resend } from "resend";
3
4import { render } from "@react-email/render";
5import { env } from "./env";
6
7export const resend = new Resend(env.RESEND_API_KEY);
8
9export interface Emails {
10 react: React.JSX.Element;
11 subject: string;
12 to: string[];
13 from: string;
14 reply_to?: string;
15}
16
17export type EmailHtml = {
18 html: string;
19 subject: string;
20 to: string;
21 from: string;
22 reply_to?: string;
23};
24export const sendEmail = async (email: Emails) => {
25 if (process.env.NODE_ENV !== "production") return;
26 await resend.emails.send(email);
27};
28
29export const sendBatchEmailHtml = async (emails: EmailHtml[]) => {
30 if (process.env.NODE_ENV !== "production") return;
31 await resend.batch.send(emails);
32};
33
34// TODO: delete in favor of sendBatchEmailHtml
35export const sendEmailHtml = async (emails: EmailHtml[]) => {
36 if (process.env.NODE_ENV !== "production") return;
37
38 await fetch("https://api.resend.com/emails/batch", {
39 method: "POST",
40 headers: {
41 "Content-Type": "application/json",
42 Authorization: `Bearer ${env.RESEND_API_KEY}`,
43 },
44 body: JSON.stringify(emails),
45 });
46};
47
48export const sendWithRender = async (email: Emails) => {
49 if (process.env.NODE_ENV !== "production") return;
50 const html = await render(email.react);
51 await resend.emails.send({
52 ...email,
53 html,
54 });
55};