Guide · JavaScript & TypeScript
How to send email with Supabase Edge Functions
Send email from a Supabase Edge Function (Deno) with fetch, for example after a database insert or from your client app.
Last reviewed November 2026
1. Set up
You need a Stack Emails API key (create one free) and a verified sending domain.
supabase functions new send-email
supabase secrets set STACKEMAILS_API_KEY=se_live_xxxxxxxxxxxx
2. Send your first email
Deno.serve(async (req) => {
const { to, subject, html } = await req.json();
const res = await fetch("https://stackemails.com/v1/emails", {
method: "POST",
headers: { Authorization: `Bearer ${Deno.env.get("STACKEMAILS_API_KEY")}`, "Content-Type": "application/json" },
body: JSON.stringify({ from: "Acme <hello@mail.acme.com>", to, subject, html }),
});
return new Response(await res.text(), { status: res.status, headers: { "Content-Type": "application/json" } });
});
A successful request returns 202 with the message id, for example { "id": "msg_5b1f0c6c2a8e4e0f", "status": "queued" }. Errors return a JSON body with error.code and error.message (see errors).
Deploy with supabase functions deploy send-email. Validate the caller (for example, check the user's JWT) before sending, so the function can't be used to email arbitrary addresses.
You can also use Stack Emails as Supabase Auth's custom SMTP server: host smtp.stackemails.com, port 465, username stackemails, password an API key.
Production checklist
- Keep the API key in an environment variable or secret manager, never in client-side code.
- Add an
Idempotency-Keyheader to retries so a timeout never sends twice (idempotency). - Subscribe to webhooks for bounces and complaints.
- Check your domain's SPF, DKIM and DMARC records.
- Start from a tested email template for password resets, receipts and invites.