Filter Cloudflare email by subject or sender
Email Routing rules match only the address. To send mail to other inboxes by subject or sender, use a short Email Worker. Here is the code and its limits.
On this page · 5 sections
What rules can and can't do
A routing rule takes one address (or the catch-all) and forwards its mail, sends it to a Worker, or drops it. [2] It never looks inside the message. To send invoices to your bookkeeper, mail from one client to a shared inbox, or anything marked urgent to your phone, send the address to an Email Worker and decide there. The Worker reads the subject with message.headers.get("subject") and the sender with message.from, then calls message.forward() with the inbox it picked. [1]
Set it up
- Add every inbox you will forward to under Email > Email Routing > Destination addresses, and click the link in each verification email. Forwarding only works to verified addresses. [1][2]
- In the Cloudflare dashboard, go to Workers & Pages, create a Worker, and replace its code with the example below, using your own words and inboxes.
- Open your domain, then Email > Email Routing > Routing rules. Edit the address (or the catch-all), set the action to Send to a Worker, and pick the Worker.
- Send a test for each rule: one with a matching subject, one from a matching sender, and one that matches nothing. Each should land in its own inbox.
// Checked top to bottom; the first match wins.
const RULES = [
{ subject: "invoice", to: "books@example.com" },
{ subject: "urgent", to: "phone@example.com" },
{ senderDomain: "bigclient.com", to: "team@example.com" },
];
const EVERYTHING_ELSE = "you@gmail.com";
export default {
async email(message, env, ctx) {
const subject = (message.headers.get("subject") ?? "").toLowerCase();
const domain = message.from.toLowerCase().split("@").pop();
const rule = RULES.find((r) =>
r.subject ? subject.includes(r.subject) : domain === r.senderDomain
);
// Destinations must be verified in Email Routing.
await message.forward(rule ? rule.to : EVERYTHING_ELSE);
},
};To send one kind of mail to several inboxes at once, call forward() once per inbox inside Promise.all, as Cloudflare's own example does for mail about security. [1]
What to know first
- `message.from` is the envelope sender. It is the bounce address the sending server gave, and mailing services often use one of their own there, not the address in the From line. To match the visible sender, read
message.headers.get("from")too. [1] - You can tag what you forward.
forward()takes extra headers, but only ones that start withX-; Cloudflare removes any others. AnX-Rule: invoiceheader gives the receiving inbox something to filter on. [1] - Words in a subject are easy to fake. Anyone can write invoice in a subject. Don't send mail on to a place that trusts it more because of a word.
- The rules live in the code. Each new rule means editing and deploying the Worker again.
To refuse some senders outright, see Block a sender with a Cloudflare Email Worker. To answer mail automatically from the same Worker, see Auto-reply with a Cloudflare Email Worker. If mail doesn't arrive at all, start with Cloudflare Email Routing not working.
Filters in Ayoh
Ayoh runs in your own Cloudflare account and its Worker already receives your domain's mail, so you sort it with rules instead of code. Under Settings → Filters & rules, a rule reads When the From, To, Subject or body contains what you type, or the AI says yes to a question like Is this a receipt?, Then file it in a folder, pin it or mark it read. See Filters & rules. Setup is in Getting started.
Sources
Read November 21, 2026.
- [1] Cloudflare, Workers API for Email Routing (opens in a new tab), updated November 17, 2026: reading the subject and
message.from, the examples that forward by recipient and by words in the subject,forward()to verified destination addresses, and itsX-header rule. - [2] Cloudflare, Email addresses (opens in a new tab): the routing rule actions, including Send to a Worker and Drop.