Sub topic 8
The filtering stack: from connection checks to machine learning
A receiving mailbox provider does not make one decision about your message. It runs the message through a series of filters, from cheap checks that run before any content arrives to expensive models that read the message and weigh it against a particular recipient's history. This chapter walks through that stack in order, shows what each layer examines, and explains how the layers combine into one outcome.
This chapter assumes you know the three placement outcomes (inbox, spam folder, rejection). It also assumes you can read SPF, DKIM, and DMARC TXT records and have a rough idea of what sender reputation is. Those topics get their own chapters. Here they appear only as inputs to the filters.
Why mail is filtered in layers
Providers receive billions of messages a day. Most of them are spam, so running every message through the most expensive analysis would be wasteful. Providers therefore order their checks by cost:
- Checks that need only the connecting IP address run first. They take microseconds.
- Checks that need the SMTP envelope and headers run next.
- Checks that need the full message body come after that.
- Per-recipient models run last, on the messages that survived everything else.
Two consequences follow. First, a message that fails an early layer is usually rejected outright, so later layers never see it. Second, a strong signal in one layer rarely rescues a weak one in an earlier layer. A perfectly written email from a blocklisted IP is still refused.
Layer 1: connection-level checks
The first decision happens during the SMTP conversation, before the receiver has accepted any message data. The receiver knows only your IP address and what your server says in the EHLO greeting. It typically checks:
- IP reputation and blocklists. The receiver looks up your IP in its own history and in third-party blocklists (DNSBLs). A DNSBL lookup is a DNS query: the IP octets are reversed and the list's zone name is appended. A listed address returns a
127.0.0.xanswer. - Reverse DNS. Your IP should have a PTR record, and the hostname it names should resolve back to the same IP (forward-confirmed reverse DNS). Missing or generic PTR names such as
ip-203-0-113-9.example-isp.netcount against you. - EHLO hostname. A hostname that does not resolve, or that claims to be someone else's domain, is suspicious.
- TLS. Large providers expect encrypted delivery and may penalize or refuse plaintext connections.
- Connection behavior. Many parallel connections, very fast message bursts, or many recipients that do not exist (a sign of directory harvesting) trigger rate limits.
- Greylisting. Some receivers answer a first-time sender with a temporary
4xxcode. A real mail server retries after a delay. Simple spam tools do not.
Failures here appear as SMTP errors, so you see them directly in your logs. A 4xx code means "try again later" and a 5xx code means "permanent refusal".
Layer 2: authentication results
Once the message headers arrive, the receiver evaluates SPF, DKIM, and DMARC. The result is a set of facts: which domain passed, and whether that domain aligns with the visible From: address.
Authentication rarely makes a message good. It makes the message attributable. The receiver now knows which domain to credit or blame, so reputation lookups in the next layer attach to a real identity instead of a forgeable one. A message that fails DMARC for a domain publishing p=reject is refused or discarded at this layer, and under p=quarantine it goes to spam.
Layer 3: reputation lookups
With the sending IP and the authenticated domains established, the receiver pulls their histories. These histories are built from complaint rates, bounce rates, spam trap hits, and engagement over time. They usually produce a score or a category (for example "good", "neutral", "poor") for the IP and for each domain.
Reputation acts in two ways. It can be a gate: a very poor reputation leads to a 5xx rejection or a throttle (421 or 451). It can also be a weight that shifts every later layer. A trusted sender gets the benefit of the doubt on borderline content. A sender with a poor record gets scrutiny on everything.
Layer 4: content and link analysis
Content analysis reads what the message says and contains. Rule-based filters such as SpamAssassin assign points to patterns, and a message crosses into spam when its total passes a threshold. Typical inputs are:
- URLs and their domains, checked against URI blocklists (URIBLs) of domains seen in spam or phishing. Link shorteners and mismatches between the visible link text and the real destination raise suspicion.
- Attachments, especially executables, macro-enabled documents, and password-protected archives.
- Structure, such as an HTML-only message with no text part, hidden text, or one large image with little text.
- Wording that matches known scam or phishing templates.
- Fingerprints. A message nearly identical to one that thousands of recipients already reported is treated as known bad. Providers compare fuzzy hashes of the body for this.
Content is a supporting signal. It rarely decides the outcome alone for a sender with a good record, but it often breaks ties for a sender in the middle.
Layer 5: machine-learning and per-recipient models
The last layer is a learned model, not a list of rules. Large providers train classifiers on enormous volumes of labeled mail. The labels come from user actions: "report spam", "not spam", delete without reading, reply, move to a folder. The model takes hundreds of features, including the outputs of all the earlier layers, and produces a probability that this message is unwanted.
The key difference from the earlier layers is personalization. The same message can land in the inbox for one recipient and in the spam folder for another. The model sees that recipient's own behavior: whether they have opened your mail, replied to you, or deleted your last ten messages unread. Engagement is covered in its own chapter. Here it is enough to know that it enters the stack mostly through this layer.
How the layers combine
The layers use two kinds of logic. Some are gates, which give a binary answer and stop the pipeline. Others are weights, which add to a running risk estimate that is compared against a threshold at the end. Real systems mix both, as the diagram shows.
The sketch below imitates this structure. It is a teaching model, not a real provider's logic. The weights and thresholds are invented.
from dataclasses import dataclass
@dataclass
class Message:
ip_listed: bool
spf_aligned: bool
dkim_aligned: bool
dmarc_policy: str # "none", "quarantine", or "reject"
domain_reputation: float # 0.0 (bad) to 1.0 (good)
content_score: float # 0.0 (clean) to 1.0 (spammy)
recipient_engagement: float # 0.0 (ignores sender) to 1.0 (engages)
def decide(m: Message) -> str:
# Gate 1: connection-level blocklist check
if m.ip_listed:
return "reject: 550 listed on blocklist"
# Gate 2: DMARC passes if SPF or DKIM passes and aligns
dmarc_pass = m.spf_aligned or m.dkim_aligned
if not dmarc_pass:
if m.dmarc_policy == "reject":
return "reject: 550 DMARC policy"
if m.dmarc_policy == "quarantine":
return "spam folder: DMARC quarantine"
# Weighted layers: reputation, content, and engagement add up to a risk score
risk = (
0.5 * (1 - m.domain_reputation)
+ 0.3 * m.content_score
+ 0.2 * (1 - m.recipient_engagement)
)
return "spam folder" if risk > 0.6 else "inbox"
good = Message(False, True, True, "none", 0.9, 0.2, 0.7)
weak = Message(False, True, True, "none", 0.4, 0.5, 0.1)
listed = Message(True, True, True, "none", 0.9, 0.2, 0.7)
print(decide(good)) # inbox
print(decide(weak)) # spam folder
print(decide(listed)) # reject: 550 listed on blocklistWhat each layer looks like to you
The layer that stopped a message determines what evidence you can see. This table maps layers to where the failure shows up.
| Layer | What it checks | Typical failure signal | Where you see it |
|---|---|---|---|
| Connection | IP, blocklists, PTR, EHLO, rate | `421`, `550` during SMTP | Your sending logs |
| Authentication | SPF, DKIM, DMARC alignment | `550` with DMARC text, or spam placement | Logs, DMARC reports |
| Reputation | IP and domain history | Throttling, spam folder for all recipients | Postmaster tools |
| Content and links | Body, URLs, attachments | Spam placement tied to a specific campaign | Seed tests, A/B comparison |
| Machine learning | Recipient-specific behavior | Spam folder for some recipients only | Engagement segments |
Common mistakes when reasoning about filters
Treating "accepted" as "delivered to the inbox"
A 250 OK reply means the receiving server took responsibility for the message. It does not say where the message went. Layers 3 to 5 often run after acceptance, and a message accepted at the SMTP level can still land in the spam folder. Never use the SMTP reply alone as a placement metric.
Fixing content when the problem is earlier
Teams often rewrite subject lines when the real cause is a blocklisted IP or a broken DKIM signature. Work through the layers in order. Rewording cannot fix a problem at an earlier gate.
Expecting one global verdict
Every provider runs its own stack with its own thresholds. A message can reach the inbox at one provider and the spam folder at another. The same holds within one provider, because of the per-recipient model.
Assuming filters are static
Models are retrained continuously, and blocklists add and remove entries within hours. A configuration that worked last quarter can degrade without any change on your side, because the surrounding traffic changed.
Summary of the working model
- Filters run from cheap to expensive, and early gates stop messages before later layers run.
- Authentication ties the message to an identity so that reputation can attach to it.
- Reputation both gates and weights every later decision.
- Content and links are supporting signals that tip borderline messages.
- Machine-learning models personalize the final decision using each recipient's behavior.
- The SMTP reply code only tells you about the gates that ran during the connection.
Exercise
Your logs show every message to one provider returning 250 OK, yet recipients there say your mail is in the spam folder. Which layers could be responsible, and which layer can you rule out?
Show answer
The connection layer is ruled out as the cause, because it would have produced a 4xx or 5xx reply during SMTP. The cause lies in layers that decide placement after acceptance: authentication results (for example a DMARC quarantine policy), reputation, content and links, or the machine-learning model. Check authentication first because it is the earliest of these and the easiest to verify, then look at postmaster tool reputation data, then at content and engagement.
Exercise
In the Python sketch, a message has ip_listed=False, dmarc_policy="reject", spf_aligned=False, dkim_aligned=False, and an excellent domain_reputation of 1.0. What does decide return, and what does this show about gates versus weights?
Show answer
It returns "reject: 550 DMARC policy". The DMARC gate stops the pipeline before the weighted score is computed, so the excellent reputation never gets a chance to matter. Gates are binary and final. Weights only matter for messages that pass every gate.