From 86109eb770a4dfac4088e534e6948556e23d3b7c Mon Sep 17 00:00:00 2001 From: regalijan Date: Thu, 19 Oct 2023 16:51:00 -0400 Subject: [PATCH] Create common email send function --- functions/email.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 functions/email.ts diff --git a/functions/email.ts b/functions/email.ts new file mode 100644 index 0000000..9133c7e --- /dev/null +++ b/functions/email.ts @@ -0,0 +1,29 @@ +export default async function ( + email: string, + sendingKey: string, + subject: string, + template: string, + variables: { + [k: string]: string; + }, +) { + const body = new FormData(); + body.append("from", "noreply@mail.carcrushers.cc"); + body.append("subject", subject); + body.append("template", template); + body.append("to", email); + + for (const [name, value] of Object.entries(variables)) + body.append(`v:${name}`, value); + + return await fetch( + "https://api.mailgun.net/v3/mail.carcrushers.cc/messages", + { + body, + headers: { + authorization: `Basic ${btoa("api" + ":" + sendingKey)}`, + }, + method: "POST", + }, + ); +}