# Stop spam on Cloudflare Email Routing

> Email Routing forwards any mail that passes SPF or DKIM, spam included. Here is what Cloudflare already blocks, and how to filter more with an Email Worker.

Source: https://ayoh.dev/help/cloudflare-email-routing-spam (updated November 22, 2026). Part of the Ayoh user guide: https://ayoh.dev/llms.txt

## What Cloudflare already blocks

Since July 3, 2025, Email Routing forwards a message only if it is authenticated with SPF or DKIM, at least one of the two. [2] That stops mail from senders who never set either up, but it says nothing about whether you wanted the message: a spammer with their own domain can pass both. Everything that passes is forwarded, and the inbox it lands in, such as Gmail, runs its own spam filter on it.

So if spam reaches you through Email Routing, there are two places to stop more of it: the addresses you accept mail for, and an Email Worker that looks at each message before forwarding it.

## First, check the catch-all

A catch-all forwards mail for *every* address at your domain, including ones spammers make up or scrape from old lists. If most of your spam goes to addresses you never use, turn the catch-all off and add a rule for each address you do use. See [Catch-all email on Cloudflare](https://ayoh.dev/help/cloudflare-catch-all-email).

## Filter with an Email Worker

Cloudflare publishes a spam filtering example. It gives each message a score from words in the subject (*urgent*, *winner*, *free money*), a subject in capital letters, and `!!!` or `$$$`, and lowers the score for domains you trust. At a score of 2 or more it refuses the message with `setReject()`; below that it forwards it with `X-Spam-Score` and `X-Spam-Reasons` headers added. [1] Here is a shorter version of the same idea:

An Email Worker that scores, then refuses or forwards:

```
const WORDS = ["winner", "lottery", "free money", "act fast", "inheritance"];
const TRUSTED = ["yourbank.com", "bigclient.com"];
const INBOX = "you@gmail.com"; // must be a verified destination

export default {
  async email(message, env, ctx) {
    const subject = message.headers.get("subject") ?? "";
    const domain = message.from.toLowerCase().split("@").pop();
    let score = TRUSTED.includes(domain) ? -2 : 0;
    for (const w of WORDS) if (subject.toLowerCase().includes(w)) score += 1;
    if (subject.length > 10 && subject === subject.toUpperCase()) score += 1;
    if (score >= 2) {
      message.setReject("Message rejected as spam");
      return;
    }
    const headers = new Headers({ "X-Spam-Score": String(score) });
    await message.forward(INBOX, headers);
  },
};
```

1. Create a Worker under **Workers & Pages** and paste the code, with your own words, trusted domains and inbox.
2. Open your domain, then **Email** > **Email Routing** > **Routing rules**, and set the address (or the catch-all) to **Send to a Worker** with that Worker.
3. Send yourself a test with *WINNER* in the subject, and one normal message. The first should bounce back to you; the second should arrive with an `X-Spam-Score` header.

## What to know first

- **Refusing is final.** `setReject()` sends the sender a permanent error, and you never see the message. A real message that trips the filter is lost unless the sender writes again. For anything borderline, forward it with a header instead and let your inbox file it.
- **Word lists catch little.** Most spam doesn't shout *WINNER*. Cloudflare calls its example simple heuristics, and suggests Workers AI for reading the content itself. [1]
- **Headers are for your inbox's filters.** `forward()` only keeps added headers that start with `X-`. In Gmail you can't filter on a header, so to sort by score, forward high scores to a separate address instead.
- **Every change is a deploy.** New words or trusted domains mean editing and deploying the Worker again.

To refuse one sender or domain for good, see [Block a sender with a Cloudflare Email Worker](https://ayoh.dev/help/cloudflare-email-routing-block-sender). To sort mail by subject or sender instead of refusing it, see [Filter Cloudflare email by subject or sender](https://ayoh.dev/help/cloudflare-email-routing-filter).

## Spam filtering in Ayoh

Ayoh runs in your own Cloudflare account and its Worker receives your domain's mail, so it filters spam without code. Mail whose From domain says it didn't send it goes to **Spam** with the reasons written out. Borderline mail and first messages from new senders get a second opinion from the AI. **Report spam** teaches it the sender and their domain. An established conversation is never moved to Spam because of one bad score. See [Spam and dangerous attachments](https://ayoh.dev/help/mail#safety). Setup is in [Getting started](https://ayoh.dev/help/getting-started).

## Sources

Read November 22, 2026.

- [1] Cloudflare, [Spam filtering](https://developers.cloudflare.com/email-service/examples/email-routing/spam-filtering/), updated October 15, 2026: the subject keyword, capitals, punctuation and trusted-domain scoring, `setReject()` at a score of 2, forwarding with `X-Spam-Score` headers, and the optional Workers AI step.
- [2] Cloudflare changelog, [Mail authentication requirements for Email Routing](https://developers.cloudflare.com/changelog/post/2025-06-30-mail-authentication/): from July 3, 2025, mail must pass SPF or DKIM to be forwarded.
