Test email routing locally

Test email routing Workers locally using wrangler dev with simulated incoming emails

Last updated Oct 15, 2026 Test email routing behavior locally using wrangler dev to simulate incoming emails and verify your routing logic before deploying.

Prerequisites

  1. Sign up for a Cloudflare account ↗︎.
  2. Install Node.js ↗︎.

<details>

<summary>

Node.js version manager

</summary>

Use <a href="https://viteplus.dev/guide/env">Vite+ (<code>vp env</code>) ↗︎</a> to install and manage Node.js versions.

For supported Node.js versions, refer to the <a href="https://developers.cloudflare.com/workers/wrangler/install-and-update/#install-wrangler">Wrangler system requirements</a>.

</details>

Configuration

Configure your Wrangler file:

{
    "name": "email-routing-worker",
    // Set this to today's date
    "compatibility_date": "2026-10-27",
}
name = "email-routing-worker"
# Set this to today's date
compatibility_date = "2026-10-27"

Basic routing worker

import * as PostalMime from "postal-mime";

export default {
    async email(message, env, ctx) {
        // Parse the raw email message
        const parser = new PostalMime.default();
        const rawEmail = new Response(message.raw);
        const email = await parser.parse(await rawEmail.arrayBuffer());

        console.log("Received email:", {
            from: message.from,
            to: message.to,
            subject: email.subject,
            text: email.text,
            html: email.html,
        });

        // Route based on recipient
        if (message.to.includes("support@")) {
            await message.forward("support-team@example.com");
        } else {
            await message.forward("general@example.com");
        }
    },
};

Send a test email

Start your development server:

npx wrangler dev

Send a test email using the local endpoint. The request body must be a raw email message in RFC 5322 ↗︎ format, and the message must include a Message-ID header:

curl --request POST 'http://localhost:8787/cdn-cgi/local/email' \
  --url-query 'from=sender@example.com' \
  --url-query 'to=recipient@example.com' \
  --data-raw 'Received: from smtp.example.com (127.0.0.1)
        by cloudflare-email.com (unknown) id 4fwwffRXOpyR
        for <recipient@example.com>; Tue, 27 Aug 2024 15:50:20 +0000
From: "John" <sender@example.com>
Reply-To: sender@example.com
To: recipient@example.com
Subject: Testing Email Workers Local Dev
Content-Type: text/html; charset="windows-1252"
X-Mailer: Curl
Date: Tue, 27 Aug 2024 08:49:44 -0700
Message-ID: <6114391943504294873000@ZSH-GHOSTTY>

Hi there'

The console shows the parsed email structure:

{
    "headers": [
        {
            "key": "received",
            "value": "from smtp.example.com (127.0.0.1) by cloudflare-email.com (unknown) id 4fwwffRXOpyR for <recipient@example.com>; Tue, 27 Aug 2024 15:50:20 +0000"
        },
        { "key": "from", "value": "\"John\" <sender@example.com>" },
        { "key": "reply-to", "value": "sender@example.com" },
        { "key": "to", "value": "recipient@example.com" },
        { "key": "subject", "value": "Testing Email Workers Local Dev" },
        { "key": "content-type", "value": "text/html; charset=\"windows-1252\"" },
        { "key": "x-mailer", "value": "Curl" },
        { "key": "date", "value": "Tue, 27 Aug 2024 08:49:44 -0700" },
        {
            "key": "message-id",
            "value": "<6114391943504294873000@ZSH-GHOSTTY>"
        }
    ],
    "from": { "address": "sender@example.com", "name": "John" },
    "to": [{ "address": "recipient@example.com", "name": "" }],
    "replyTo": [{ "address": "sender@example.com", "name": "" }],
    "subject": "Testing Email Workers Local Dev",
    "messageId": "<6114391943504294873000@ZSH-GHOSTTY>",
    "date": "2024-08-27T15:49:44.000Z",
    "html": "Hi there\n",
    "attachments": []
}

Next steps

Was this helpful?

YesNo

On this page

Test email routing locally - 图1Docs

{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/email-service/observability/local-development/routing/#page","headline":"Test email routing locally","description":"Test Email Service routing Workers locally with simulated incoming emails.","url":"https://developers.cloudflare.com/email-service/observability/local-development/routing/","inLanguage":"en","image":"https://developers.cloudflare.com/email-service/observability/local-development/routing/og.png?v=17d6ff7e7dfc8640","dateModified":"2026-10-15","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}


description: Test Email Service sending Workers locally with simulated delivery. title: Test email sending locally image: https://developers.cloudflare.com/email-service/observability/local-development/sending/og.png?v=76c071adeb4a172d


Skip to content

Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/email-service/llms.txt
Use this file to discover all available pages before exploring further.