惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 【当耐特】
WordPress大学
WordPress大学
T
The Exploit Database - CXSecurity.com
博客园_首页
MyScale Blog
MyScale Blog
The Cloudflare Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
美团技术团队
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
M
MIT News - Artificial intelligence
Microsoft Security Blog
Microsoft Security Blog
F
Full Disclosure
V
V2EX
博客园 - Franky
博客园 - 三生石上(FineUI控件)
Hugging Face - Blog
Hugging Face - Blog
P
Proofpoint News Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
SecWiki News
SecWiki News
N
Netflix TechBlog - Medium
S
Secure Thoughts
酷 壳 – CoolShell
酷 壳 – CoolShell
Hacker News: Ask HN
Hacker News: Ask HN
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Webroot Blog
Webroot Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Martin Fowler
Martin Fowler
PCI Perspectives
PCI Perspectives
S
Security @ Cisco Blogs
Recorded Future
Recorded Future
Help Net Security
Help Net Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
AI
AI
Microsoft Azure Blog
Microsoft Azure Blog
K
Kaspersky official blog
G
GRAHAM CLULEY
H
Hackread – Cybersecurity News, Data Breaches, AI and More
C
CERT Recently Published Vulnerability Notes
U
Unit 42
T
Tor Project blog
Cloudbric
Cloudbric
Hacker News - Newest:
Hacker News - Newest: "LLM"
MongoDB | Blog
MongoDB | Blog
GbyAI
GbyAI
T
The Blog of Author Tim Ferriss
Security Latest
Security Latest
N
News and Events Feed by Topic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO

Flavio Copes

Better Auth: an introduction Cloudflare Artifacts: Git storage built for AI agents Workers Cache: a cache in front of your Cloudflare Worker Cloudflare Drop: drag a folder, get a live site Temporary Cloudflare accounts: agents can now deploy without signing up Moondream 3.1 on Workers AI: fast vision at the edge inferencecost.dev: what will AI inference cost you at 10k users? Sitebase: all the features your website needs, in one place StackPlan: figure out where to deploy your app, and what it How the Cloudflare Pages build cache works The Summer of Code How I generate an Open Graph image for every post New: 90 free tools for developers How I added search to my static site with Pagefind How to rebuild a Cloudflare site on a schedule Cloudflare Turnstile: stop bots without annoying CAPTCHAs Cloudflare Workers: secrets and environments Cloudflare Workers observability: logs and traces Cloudflare Analytics Engine: store and query metrics The AI Workshop (July 2026 cohort) Cloudflare Cron Triggers: run a Worker on a schedule Cloudflare Durable Objects: state that lives in one place Cloudflare Queues: run work in the background Cloudflare R2: object storage without egress fees Cloudflare KV: a key-value store for your Workers Cloudflare D1: a SQL database for your Workers Serving a website with Cloudflare Workers static assets Wrangler: the Cloudflare Workers command line tool Executor: one gateway to connect your AI agent to every tool Cloudflare Workers: your first serverless function Vercel eve: an open framework for building AI agents Flue: the open framework for building AI agents Val Town: write and deploy code in seconds A hands-on guide to The Agency, a collection of AI agents The AI Workshop (June 2026 cohort) The AI Workshop (May 2026 cohort) The AI Workshop (Apr 2026 cohort)
Cloudflare Email Workers: run code when an email arrives
Flavio Copes · 2026-07-03 · via Flavio Copes

By Flavio Copes

How to receive and process incoming email with Cloudflare Email Workers, parse it with postal-mime, and forward or act on it.

~~~

We usually think about sending email. But sometimes you want to receive it and do something. Turn a support email into a ticket, post it to Slack, or pull data out of an attachment.

Email Workers let you run code whenever an email arrives at an address you own. The email becomes the trigger, the same way an HTTP request triggers a normal Worker.

How it fits together

First you set up Email Routing on your domain in the Cloudflare dashboard. That’s what lets Cloudflare receive mail for you. Then you route an address to a Worker instead of forwarding it to an inbox.

While you’re in the DNS settings, it’s a good moment to check the records you need for sending mail from the domain too. I made a tool that generates and explains SPF, DKIM and DMARC records.

Once that’s done, every email to that address calls your Worker’s email handler.

The email handler

A normal Worker has a fetch handler. An Email Worker has an email handler:

export default {
  async email(message, env, ctx) {
    console.log('From:', message.from)
    console.log('To:', message.to)
    console.log('Subject:', message.headers.get('subject'))
  },
}

The message gives you the basics right away: from, to, and the headers. For the subject and other header fields, you read them from message.headers, just like a normal Headers object.

Read the body with postal-mime

The basics are easy, but the actual body of an email is messy. Real emails are MIME: multiple parts, text and HTML versions, different encodings, attachments. You don’t want to parse that by hand.

The standard tool is postal-mime. Install it:

npm install postal-mime

Then parse the raw message:

import PostalMime from 'postal-mime'

export default {
  async email(message, env, ctx) {
    const email = await PostalMime.parse(message.raw)

    console.log('Subject:', email.subject)
    console.log('Text:', email.text)
    console.log('HTML:', email.html)
    console.log('Attachments:', email.attachments)
  },
}

message.raw is the full email. PostalMime.parse turns it into a clean object with subject, text, html, and attachments ready to use.

Forwarding

The simplest action is to forward the email somewhere:

export default {
  async email(message, env, ctx) {
    await message.forward('[email protected]')
  },
}

The destination has to be a verified address in your Email Routing setup.

A real example: route by recipient

Here’s a pattern I like. One Worker handles several addresses and routes based on who the mail was sent to:

export default {
  async email(message, env, ctx) {
    if (message.to.includes('support@')) {
      await message.forward('[email protected]')
    } else if (message.to.includes('sales@')) {
      await message.forward('[email protected]')
    } else {
      await message.forward('[email protected]')
    }
  },
}

Swap the forwards for whatever you need: write a row to D1, drop a job on a Queue, post to Slack.

Test it locally

You don’t need to send real email to test. Run wrangler dev, and it exposes a local endpoint you can POST a raw email to:

curl -X POST 'http://localhost:8787/cdn-cgi/handler/email' \
  --url-query '[email protected]' \
  --url-query '[email protected]' \
  --header 'Content-Type: application/json' \
  --data-raw 'From: [email protected]
To: [email protected]
Subject: Testing
Message-ID: <[email protected]>

Hello there'

That fires your email handler with the message, so you can build and debug without sending anything for real.

Where this shines

Email Workers turn your inbox into an API. Anything that arrives by email can kick off code: support automation, parsing receipts, handling replies, catching bounces.

Pair it with the rest of the platform, a Queue for the slow work, D1 to store results, and you’ve got a real email pipeline with no mail server to run. The full reference is in the Email Workers docs.

~~~

Related posts about cloudflare: