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

推荐订阅源

L
Lohrmann on Cybersecurity
Martin Fowler
Martin Fowler
Engineering at Meta
Engineering at Meta
腾讯CDC
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Microsoft Azure Blog
Microsoft Azure Blog
G
Google Developers Blog
TaoSecurity Blog
TaoSecurity Blog
博客园_首页
Vercel News
Vercel News
Hugging Face - Blog
Hugging Face - Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Last Week in AI
Last Week in AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
The Exploit Database - CXSecurity.com
量子位
Project Zero
Project Zero
A
Arctic Wolf
小众软件
小众软件
NISL@THU
NISL@THU
C
CERT Recently Published Vulnerability Notes
有赞技术团队
有赞技术团队
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
N
News and Events Feed by Topic
宝玉的分享
宝玉的分享
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Troy Hunt's Blog
P
Privacy & Cybersecurity Law Blog
Security Latest
Security Latest
B
Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
D
DataBreaches.Net
Schneier on Security
Schneier on Security
The Hacker News
The Hacker News
K
Kaspersky official blog
C
Check Point Blog
Hacker News: Ask HN
Hacker News: Ask HN
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Webroot Blog
Webroot Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
人人都是产品经理
人人都是产品经理
AI
AI
Cisco Talos Blog
Cisco Talos Blog
MyScale Blog
MyScale Blog
Cloudbric
Cloudbric
B
Blog RSS Feed
S
Schneier on Security
P
Palo Alto Networks Blog

Flavio Copes

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: