From 8f900ce7c0a410709fa17bd5db5e95dfd0d6aac3 Mon Sep 17 00:00:00 2001 From: Calum Muir Date: Mon, 22 Dec 2025 14:42:04 +0000 Subject: [PATCH] Switch contact email to Resend --- src/worker.ts | 66 ++++++++++++--------------------------------------- 1 file changed, 15 insertions(+), 51 deletions(-) diff --git a/src/worker.ts b/src/worker.ts index 522fd80..1d8898d 100644 --- a/src/worker.ts +++ b/src/worker.ts @@ -1,6 +1,6 @@ type Env = { TURNSTILE_SECRET_KEY?: string; - AZURE_COMMUNICATION_CONNECTION_STRING?: string; + RESEND_API_KEY?: string; CONTACT_FROM_EMAIL?: string; CONTACT_TO_EMAIL?: string; ASSETS: Fetcher; @@ -15,23 +15,6 @@ const jsonResponse = (data: Record, status = 200) => const readFormField = (value: FormDataEntryValue | null) => typeof value === "string" ? value.trim() : ""; -const parseConnectionString = (connectionString: string) => { - const parts = connectionString.split(";").filter(Boolean); - const map = new Map(); - for (const part of parts) { - const [rawKey, ...rest] = part.split("="); - const key = rawKey?.trim().toLowerCase(); - const value = rest.join("=").trim(); - if (key && value) { - map.set(key, value); - } - } - return { - endpoint: map.get("endpoint") ?? "", - accessKey: map.get("accesskey") ?? map.get("access_key") ?? "", - }; -}; - const handleContact = async (request: Request, env: Env) => { const formData = await request.formData(); const name = readFormField(formData.get("name")); @@ -84,7 +67,7 @@ const handleContact = async (request: Request, env: Env) => { ); } - if (!env.AZURE_COMMUNICATION_CONNECTION_STRING) { + if (!env.RESEND_API_KEY) { return jsonResponse( { ok: false, error: "Email provider not configured." }, 500, @@ -100,39 +83,22 @@ const handleContact = async (request: Request, env: Env) => { } const toEmail = env.CONTACT_TO_EMAIL ?? "calum@muir.in"; - const { endpoint, accessKey } = parseConnectionString( - env.AZURE_COMMUNICATION_CONNECTION_STRING, - ); - if (!endpoint || !accessKey) { - return jsonResponse( - { ok: false, error: "Email provider not configured." }, - 500, - ); - } - const emailPayload = { - senderAddress: fromEmail, - content: { - subject: "New contact form submission", - plainText: `Name: ${name}\nEmail: ${email}\n\n${message}`, - }, - recipients: { - to: [{ address: toEmail }], - }, - replyTo: [{ address: email }], + from: fromEmail, + to: [toEmail], + reply_to: email, + subject: "New contact form submission", + text: `Name: ${name}\nEmail: ${email}\n\n${message}`, }; - const response = await fetch( - `${endpoint.replace(/\/$/, "")}/emails:send?api-version=2023-03-31`, - { - method: "POST", - headers: { - "Content-Type": "application/json", - "api-key": accessKey, - }, - body: JSON.stringify(emailPayload), + const response = await fetch("https://api.resend.com/emails", { + method: "POST", + headers: { + Authorization: `Bearer ${env.RESEND_API_KEY}`, + "Content-Type": "application/json", }, - ); + body: JSON.stringify(emailPayload), + }); if (!response.ok) { const contentType = response.headers.get("content-type") ?? ""; const errorText = contentType.includes("application/json") @@ -177,9 +143,7 @@ export default { return jsonResponse({ ok: true, hasTurnstileSecret: Boolean(env.TURNSTILE_SECRET_KEY), - hasAzureConnectionString: Boolean( - env.AZURE_COMMUNICATION_CONNECTION_STRING, - ), + hasResendApiKey: Boolean(env.RESEND_API_KEY), hasContactFromEmail: Boolean(env.CONTACT_FROM_EMAIL), hasContactToEmail: Boolean(env.CONTACT_TO_EMAIL), });