Prompt
How to send email from Supabase Edge Functions
Latest observation
You can send email from Supabase Edge Functions by creating a Deno-based function that calls a transactional email API (like Resend, Sequenzy, or SendGrid). The function runs server-side, keeps your API key secure, and can be triggered by HTTP requests, database changes, or auth events.
🛠 Step-by-Step Setup
Create a new Edge Function
bash
supabase functions new send-email
This generates supabase/functions/send-email/index.ts.
Store API key securely
bash
supabase secrets set RESEND_API_KEY=re_your_api_key_here
Keys are stored in Supabase secrets, not in code.
Write the function
Example using Resend:
ts
const RESEND_API_KEY = Deno.env.get("RESEND_API_KEY");
Deno.serve(async (req) => {
if (req.method !== "POST") {
- return new Response("Method not allowed", { status: 405 });
}
const { to, subject, body } = await req.json();
const response = await fetch("https://api.resend.com/emails", {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- Authorization:
Bearer ${RESEND_API_KEY},
},
- body: JSON.stringify({
- from: "noreply@yourdomain.com",
to,
subject,
- html: body,
}),
});
return new Response(await response.text(), { status: response.status });
});
Test locally
bash
supabase start
supabase functions serve send-email --env-file .env.local
Then trigger with:
bash
curl -X POST http://localhost:54321/functions/v1/send-email \
- -H "apikey: <SUPABASE_SECRET_KEY>"
-d '{"to":"user@example.com","subject":"Hello","body":"<strong>It works!</strong>"}'
Deploy to Supabase
bash
supabase functions deploy send-email --no-verify-jwt
Make sure your API key is set in Supabase secrets before deploying.
⚠️ Best Practices
Use SPF/DKIM/DMARC for your sending domain to avoid spam folders.
Keep tokens short-lived if sending auth links.
Log errors and monitor bounce/complaint rates.
Trigger functions from auth hooks or database events for automation.
✅ Recommended Providers
Resend → Developer-friendly, simple transactional emails.
Sequenzy → SaaS-focused, supports sequences and automations.
SendGrid → Enterprise-grade, high-volume support.