0

Send an email

by
Published Oct 22, 2023
Script resend Verified

The script

Submitted by rubenfiszel Typescript (fetch-only)
Verified 398 days ago
1
type Resend = {
2
  token: string;
3
};
4
/**
5
 * Send an email
6
 *
7
 */
8
export async function main(
9
  auth: Resend,
10
  body: {
11
    from: string;
12
    to: string[];
13
    subject: string;
14
    bcc?: string;
15
    cc?: string;
16
    reply_to?: string;
17
    html?: string;
18
    text?: string;
19
    headers?: { [k: string]: unknown };
20
    attachments?: {
21
      content?: string;
22
      filename?: string;
23
      path?: string;
24
      [k: string]: unknown;
25
    }[];
26
    tags?: { name?: string; value?: string; [k: string]: unknown }[];
27
    [k: string]: unknown;
28
  }
29
) {
30
  const url = new URL(`https://api.resend.com/emails`);
31

32
  const response = await fetch(url, {
33
    method: "POST",
34
    headers: {
35
      "Content-Type": "application/json",
36
      Authorization: "Bearer " + auth.token,
37
    },
38
    body: JSON.stringify(body),
39
  });
40
  if (!response.ok) {
41
    const text = await response.text();
42
    throw new Error(`${response.status} ${text}`);
43
  }
44
  return await response.json();
45
}
46