Sub topic 9
Authenticating your sending domain
Authentication lets a receiving mail server check that a message really comes from the domain it claims. Newsletters, promotions, and transactional messages all depend on it, and a marketing stream you want to protect needs it most. This chapter assumes you know how email is sent and received, you know what an email service provider (ESP) does, and you can read a basic DNS record. It covers the three standards (SPF, DKIM, DMARC), how they combine, how to roll them out safely, and the errors that most often break them.
Why authentication matters for each email type
Email's original design lets anyone write any address in the From header. Mailbox providers therefore use authentication results as a main signal when deciding between inbox, spam folder, and rejection.
- Transactional messages such as password resets are the most damaging to lose. A reset link that lands in spam creates a support ticket.
- Newsletters and promotions are sent in bulk, so they get the most scrutiny. Since 2024, Gmail and Yahoo have required bulk senders (Gmail's threshold is about 5,000 messages a day to its users) to pass SPF and DKIM, publish a DMARC policy, and support one-click unsubscribe.
- Attackers impersonate password-reset and invoice emails because recipients trust them. Authentication is what lets you tell receivers to reject forgeries.
The three checks
| Standard | What it proves | Where the check happens | Where it is published |
|---|---|---|---|
| SPF | The sending server's IP is allowed to send for the domain | Against the envelope sender (Return-Path) domain | TXT record on that domain |
| DKIM | The message was signed by a key the domain controls and was not altered | Against the `d=` domain in the DKIM-Signature header | TXT record at `selector._domainkey.domain` |
| DMARC | The SPF or DKIM domain matches the visible From domain, and what to do if not | Against the From header domain | TXT record at `_dmarc.domain` |
SPF: which servers may send
Sender Policy Framework (SPF) is a list of servers allowed to send mail for a domain. The receiving server looks at the envelope sender, also called the Return-Path or MAIL FROM address. This is the address used in the SMTP conversation, and recipients usually never see it. The server then fetches the SPF record for that domain and checks whether the connecting IP address is listed.
mail.example.com. TXT "v=spf1 include:_spf.esp.example.net -all"
_dmarc.mail.example.com. TXT "v=DMARC1; p=none; rua=mailto:[email protected]"Read the SPF record from left to right:
v=spf1marks the record as SPF version 1.include:_spf.esp.example.netsays "also accept whatever IPs that domain's SPF record lists". Your ESP publishes this, so you do not track their IP addresses yourself.-allis a hard fail for everything else.~allis a soft fail, which receivers treat as suspicious but usually still accept.
SPF has well-known limits:
- A domain may publish only one SPF record. Two
v=spf1records cause a permanent error, and receivers then treat SPF as failed. To add a second sender, merge it into the existing record. - The evaluation allows at most 10 DNS lookups. Each
include,a,mx, andredirectcounts, including the lookups nested inside included records. Going over 10 produces apermerror. - SPF breaks when mail is forwarded, because the forwarding server's IP is not in your record. DKIM is what survives forwarding.
DKIM: a signature on the message
DomainKeys Identified Mail (DKIM) signs selected headers and the body with a private key held by the sender. The signature travels in a DKIM-Signature header. The receiver reads the d= (signing domain) and s= (selector) values, fetches the public key from DNS, and verifies the signature.
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=mail.example.com; s=s2026a;
h=from:to:subject:date:message-id;
bh=BASE64_BODY_HASH; b=BASE64_SIGNATURE
s2026a._domainkey.mail.example.com. TXT "v=DKIM1; k=rsa; p=BASE64_PUBLIC_KEY"Key points:
- The selector lets you publish several keys at once. Include a date or version in the name, such as
s2026a, so you can rotate keys by adding a new selector, switching signing to it, and removing the old one later. - Use 2048-bit RSA keys where your DNS provider allows it. Some DNS panels require splitting long TXT values into several quoted strings, and that is valid.
- A signature breaks if any signed header or the body changes in transit. Mailing lists that add footers are the usual cause.
- Many ESPs ask you to publish CNAME records instead of TXT. The ESP then hosts the key and can rotate it for you.
DMARC: alignment and policy
Domain-based Message Authentication, Reporting and Conformance (DMARC) ties SPF and DKIM to the address people see. A message passes DMARC if at least one of these holds:
- SPF passes and the Return-Path domain aligns with the From domain.
- DKIM passes and the
d=domain aligns with the From domain.
Alignment is the part people miss. If your ESP sends with its own Return-Path domain and signs with its own d= domain, SPF and DKIM can both pass while DMARC still fails, because neither domain matches your From address. Setting up a custom sending domain in the ESP, usually with a few CNAME records, is what fixes this.
Alignment is relaxed by default, so bounce.mail.example.com aligns with mail.example.com because they share an organizational domain. Strict alignment (aspf=s, adkim=s) requires an exact match.
The DMARC p= tag tells receivers what to do when a message fails:
p=nonemonitors only. Receivers deliver normally and send you reports.p=quarantineasks receivers to treat failures as suspicious, usually by putting them in spam.p=rejectasks receivers to refuse failures.
The rua= tag names a mailbox for aggregate reports, which are daily XML summaries from receivers showing which IPs sent mail as your domain and whether it passed.
Setting it up for a marketing stream
Work in this order. Each step can be checked before the next.
- Pick a sending subdomain, such as
mail.example.com, rather than your root domain. A subdomain isolates reputation and lets you apply different DMARC settings to it. - In your ESP, add the domain for sending. The ESP gives you DNS records for DKIM, a custom Return-Path, and sometimes SPF.
- Publish those records and wait for the ESP to confirm them. Some DNS changes take hours to propagate.
- Publish a DMARC record at
_dmarc.mail.example.comwithp=noneand anruaaddress. - Send a test message to a mailbox you control. View the original headers and look for
spf=pass,dkim=pass, anddmarc=passin theAuthentication-Resultsheader. - Read the aggregate reports for a few weeks. Find every legitimate source of mail using your domain, such as the CRM, the support desk, or an old billing tool. Fix any that fail.
- Move to
p=quarantine, then top=reject. Thepct=tag lets you apply the policy to a fraction of failing mail at first.
Marketing mail also needs the unsubscribe headers described elsewhere in this tutorial. They sit beside authentication in the bulk-sender requirements, and the pair of headers looks like this:
List-Unsubscribe: <https://example.com/unsubscribe/abc123>, <mailto:[email protected]?subject=unsubscribe>
List-Unsubscribe-Post: List-Unsubscribe=One-ClickNote that List-Unsubscribe is only trusted by receivers when the message is DKIM-signed. Gmail's rules also require the unsubscribe headers to be covered by the signature for one-click handling.
Checking your records with code
This script reads the SPF and DMARC records for a sending domain and flags the most common mistakes. Install the library first with pip install dnspython.
import dns.resolver
def get_txt(name):
try:
answers = dns.resolver.resolve(name, "TXT")
except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
return []
return [b"".join(rdata.strings).decode() for rdata in answers]
domain = "mail.example.com"
spf = [t for t in get_txt(domain) if t.lower().startswith("v=spf1")]
dmarc = [t for t in get_txt(f"_dmarc.{domain}") if t.upper().startswith("V=DMARC1")]
if len(spf) == 0:
print("No SPF record found")
elif len(spf) > 1:
print("Problem: more than one SPF record, which causes a permerror")
else:
print("SPF:", spf[0])
if not spf[0].rstrip().endswith(("-all", "~all")):
print("Warning: SPF record does not end with -all or ~all")
if len(dmarc) == 0:
print("No DMARC record found")
else:
print("DMARC:", dmarc[0])This does not count the 10 SPF lookups or fetch DKIM keys, which need the selector name. Use your ESP's verification page or a dedicated SPF checker for those.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| SPF and DKIM pass but DMARC fails | Return-Path and `d=` domains belong to the ESP, not to you | Configure a custom sending domain so at least one aligns |
| `spf=permerror` | Two SPF records, or more than 10 lookups | Merge into one record; remove unused `include` entries |
| `dkim=fail (body hash did not verify)` | A system changed the body after signing, for example a footer or link rewriting | Sign after all modifications, or turn off the rewriting |
| Mail from a forgotten tool is rejected after `p=reject` | The tool was never in your SPF record or signing with your domain | Read aggregate reports before tightening the policy |
| Reset emails pass but the newsletter fails | Streams use different domains or different ESP settings | Verify each stream's records separately |
Summary of the model
SPF authorizes servers, DKIM signs content, and DMARC checks that at least one of them lines up with the visible From domain and tells receivers what to do otherwise. You roll out in monitoring mode, read reports, fix every legitimate source, then tighten the policy.
Exercise
Your ESP's test shows spf=pass and dkim=pass, yet dmarc=fail. The From address is [email protected], the Return-Path is [email protected], and the DKIM signature has d=esp-mail.net. Why does DMARC fail, and what do you change?
Show answer
DMARC requires that a passing SPF or DKIM domain align with the From domain. Here the SPF domain (esp-mail.net) and the DKIM domain (esp-mail.net) both differ from mail.example.com, so neither aligns. Set up a custom sending domain in the ESP so the Return-Path becomes something like bounce.mail.example.com (aligned under relaxed mode) and the DKIM signature uses d=mail.example.com. Aligning just one of the two is enough for DMARC to pass.
Exercise
A colleague adds a second TXT record starting with v=spf1 for a new survey tool, leaving the existing record in place. What happens, and how should it be done?
Show answer
A domain must have exactly one SPF record. Two v=spf1 records produce a permerror, so receivers treat SPF as failed for all mail from that domain. Merge the two into a single record, for example by adding the survey tool's include: mechanism to the existing one, and check that the total stays within 10 DNS lookups.