A Cloudflare Email Worker for invoices@ allowlists vendors by message.from and calls setReject() for the rest, yet one vendor's invoices bounce; why, and how do you fix it?
answer
- two senders on one message
- envelope versus header
- billing platforms use bounce addresses
- compare the From header's domain
- log first, then enforce
basics
~20 smessage.from is the envelope MAIL FROM, and a vendor's billing platform often sends with its own bounce address, so an exact allowlist on it rejects real invoices. Compare the domain of the From header instead, and log decisions before enforcing setReject().
solid answer
~50 sIn an Email Worker, `message.from` and `message.to` are **envelope** addresses, while the address a mail client shows is the `From` header, read with `message.headers.get("from")`. Billing and mailing platforms often put a per-message bounce address in the envelope, such as `[email protected]`, while the header still says `[email protected]`. An exact allowlist on `message.from` fails, and `setReject()` sends the vendor a permanent SMTP error, so the invoice bounces. The fix is to parse the `From` header, take its lowercased domain, and allowlist domains, not exact strings. Before enforcing, run the check in log-only mode, so `console.log` records each decision while every message is still forwarded. Since July 2025 Email Routing has already rejected mail that fails both SPF and DKIM, and it also rejects mail failing the sender's DMARC policy, which makes the header domain more trustworthy, though not proof that an invoice is genuine.
code
javascript · 24 linesconst VENDOR_DOMAINS = new Set(["vendor.example", "supplies.example"]);
const ENFORCE = false; // switch on once the logs show no real vendor failing
function headerDomain(value) {
const match = /<([^>]+)>/.exec(value ?? "");
const address = (match ? match[1] : value ?? "").trim().toLowerCase();
return address.split("@")[1] ?? "";
}
export default {
async email(message, env, ctx) {
const fromDomain = headerDomain(message.headers.get("from"));
const known = VENDOR_DOMAINS.has(fromDomain);
console.log(JSON.stringify({ envelopeFrom: message.from, fromDomain, known, enforce: ENFORCE }));
if (!known && ENFORCE) {
message.setReject("Invoices are accepted from registered vendors only");
return;
}
const headers = new Headers({ "X-Vendor-Check": known ? "pass" : "unknown" });
await message.forward("[email protected]", headers);
},
};go deeper
Recall that an Email Worker sees two senders: message.from is the envelope address, and the From header is read from message.headers.
Explain why software-sent mail carries a bounce address in the envelope, how to extract and lowercase the header domain, and what setReject() returns to the sender.
Diagnose the bounce from both senders in the logs, roll the allowlist out in log-only mode, and say what Email Routing's SPF, DKIM and DMARC rejection does and does not guarantee about the header.
Decide who owns the vendor list and what an unknown sender costs: a bounced real invoice versus a reviewer reading spam, and keep automatic payment away from anything the allowlist alone admitted.
## Two senders on every message Every email carries two sender addresses. The **envelope sender** is given in the SMTP `MAIL FROM` command, and bounces go to it. The **header sender** is the `From:` line inside the message, the one a mail client shows. The general rules for the two are taught with SMTP. What matters here is how an Email Worker exposes them: | What you want | Where an Email Worker finds it | |---|---| | envelope sender (`MAIL FROM`) | `message.from` | | envelope recipient (`RCPT TO`) | `message.to` | | `From` header, display name included | `message.headers.get("from")` | | everything else (`Subject`, `Message-ID`, `Reply-To`) | `message.headers.get(...)` | For mail typed by a person, the two senders often match. For mail sent by software they usually do not. ## Why the allowlist rejected real invoices A vendor whose invoices come from a billing platform sends them with a per-message bounce address in the envelope, for example `[email protected]`, so that platform can track bounces. The `From` header still reads `"Billing" <[email protected]>`. The Worker compared `message.from` against a list of exact vendor addresses, found no match, and called `message.setReject()`. Email Routing then answered the vendor's server with a **permanent SMTP error** carrying the reason. The invoice bounced back to the vendor and never reached `accounts@`. Two smaller faults usually sit beside it: - **exact string matching** breaks on display names (`"Billing" <...>`) and on capital letters, because addresses arrive as written; - the switch went straight to `setReject()`, so the first sign of a wrong list was a vendor's complaint. Running the same message under `wrangler dev` shows the gap at once. The local endpoint takes the envelope sender as its `from` query parameter, and Wrangler warns *Provided MAIL FROM address doesn't match the email message's "From" header* when it differs. ## Fixing the check 1. Read the `From` header with `message.headers.get("from")`, or parse `message.raw` with a MIME parser such as `postal-mime` when you also need attachments. 2. Pull the address out of any display name and **lowercase** it. 3. Compare its **domain** with a list of vendor domains, not exact addresses, since vendors add and rename sending mailboxes. 4. Log each decision, keeping both senders, to see who would be rejected. 5. Only after the logs are clean, enforce `setReject()` with a reason the vendor can act on, such as *Invoices are accepted from registered vendors only*. ## What Email Routing has already checked An allowlist on the `From` header is only as good as that header, so know what happens before the Worker runs: - mail that passes **neither SPF nor DKIM** is rejected at receipt, a requirement in force since 3 July 2025; - mail that fails authentication **according to the sender's DMARC policy** is rejected; - mail from IP addresses on a **Realtime Block List** is rejected; - the **Activity log** shows each routed message's SPF, DKIM and DMARC results. So a `From` domain that publishes an enforcing DMARC policy is hard to forge by the time the Worker sees it. A domain with no DMARC, or with `p=none`, gets no such protection. Treat the allowlist as a routing decision, not as proof. An invoice from an unexpected sender should go to a person, never to automatic payment. ## Rolling the rule out safely - Ship the Worker with enforcement **off**. It logs `{ envelopeFrom, fromDomain, known }` for each message and forwards everything, tagging unknown senders with an `X-` header that the accounts team can filter on. - Read the logs in Workers Logs, or stream them while testing, for a few billing cycles. - Add the vendor domains the logs reveal, then turn enforcement on. - Keep forwarding unknown senders to a reviewer for a while instead of rejecting them, if losing an invoice costs more than reading some spam. ## Filing the attachments The same Worker usually stores invoice PDFs. Parse `message.raw` with `postal-mime` and write each attachment through an R2 binding. Messages above **25 MiB** are rejected before the Worker runs. A large parse counts against the Workers CPU limit, and on the Workers Free plan it can fail with `EXCEEDED_CPU`, which is one more reason to forward first and store second.
- Can an Email Worker trust the From header it allowlists?Partly. Before the Worker runs, Email Routing rejects mail that fails both SPF and DKIM, or fails the sender's DMARC policy. A domain with an enforcing DMARC policy is therefore hard to forge. A domain with no DMARC record, or with `p=none`, has no such protection, so the allowlist routes mail but does not prove an invoice is genuine.
- How do you learn which real senders a new allowlist would reject before turning it on?Deploy it in log-only mode. It logs the envelope sender, the header domain and the verdict for each message, then forwards everything, tagging unknown senders with an `X-` header. Review the logs over a billing cycle, add the missing vendor domains, and only then let it call `setReject()`.
- What does a vendor see when the invoices@ Worker calls message.setReject()?Email Routing gives the vendor's sending server a permanent SMTP error with the Worker's reason. That server returns a bounce to whoever sent the invoice. Nothing reaches `accounts@`, so the reason should tell the vendor what to do next.
A company's invoices arrive in envelopes from its mailing house, whose return address is printed on the outside, while the letterhead inside still names the company. A mailroom that checks only the return address turns genuine invoices away; it has to open the envelope and read the letterhead, remembering that a letterhead alone can be forged.
- message.from is the From address the recipient's mail client displays.
- Email Routing hands unauthenticated mail to the Worker, so the Worker must verify SPF itself.
- A From header from an allowlisted domain proves the invoice is genuine.
- Comparing the raw From header to an exact address handles display names and capitals.
- setReject() can be switched on at once, since a rejected vendor simply resends.
Assumes Email Routing as of 2026-10-10; since 3 July 2025 inbound mail must pass SPF or DKIM, while before that unauthenticated mail was still forwarded.