Skip to content

Publish MTA-STS and TLS-RPT for your mail domain

Force inbound SMTP to use verified TLS. Publish the _mta-sts TXT record, serve the policy file from nginx over HTTPS, add TLS-RPT reporting, and roll from testing to enforce safely.

Your MX server offers STARTTLS, so inbound mail is encrypted — except when it quietly isn’t. STARTTLS is advertised in cleartext, which means anything sitting on the path can strip the advertisement, the sending server concludes TLS was never available, and your mail arrives in the clear with no error raised anywhere. MTA-STS (RFC 8461) closes that door from your side: you publish a policy that says “my MX hosts are exactly these names, they always speak TLS with a publicly trusted certificate, and if that turns out not to be true, do not deliver.” TLS-RPT (RFC 8460) supplies the other half — daily JSON reports from Google, Microsoft and everyone else who tried, telling you precisely what succeeded and what failed. This guide publishes both for example.com: the _mta-sts.<domain> TXT record, the policy file served by nginx over HTTPS, the _smtp._tls.<domain> TXT record, and the rollout from testing to enforce without deferring real mail.

What you’ll accomplish

  • Publish a _mta-sts.example.com TXT record and a policy file at https://mta-sts.example.com/.well-known/mta-sts.txt that senders can actually fetch.
  • Get a certificate on the policy host and confirm the certificate on your MX host matches the names listed in the policy.
  • Turn on TLS-RPT so failures arrive as reports instead of as a support ticket three weeks later.
  • Run in mode: testing long enough to prove the policy is correct, then move to mode: enforce.
  • Know when to reach for DANE instead, and what MTA-STS does not protect.

Prerequisites

  • Control of the domain’s DNS, including the ability to create _mta-sts and _smtp._tls TXT records and an A/AAAA record for mta-sts.example.com.
  • MX records that are already correct and stable. MTA-STS pins the names in your policy to the names in DNS. If your MX set is about to change, change it first and let it settle.
  • An HTTPS host you can point mta-sts.example.com at. It does not have to be the mail server — a small nginx vhost on any web server is fine, and separating it is often easier to keep certified.
  • A publicly trusted certificate for mta-sts.example.com (Let’s Encrypt is fine). Self-signed certificates fail the policy fetch outright.
  • A working certificate on port 25 whose subject alternative names cover each MX hostname, with Postfix running smtpd_tls_security_level = may so STARTTLS is offered on inbound SMTP.

How it works

MTA-STS is a sender-enforced policy that you publish. Nothing on your mail server changes behaviour; what changes is what a well-behaved sending MTA is willing to do when TLS looks wrong. The sender walks four lookups, in order, and caches the result.

Four-stage sequence showing a sending MTA looking up MX records, the _mta-sts TXT record, fetching the policy file over HTTPS, then validating the MX certificate before delivering or deferring

The split matters. The TXT record is a cheap signal — it carries a version and an opaque id, nothing else. Senders poll it often; when the id changes, they know the policy has been edited and re-fetch it. The policy file is the expensive part, fetched over HTTPS and cached for max_age seconds. The id is the only thing that invalidates that cache early, which is why forgetting to bump it is the single most common way people wonder why their edit “didn’t take.”

The HTTPS fetch is doing security work of its own. Because the policy is retrieved over a connection authenticated by a certificate for mta-sts.example.com, an attacker who can spoof DNS still cannot forge or suppress a policy without also holding a valid certificate for that name. RFC 8461 is strict about this: the connection must validate, and HTTP 3xx redirects must not be followed. There is no fallback mode.

Once a sender holds your policy, delivery becomes conditional. The MX hostname it connected to must match one of the mx: patterns, and the certificate that host presents on port 25 must be valid for that hostname and chain to a public trust anchor. If either check fails under mode: enforce, the sender does not downgrade to plaintext — it defers the message and tries again later.

Step-by-step

1. Write down your MX names before touching anything else. The policy has to match reality exactly.

dig +short MX example.com
10 mail.example.com.
20 mail2.example.com.

2. Check the certificate each MX presents on port 25. This is the check that will fail in production if you skip it.

openssl s_client -starttls smtp -connect mail.example.com:25 -servername mail.example.com </dev/null 2>/dev/null | openssl x509 -noout -subject -dates -ext subjectAltName
subject=CN = mail.example.com
notBefore=Jul  2 04:11:07 2026 GMT
notAfter=Sep 30 04:11:06 2026 GMT
X509v3 Subject Alternative Name:
    DNS:mail.example.com

The MX hostname must appear in that SAN list. A certificate issued for example.com only, or for the hosting provider’s name, will not do. On the Postfix side, confirm STARTTLS is offered and pointed at the right files:

postconf smtpd_tls_security_level smtpd_tls_cert_file smtpd_tls_key_file
smtpd_tls_security_level = may
smtpd_tls_cert_file = /etc/letsencrypt/live/mail.example.com/fullchain.pem
smtpd_tls_key_file = /etc/letsencrypt/live/mail.example.com/privkey.pem

Leave smtpd_tls_security_level at may. MTA-STS is enforced by the sender; setting encrypt on port 25 would reject legitimate senders that have no TLS at all, which is a different and much blunter decision.

3. Point mta-sts.example.com at your web server and get a certificate. Create the A (and AAAA) record first, wait for it to resolve, then issue.

sudo certbot certonly --nginx -d mta-sts.example.com
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/mta-sts.example.com/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/mta-sts.example.com/privkey.pem

4. Write the policy file. Start in testing with a short max_age — you want mistakes to expire quickly while you are still learning what your senders see.

sudo mkdir -p /var/www/mta-sts/.well-known
sudo tee /var/www/mta-sts/.well-known/mta-sts.txt >/dev/null <<'EOF'
version: STSv1
mode: testing
mx: mail.example.com
mx: mail2.example.com
max_age: 86400
EOF

The fields, in full: version is always STSv1. mode is testing, enforce, or none (publish none to withdraw a policy you no longer want honoured). Each mx: line names one host; a wildcard is permitted only in the leftmost label, so mx: *.example.net is valid and mx: mail.*.net is not. max_age is the cache lifetime in seconds, capped at 31557600.

The RFC’s grammar terminates each line with CRLF. Most senders accept bare LF, but not every validator does, so normalise the file and check it:

sudo sed -i 's/$/\r/' /var/www/mta-sts/.well-known/mta-sts.txt
cat -A /var/www/mta-sts/.well-known/mta-sts.txt
version: STSv1^M$
mode: testing^M$
mx: mail.example.com^M$
mx: mail2.example.com^M$
max_age: 86400^M$

Every line should end ^M$. A line ending in a bare $ is LF-only.

5. Serve it with nginx. One vhost, one location, nothing else on the host.

server {
    listen 80;
    listen [::]:80;
    server_name mta-sts.example.com;
    root /var/www/mta-sts;

    location /.well-known/acme-challenge/ { }
    location / { return 301 https://$host$request_uri; }
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name mta-sts.example.com;
    root /var/www/mta-sts;

    ssl_certificate     /etc/letsencrypt/live/mta-sts.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mta-sts.example.com/privkey.pem;

    location = /.well-known/mta-sts.txt {
        types { }
        default_type text/plain;
        add_header Cache-Control "max-age=600" always;
    }

    location / { return 404; }
}

The types { } block clears the MIME map for that one location so default_type always wins — senders that see anything other than text/plain will discard the policy. The redirect on port 80 is fine because senders never fetch the policy over HTTP; only the HTTPS URL is ever requested.

sudo nginx -t && sudo systemctl reload nginx
nginx: configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

6. Publish the _mta-sts TXT record. The id is an opaque token of 1 to 32 alphanumeric characters — a timestamp is the convention because it makes “when did I last change this” obvious.

_mta-sts.example.com.  3600  IN  TXT  "v=STSv1; id=20260722T101500;"

7. Publish the TLS-RPT record. This one is independent of MTA-STS and worth having even if you never move past testing.

_smtp._tls.example.com.  3600  IN  TXT  "v=TLSRPTv1; rua=mailto:tlsrpt@example.com"

rua takes a mailto: or an https:// endpoint, and you may list several separated by commas. Reports arrive roughly daily as gzipped JSON, one per reporting organisation, summarising every TLS session they attempted to your MX hosts. Route them to a mailbox someone actually reads, or to a parser.

8. Sit in testing, then move to enforce. Give it at least two weeks so the slower reporters have a full cycle.

Three-phase rollout showing testing mode with a short max_age, a reading period for TLS-RPT reports, then enforce mode with a bumped policy id and longer max_age

When the reports are clean, edit the policy and bump the id in the same change:

sudo sed -i 's/^mode: testing/mode: enforce/; s/^max_age: 86400/max_age: 604800/' /var/www/mta-sts/.well-known/mta-sts.txt
cat /var/www/mta-sts/.well-known/mta-sts.txt
version: STSv1
mode: enforce
mx: mail.example.com
mx: mail2.example.com
max_age: 604800

Then update the TXT record to a new id — for example id=20260812T090000;. Senders holding the old cached copy will notice the change on their next TXT poll and re-fetch.

Validate it works

Confirm both TXT records resolve from outside your network, not just from the mail server.

dig +short TXT _mta-sts.example.com @1.1.1.1
dig +short TXT _smtp._tls.example.com @1.1.1.1
"v=STSv1; id=20260722T101500;"
"v=TLSRPTv1; rua=mailto:tlsrpt@example.com"

Check the headers on the policy fetch. The status must be 200 with no redirect, and the content type must be text/plain.

curl -sSI https://mta-sts.example.com/.well-known/mta-sts.txt
HTTP/1.1 200 OK
Server: nginx
Content-Type: text/plain
Content-Length: 92
Cache-Control: max-age=600

If you see 301, text/html, or application/octet-stream, senders will treat the policy as absent. Fetch the body too, and confirm the certificate validated — curl fails loudly if it did not:

curl -sS https://mta-sts.example.com/.well-known/mta-sts.txt
version: STSv1
mode: testing
mx: mail.example.com
mx: mail2.example.com
max_age: 86400

Verify each MX certificate against the policy. Run this once per name in your mx: list — the SAN must contain that exact hostname.

openssl s_client -starttls smtp -connect mail2.example.com:25 -servername mail2.example.com </dev/null 2>/dev/null | openssl x509 -noout -subject -ext subjectAltName -checkhost mail2.example.com
subject=CN = mail2.example.com
X509v3 Subject Alternative Name:
    DNS:mail2.example.com
Host mail2.example.com does match certificate

That last line is the whole test. does NOT match certificate means enforce mode would defer mail from every sender that honours your policy.

Troubleshooting

The policy is served with the wrong content type. By far the most common failure, and it is silent — senders simply behave as if you published nothing. It usually happens when the file is served by an application framework, a static-site host, or a CDN that rewrites the type. Check with curl -sSI as above; if a CDN sits in front, verify the header at the edge, not just at the origin.

CRLF versus LF line endings. The policy grammar terminates lines with CRLF. Many senders are tolerant, several validators are not, and the failure is confusing because the file looks perfect in an editor. Confirm with cat -A and look for ^M$ on every line. If you generate the file from configuration management, remember that most templating engines emit LF by default.

An MX name is not covered by any mx: pattern. Compare dig +short MX example.com against the policy line by line, including the backup MX everyone forgets and any provider-supplied host such as a spam-filtering relay in front of your server. Wildcards only match one label, so mx: *.example.net covers mail.example.net but not mail.eu.example.net. In testing this shows up as failure entries in TLS-RPT; in enforce it shows up as deferred mail.

The certificate on the policy host expired. The policy host is easy to forget precisely because nothing is hosted on it — no monitoring, no visitors, no complaints. When its certificate lapses, the fetch fails and senders fall back to the cached policy until max_age runs out, after which the policy is gone. Put mta-sts.example.com in the same certificate monitoring as everything else, and confirm the renewal hook reloads nginx:

sudo certbot renew --dry-run
Congratulations, all simulated renewals succeeded:
  /etc/letsencrypt/live/mta-sts.example.com/fullchain.pem (success)

You edited the policy but did not bump the id. Senders re-fetch when the id in the TXT record changes, or when max_age expires — whichever comes first. Edit the file without touching the id and your change is invisible for up to a week. Treat the two as a single atomic change: never save the policy file without also updating the TXT record.

When this is the wrong approach

  • This protects inbound mail only. MTA-STS tells other people’s servers how to treat your MX hosts. It does nothing about the mail you send. To honour other domains’ MTA-STS policies as a sender, Postfix needs a helper such as a socketmap-based MTA-STS resolver wired into smtp_tls_policy_maps; that is a separate build with its own daemon and cache to operate.
  • DANE is the better answer if your zone is signed. DANE publishes TLSA records at _25._tcp.mail.example.com and anchors trust in DNSSEC rather than in the public CA system and an HTTPS host. It needs no web server and no certificate for a policy host, and it is what most large European providers check first. The cost is real DNSSEC operations — a mismanaged key rollover breaks mail delivery in a way that is genuinely hard to unwind. No DNSSEC, no DANE. Many domains run both; senders that support both generally prefer DANE.
  • Hosted mail may already handle it. If your MX points at Google Workspace or Microsoft 365, they publish and manage MTA-STS for their own hostnames. Publishing your own policy on top is possible but you now own the mx: list for infrastructure you do not control.
  • enforce without operational discipline is worse than nothing. An expired certificate on the policy host or a stale mx: line will defer real mail. If nobody is watching TLS-RPT and certificate expiry, stay in testing — you still get the reports, and you never defer a message.

The specifications are short and readable: RFC 8461 for MTA-STS and RFC 8460 for TLS-RPT. For the transport-level context underneath all of this, see STARTTLS vs implicit TLS for SMTP; for the authentication layer that sits alongside it, see OpenDMARC validation for Postfix.

If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.