Day 2 · DNS, mail servers & hosting
DNS for email: MX, PTR, null MX and every record a mail domain needs
Course contents
Email is routed entirely by DNS. A sending server finds where to deliver from the recipient domain’s records, and a receiving server judges the sender by the sender’s records. Most email problems an IT person meets are, in the end, a DNS record that is missing, wrong or out of date. This lesson covers every record a mail domain needs except the three authentication records (SPF, DKIM and DMARC), which get their own lessons on day 3.
If you need a refresher on DNS itself (zones, resolvers, TTLs, dig), see How DNS works in the networking course.
The map: every record email uses
| Record | Name | Example value | Used by | Lesson |
|---|---|---|---|---|
| MX | example.com | 10 mx1.example.com. | Senders: where to deliver mail for the domain | this one |
| A / AAAA | mx1.example.com | 203.0.113.25 | Senders: the address of each MX host | this one |
| PTR | 25.113.0.203.in-addr.arpa | mx1.example.com. | Receivers: the name of a sending IP | this one |
| TXT (SPF) | example.com | v=spf1 include:_spf.google.com -all | Receivers: which IPs may send for the domain | 8 |
| TXT (DKIM) | selector._domainkey.example.com | v=DKIM1; k=rsa; p=MIIBIj... | Receivers: the public key for DKIM signatures | 9 |
| TXT (DMARC) | _dmarc.example.com | v=DMARC1; p=reject; rua=mailto:... | Receivers: policy and reports | 10 |
| TXT + HTTPS (MTA-STS) | _mta-sts.example.com | v=STSv1; id=2026110101 | Senders: require TLS to your MX | 11 |
| TXT (TLS-RPT) | _smtp._tls.example.com | v=TLSRPTv1; rua=mailto:... | Senders: where to report TLS problems | 11 |
| TLSA (DANE) | _25._tcp.mx1.example.com | 3 1 1 a1b2... | Senders: pin the MX’s certificate (needs DNSSEC) | 11 |
| TXT (BIMI) | default._bimi.example.com | v=BIMI1; l=https://...svg; a=... | Mailbox providers: your logo | 11 |
| SRV / CNAME | _imaps._tcp, autodiscover | 0 1 993 imap.example.com. | Mail apps: find server settings | 4 |
| TXT (verification) | example.com | google-site-verification=..., MS=ms12345678 | Providers: prove you own the domain | 7 |
MX records in depth
An MX record says “mail for this domain is accepted by this host” and has two fields: a preference (priority) and a host name.
dig +short gmail.com MX
5 gmail-smtp-in.l.google.com.
10 alt1.gmail-smtp-in.l.google.com.
20 alt2.gmail-smtp-in.l.google.com.
30 alt3.gmail-smtp-in.l.google.com.
40 alt4.gmail-smtp-in.l.google.com.
How a sending MTA uses them (RFC 5321, section 5):
- Sort the MX hosts by preference, lowest first. Hosts with the same preference are tried in random order, which spreads the load.
- Resolve each host to its addresses (AAAA and A) and try them one by one.
- If a host can’t be reached, or answers with a temporary 4xx error, move on to the next. If every host fails, keep the message in the queue and retry later.
- A permanent 5xx answer stops the attempt: the next MX would only say the same thing.
Rules that trip people up:
- The target of an MX must be a host name with A or AAAA records. Not an IP address (
10 203.0.113.25is invalid, even though some DNS panels accept it), and not a CNAME (forbidden by RFC 2181 and the reason for some very confusing failures). - The preference numbers are only compared with each other.
10/20and1/2mean the same thing. - The trailing dot matters in zone files:
mx1.example.comwithout the dot, inside theexample.comzone, becomesmx1.example.com.example.com.. DNS panels usually add it for you; zone files don’t. - MX records belong to one exact name.
example.comhaving an MX says nothing aboutsales.example.com. If you want to receive mail at a subdomain, give it its own MX.
No MX at all: the implicit MX
If a domain has no MX record, senders fall back to its A or AAAA record and try to deliver to that address (“implicit MX”, RFC 5321 section 5.1). That’s a leftover from the time before MX records existed, and it has an unpleasant side effect: a domain that only hosts a website receives SMTP connections on its web server, and senders keep retrying for days when nothing answers on port 25.
Null MX: “this domain never receives mail”
The fix is a null MX (RFC 7505): a single MX record with preference 0 and the root . as the host.
dig +short example.com MX
0 .
A sender that sees it rejects the message immediately with a clear error, instead of trying for five days. Use it for every domain that doesn’t receive mail.
Backup MX: think twice
A secondary MX on another network, run by you or an ISP, was common when servers went down for days. Today it often does more harm than good:
- Spammers deliberately send to the lowest-priority MX, hoping it filters less. A backup MX must check recipients, spam and authentication exactly like the primary, or it becomes a backscatter source.
- Sending MTAs already queue mail for days when your MX is down. For most organizations, a highly available primary (or a hosted provider with many servers behind one name) plus the senders’ own queues is better than a weaker backup.
A, AAAA and IPv6
Every MX host needs address records, and since IPv6 is common, give it AAAA too if it’s reachable over IPv6. Two cautions:
- Sending over IPv6 is held to a higher standard. Gmail, for example, rejects IPv6 senders without a valid PTR record. If you can’t set reverse DNS for your IPv6 address, configure your MTA to send over IPv4 only (Postfix:
inet_protocols = ipv4, orsmtp_address_preference = ipv4). - Reputation systems treat IPv6 by network, not by single address, because one server can have billions of them. Send from one fixed address, not a random one from a /64.
PTR and forward-confirmed reverse DNS
A PTR record maps an IP address back to a name, in the special in-addr.arpa (IPv4) and ip6.arpa (IPv6) zones. Receivers look up the PTR of every connecting IP, and then look up that name’s A record to check that it points back to the same IP. When both directions agree, the IP has forward-confirmed reverse DNS (FCrDNS).
dig +short -x 209.85.220.41 # reverse: the name of one of Google's sending IPs
dig +short mail-sor-f41.google.com # forward: does that name point back?
mail-sor-f41.google.com.
209.85.220.41
Name and address confirm each other: FCrDNS passes. Why it matters:
- Gmail requires valid forward and reverse DNS for every sending IP (
550 5.7.25 ... doesn't have a PTR recordotherwise), and many other receivers reject or heavily penalize IPs without one. - A generic PTR such as
203-0-113-25.dsl.isp.exampleorec2-203-0-113-25.compute-1.amazonaws.comsays “this is a home connection or a random cloud VM”, and spam filters treat it that way. A mail server’s PTR should be its own name, likemail.example.com. - The PTR name, the A record, and the name the server uses in
EHLOshould all be the same name.
You can’t set a PTR in your own DNS zone: it lives in the zone of whoever owns the IP block. For a server at a hosting provider, you set it in their control panel or ask their support (AWS: the reverse DNS setting of an Elastic IP; Google Cloud: the PTR setting on the VM’s external IP; Azure: the public IP’s DNS settings; most VPS hosts: a “reverse DNS” field). If you use Google Workspace, Microsoft 365 or an ESP, they handle PTR for their own IPs.
Here is a Microsoft 365 sending server, with the PTR and A records in agreement:
dig +short -x 40.107.22.101
dig +short mail-am6eur05on2101.outbound.protection.outlook.com
mail-am6eur05on2101.outbound.protection.outlook.com.
40.107.22.101
TXT records: the rules of the container
SPF, DKIM, DMARC and many other mechanisms live in TXT records, so a few rules of the TXT type apply to all of them:
- One TXT record can hold several strings of up to 255 bytes each, which are joined without spaces when read. Long DKIM keys must be split this way:
"v=DKIM1; k=rsa; p=MIIBIjANBg..." "...IDAQAB". Most DNS panels split automatically; some get it wrong, so always check the published result withdig. - One name can have several TXT records:
gmail.comhas an SPF record and several verification records side by side. But SPF in particular must be exactly one record starting withv=spf1; two SPF records make SPF fail with apermerror. - Records are found by exact name: a DMARC record at
dmarc.example.com(missing the underscore) or a DKIM key atselector.domainkey.example.comsimply doesn’t exist for the receiver. - Quotes are part of the zone-file syntax, not of the value. Some web panels want them, others add them, and doubled quotes (
""v=spf1..."") are a common mistake.
Locking down domains that don’t send mail
Every domain you own is a domain someone can forge: old brand names, typo domains you bought defensively, domains used only for websites or redirects. If a domain never sends or receives email, publish these records to tell every receiver so. example.com itself is a perfect model:
dig +short example.com MX
dig +short example.com TXT | grep spf
dig +short _dmarc.example.com TXT
0 .
"v=spf1 -all"
"v=DMARC1;p=reject;sp=reject;adkim=s;aspf=s"
| Record | Meaning |
|---|---|
example.com. MX 0 . | Accepts no mail (null MX) |
example.com. TXT "v=spf1 -all" | No IP is allowed to send as this domain |
_dmarc.example.com. TXT "v=DMARC1; p=reject; sp=reject; adkim=s; aspf=s" | Reject anything that claims to be from it, subdomains included |
*._domainkey.example.com. TXT "v=DKIM1; p=" | Optional: an empty key, so any DKIM selector is explicitly revoked |
The same applies to subdomains that only serve websites (www, shop, blog): a DMARC policy with sp=reject on the main domain covers them, and an SPF record of v=spf1 -all on hosts that have A records closes the remaining gap for receivers that check SPF on subdomains.
Using subdomains for sending
Large senders separate their mail streams by subdomain: people send from example.com, the application sends receipts from mail.example.com, and marketing sends from news.example.com. Each subdomain has its own SPF, DKIM keys and reputation, so a marketing campaign that annoys people doesn’t hurt the delivery of password-reset emails. Lesson 13 covers this in depth.
Operating DNS for email
- TTL. Keep MX, SPF and DMARC records at a moderate TTL (an hour is typical). Before any planned change (a migration, a new provider), lower the TTL to 5 minutes a day or two in advance, so the change takes effect quickly and can be rolled back quickly.
- DNSSEC. Signing your zone protects all of these records from forgery, and is a requirement for DANE. Most DNS providers can enable it with a click; the risky part is the DS record at the registrar.
- Change control. A typo in an SPF or MX record can stop a company’s email. Keep DNS in version control (OctoDNS, DNSControl, Terraform) or at least document every change, and check every change with
digagainst the authoritative servers right after you make it. - Don’t delete what you don’t understand. Old verification TXT records look like junk, but removing one can break a service’s domain verification. Find out what each record is for first.