Urgent.News

What's breaking now, across thousands of outlets.

Tech

Cloudflare Email Worker inbox: store every message in D1 in 30 lines

Originally published at guushu.com/notes . I keep the original updated, so this copy may lag. Email Routing forwards a message and keeps nothing. If you want a copy you can search later, point the routing rule at a Worker, write the message to D1, and forward it on. That's a Cloudflare Email Worker inbox, about 30 lines of TypeScript. Three self-hosted projects already ship that shape, two open…

Cloudflare Email Worker inbox utilizes 30 lines of TypeScript to store every incoming message in D1. This setup allows for the preservation of messages for later reference. Instead of forwarding the email to a mailbox, the message is written to D1 (and R2 for attachments) by the worker. A small web UI is then used to read the stored messages.

The Email Routing system forwards messages, but it does not have a built-in mailbox. This is because Cloudflare's focus is on the edge, not storage. Email Routing accepts the SMTP session, applies routing rules, and then hands the bytes to a destination, which in this case is a Worker. The email() method within the worker exports a function that processes the incoming message.

It reads the raw MIME data, parses it, and then stores the message details in D1 using an SQL insert statement. The worker also forwards the message to a verified destination. It's important to note that message.raw is a ReadableStream, not a string, so it must be wrapped in a Response object before it can be read. The forwarding of the message is limited to verified destinations, and unverified addresses will result in a bounce.

The setup requires a small web UI to read the stored messages. The configuration is done through the Wrangler CLI, and the routing rule is set up in the Cloudflare dashboard.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

A Button That Sends Command+Shift+3

My iPad lives on a stand most of the day. Videos, presentations, docs, whatever the work needs. That is fine until I want a capture of whatever is on the screen.

More from Monday 28 September →