|
| 1 | +--- |
| 2 | +title: Local development support for Email Workers |
| 3 | +description: Developers can now use wrangler to do local development for Email Workers. |
| 4 | +date: 2025-04-04T17:00:00Z |
| 5 | +--- |
| 6 | + |
| 7 | +Developers can now test the behavior of an Email Worker script, receiving, replying and sending emails in your local environment using `wrangler dev`. |
| 8 | + |
| 9 | +In this example we receive messages using the `email()` handler and parse them using [postal-mime](https://www.npmjs.com/package/postal-mime): |
| 10 | + |
| 11 | +```ts |
| 12 | +import * as PostalMime from 'postal-mime'; |
| 13 | + |
| 14 | +export default { |
| 15 | + async email(message, env, ctx) { |
| 16 | + const parser = new PostalMime.default(); |
| 17 | + const rawEmail = new Response(message.raw); |
| 18 | + const email = await parser.parse(await rawEmail.arrayBuffer()); |
| 19 | + console.log(email); |
| 20 | + }, |
| 21 | +}; |
| 22 | + |
| 23 | +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: |
| 24 | + |
| 25 | +```bash |
| 26 | +curl -X POST 'http://localhost:8787/cdn-cgi/handler/email' \ |
| 27 | + |
| 28 | + |
| 29 | + --header 'Content-Type: application/json' \ |
| 30 | + --data-raw 'Received: from smtp.example.com (127.0.0.1) |
| 31 | + by cloudflare-email.com (unknown) id 4fwwffRXOpyR |
| 32 | + for <[email protected]>; Tue, 27 Aug 2024 15:50:20 +0000 |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +Subject: Testing Email Workers Local Dev |
| 37 | +Content-Type: text/html; charset="windows-1252" |
| 38 | +X-Mailer: Curl |
| 39 | +Date: Tue, 27 Aug 2024 08:49:44 -0700 |
| 40 | +Message-ID: <6114391943504294873000@ZSH-GHOSTTY> |
| 41 | +
|
| 42 | +Hi there' |
| 43 | +``` |
| 44 | + |
| 45 | +This is what you get in the console: |
| 46 | + |
| 47 | +```json |
| 48 | +{ |
| 49 | + headers: [ |
| 50 | + { |
| 51 | + key: 'received', |
| 52 | + value: 'from smtp.example.com (127.0.0.1) by cloudflare-email.com (unknown) id 4fwwffRXOpyR for <[email protected]>; Tue, 27 Aug 2024 15:50:20 +0000' |
| 53 | + }, |
| 54 | + { key: 'from', value: '"John" <[email protected]>' }, |
| 55 | + { key: 'reply-to', value: '[email protected]' }, |
| 56 | + { key: 'to', value: '[email protected]' }, |
| 57 | + { key: 'subject', value: 'Testing Email Workers Local Dev' }, |
| 58 | + { key: 'content-type', value: 'text/html; charset="windows-1252"' }, |
| 59 | + { key: 'x-mailer', value: 'Curl' }, |
| 60 | + { key: 'date', value: 'Tue, 27 Aug 2024 08:49:44 -0700' }, |
| 61 | + { |
| 62 | + key: 'message-id', |
| 63 | + value: '<6114391943504294873000@ZSH-GHOSTTY>' |
| 64 | + } |
| 65 | + ], |
| 66 | + from: { address: '[email protected]', name: 'John' }, |
| 67 | + to: [ { address: '[email protected]', name: '' } ], |
| 68 | + replyTo: [ { address: '[email protected]', name: '' } ], |
| 69 | + subject: 'Testing Email Workers Local Dev', |
| 70 | + messageId: '<6114391943504294873000@ZSH-GHOSTTY>', |
| 71 | + date: '2024-08-27T15:49:44.000Z', |
| 72 | + html: 'Hi there\n', |
| 73 | + attachments: [] |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +Local development also works for sending, replying and forwarding emails. See [our documentation](/email-routing/email-workers/local-development/) for more information. |
0 commit comments