0
Send Email
One script reply has been approved by the moderators Verified

Send email with Mailgun. See the docs here

Created by rubenchicco 929 days ago Viewed 3470 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 229 days ago
1
type Mailgun = {
2
  api_key: string;
3
};
4

5
export async function main(
6
  resource: Mailgun,
7
  domain: string,
8
  data: {
9
    from: string;
10
    to: string;
11
    subject: string;
12
    html: string;
13
    recipient_variables?: string;
14
    template?: string;
15
    cc?: string;
16
    bcc?: string;
17
    text?: string;
18
    amp_html?: string;
19
    attachment?: string;
20
    inline?: string;
21
    "o:tracking-opens"?: string;
22
    "t:version"?: string;
23
    "t:text"?: string;
24
    "t:variables"?: string;
25
    "o:tag"?: string;
26
    "o:dkim"?: string;
27
    "o:deliverytime"?: string;
28
    "o:deliverytime-optimize-period"?: string;
29
    "o:time-zone-localize"?: string;
30
    "o:testmode"?: string;
31
    "o:tracking"?: string;
32
    "o:tracking-clicks"?: string;
33
    "o:require-tls"?: string;
34
    "o:skip-verification"?: string;
35
    others?: Record<string, string>;
36
  }
37
) {
38
  const form = new FormData();
39
  form.append("from", data.from);
40
  form.append("to", data.to);
41
  form.append("subject", data.subject);
42
  form.append("html", data.html);
43
  data.recipient_variables &&
44
    form.append("recipient-variables", data.recipient_variables);
45
  data.template && form.append("template", data.template);
46
  data.cc && form.append("cc", data.cc);
47
  data.bcc && form.append("bcc", data.bcc);
48
  data.text && form.append("text", data.text);
49
  data.amp_html && form.append("amp-html", data.amp_html);
50
  data.attachment && form.append("attachment", data.attachment);
51
  data.inline && form.append("inline", data.inline);
52
  data["o:tracking-opens"] &&
53
    form.append("o:tracking-opens", data["o:tracking-opens"]);
54
  data["t:version"] && form.append("t:version", data["t:version"]);
55
  data["t:text"] && form.append("t:text", data["t:text"]);
56
  data["t:variables"] && form.append("t:variables", data["t:variables"]);
57
  data["o:tag"] && form.append("o:tag", data["o:tag"]);
58
  data["o:dkim"] && form.append("o:dkim", data["o:dkim"]);
59
  data["o:deliverytime"] &&
60
    form.append("o:deliverytime", data["o:deliverytime"]);
61
  data["o:deliverytime-optimize-period"] &&
62
    form.append(
63
      "o:deliverytime-optimize-period",
64
      data["o:deliverytime-optimize-period"]
65
    );
66
  data["o:time-zone-localize"] &&
67
    form.append("o:time-zone-localize", data["o:time-zone-localize"]);
68
  data["o:testmode"] && form.append("o:testmode", data["o:testmode"]);
69
  data["o:tracking"] && form.append("o:tracking", data["o:tracking"]);
70
  data["o:tracking-clicks"] &&
71
    form.append("o:tracking-clicks", data["o:tracking-clicks"]);
72
  data["o:require-tls"] && form.append("o:require-tls", data["o:require-tls"]);
73
  data["o:skip-verification"] &&
74
    form.append("o:skip-verification", data["o:skip-verification"]);
75
  Object.entries(data.others || {}).forEach(([key, value]) =>
76
    form.append(key, value)
77
  );
78

79
  return (
80
    await fetch(`https://api.mailgun.net/v3/${domain}/messages`, {
81
      method: "POST",
82
      headers: {
83
        Authorization:
84
          "Basic " + Buffer.from(`api:${resource.api_key}`).toString("base64"),
85
      },
86
      body: form,
87
    })
88
  ).json();
89
}
90