0

Send a transactional email

by
Published Apr 8, 2025
Script brevo Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Brevo = {
3
  apiKey: string;
4
};
5
/**
6
 * Send a transactional email
7
 *
8
 */
9
export async function main(
10
  auth: Brevo,
11
  body: {
12
    sender?: { name?: string; email?: string; id?: number };
13
    to?: { email: string; name?: string }[];
14
    bcc?: { email: string; name?: string }[];
15
    cc?: { email: string; name?: string }[];
16
    htmlContent?: string;
17
    textContent?: string;
18
    subject?: string;
19
    replyTo?: { email: string; name?: string };
20
    attachment?: { url?: string; content?: string; name?: string }[];
21
    headers?: {};
22
    templateId?: number;
23
    params?: {};
24
    messageVersions?: {
25
      to: { email: string; name?: string }[];
26
      params?: {};
27
      bcc?: { email: string; name?: string }[];
28
      cc?: { email: string; name?: string }[];
29
      replyTo?: { email: string; name?: string };
30
      subject?: string;
31
      htmlContent?: string;
32
      textContent?: string;
33
    }[];
34
    tags?: string[];
35
    scheduledAt?: string;
36
    batchId?: string;
37
  },
38
) {
39
  const url = new URL(`https://api.brevo.com/v3/smtp/email`);
40

41
  const response = await fetch(url, {
42
    method: "POST",
43
    headers: {
44
      "Content-Type": "application/json",
45
      "api-key": auth.apiKey,
46
    },
47
    body: JSON.stringify(body),
48
  });
49
  if (!response.ok) {
50
    const text = await response.text();
51
    throw new Error(`${response.status} ${text}`);
52
  }
53
  return await response.json();
54
}
55