Cloudflare changelogs | Developer platform

Cloudflare changelogs for Developer platform products

马上订阅 Cloudflare changelogs | Developer platform RSS 更新: https://developers.cloudflare.com/changelog/rss/developer-platform.xml

Email Routing - Local development support for Email Workers

2025年4月8日 08:00
Email Routing

Email Workers enables developers to programmatically take action on anything that hits their email inbox. If you're building with Email Workers, you can now test the behavior of an Email Worker script, receiving, replying and sending emails in your local environment using wrangler dev.

Below is an example that shows you how you can receive messages using the email() handler and parse them using postal-mime:

import * as PostalMime from "postal-mime";
export default {
async email(message, env, ctx) {
const parser = new PostalMime.default();
const rawEmail = new Response(message.raw);
const email = await parser.parse(await rawEmail.arrayBuffer());
console.log(email);
},
};

Now when you run npx wrangler dev, wrangler will expose a local /cdn-cgi/handler/email endpoint that you can POST email messages to and trigger your Worker's email() handler:

curl -X POST 'http://localhost:8787/cdn-cgi/handler/email' \
--url-query 'from=sender@example.com' \
--url-query 'to=recipient@example.com' \
--header 'Content-Type: application/json' \
--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...

剩余内容已隐藏

查看完整文章以阅读更多