Block a sender with a Cloudflare Email Worker
Email Routing has no block list, but an Email Worker can refuse mail from chosen senders or domains with message.setReject(). Here is the code and its limits.
On this page · 5 sections
What it does
A routing rule forwards mail for an address, sends it to a Worker, or drops it. [2] It can't look at who sent it. To keep one person or one domain out, send the address to an Email Worker and call message.setReject() for them. Cloudflare then refuses the message with a permanent SMTP error, so the sending server gets a bounce and nothing reaches your inbox. [1]
Set it up
- In the Cloudflare dashboard, go to Workers & Pages, create a Worker, and replace its code with the example below. Put your own senders in the lists and your own inbox in
forward(). - 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 message from an address you blocked. It should bounce back to you with your reason, and mail from anyone else should still arrive.
const BLOCKED_ADDRESSES = ["pest@example.com"];
const BLOCKED_DOMAINS = ["spam.example"];
export default {
async email(message, env, ctx) {
const sender = message.from.toLowerCase();
const domain = sender.split("@").pop();
if (
BLOCKED_ADDRESSES.includes(sender) ||
BLOCKED_DOMAINS.some((d) => domain === d || domain.endsWith("." + d))
) {
message.setReject("Not accepted at this address");
return;
}
// A verified destination address.
await message.forward("you@gmail.com");
},
};The reason you pass to setReject() goes back to the sending server with the refusal. Keep it short and don't put anything private in it. The same check can look at other parts of the message: Cloudflare's own examples refuse mail by words in the subject, and anything over 25 MiB with message.rawSize. [1]
What to know first
- The sender it checks is the envelope sender.
message.fromis the bounce address the sending server gave, and newsletters and mailing services often use one of their own there, not the address in the From line. If a block doesn't catch a sender, read the From header withmessage.headers.get("from")as well. [1] - A refusal is a bounce. The sender is told the mail was not accepted. A rule's Drop action deletes mail without a bounce, but it applies to everyone who writes to that address, not to one sender. [2]
- The sender's own address can be forged. Cloudflare checks DMARC before your Worker runs and adds an Authentication-Results header from
mx.cloudflare.net. Read the first one if you also want to refuse mail that fails DMARC; later ones may come from the sender and shouldn't be trusted. [1] - The list lives in the code. Each new sender means editing and deploying the Worker again.
To answer mail automatically from the same Worker, see Auto-reply with a Cloudflare Email Worker. To send to several inboxes, see Forward one Cloudflare email address to several inboxes. If mail doesn't arrive at all, start with Cloudflare Email Routing not working.
Blocking in Ayoh
Ayoh runs in your own Cloudflare account and its Worker already receives your domain's mail, so you block people from the message itself: More > Block sender or Block domain. Their mail goes straight to Trash, read and unannounced, and the list is under Settings → Filters → Blocked senders, where you can take one off again without touching code. See Mail. Setup is in Getting started.
Sources
Read November 19, 2026.
- [1] Cloudflare, Workers API for Email Routing (opens in a new tab), updated November 17, 2026:
setReject()and its permanent SMTP error, the reject examples by sender domain, subject and size,message.fromas the envelope sender, and the Authentication-Results header. - [2] Cloudflare, Email addresses (opens in a new tab): the routing rule actions, including Send to a Worker and Drop, which deletes matching mail without routing it.