Guide · JavaScript & TypeScript
How to send email with Cloudflare Workers
Send email from a Cloudflare Worker over HTTPS. Workers can't open SMTP connections on port 25, so an HTTP email API is the simplest route.
Last reviewed November 2026
1. Set up
You need a Stack Emails API key (create one free) and a verified sending domain.
Store the key as a secret:
npx wrangler secret put STACKEMAILS_API_KEY
2. Send your first email
export interface Env { STACKEMAILS_API_KEY: string }
export default {
async fetch(request: Request, env: Env): Promise<Response> {
if (request.method !== "POST") return new Response("Method not allowed", { status: 405 });
const { email } = await request.json<{ email: string }>();
const res = await fetch("https://stackemails.com/v1/emails", {
method: "POST",
headers: { Authorization: `Bearer ${env.STACKEMAILS_API_KEY}`, "Content-Type": "application/json" },
body: JSON.stringify({
from: "Acme <hello@mail.acme.com>",
to: email,
subject: "You're on the list",
html: "<p>Thanks for joining the waitlist.</p>",
}),
});
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).
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.