Skip to content

Publish MTA-STS

MTA-STS publishes an HTTPS transport policy for your MX hosts. An incoming domain policy and an outgoing Postfix policy client are separate features.

Setup

Prepare your values and Debian packages. Use your publicly trusted certificates; laboratory acceptance uses an explicitly local test CA.

Publish an A record for mta-sts.YOUR-DOMAIN pointing to your webserver and obtain its ACME certificate. Use the existing Nginx or Apache webroot challenge where applicable; with a free HTTP port, alternatively:

Public ACME issuance
. /root/mailserver.env
certbot certonly --standalone --agree-tos --non-interactive --email "$REPORT_MAILBOX" -d "mta-sts.$DOMAIN"

Then run this block. Begin in testing mode and monitor TLS-RPT; switch to enforce only after successful external MX and TLS checks. Publish the printed DNS TXT value at your provider.

Select the same webserver as during installation. These examples create a dedicated HTTPS virtual host; for existing virtual hosts, merge the path configuration there instead of creating a duplicate host for the same name.

set -euo pipefail
. /root/mailserver.env
test -s "/etc/letsencrypt/live/mta-sts.$DOMAIN/fullchain.pem"
install -d -m 0755 /var/www/mta-sts/.well-known
cat > /var/www/mta-sts/.well-known/mta-sts.txt <<EOF
version: STSv1
mode: testing
mx: $MAILHOST
max_age: 86400
EOF
cat > /etc/nginx/sites-available/mail-mta-sts <<EOF
server {
    listen 443 ssl;
    server_name mta-sts.$DOMAIN;
    ssl_certificate /etc/letsencrypt/live/mta-sts.$DOMAIN/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mta-sts.$DOMAIN/privkey.pem;
    location = /.well-known/mta-sts.txt {
        root /var/www/mta-sts;
        default_type text/plain;
    }
    location / { return 404; }
}
EOF
ln -sfn /etc/nginx/sites-available/mail-mta-sts /etc/nginx/sites-enabled/mail-mta-sts
nginx -t
systemctl enable --now nginx
systemctl reload nginx
id=$(date -u +%Y%m%dT%H%M%S)
printf '_mta-sts.%s. IN TXT "v=STSv1; id=%s"\n' "$DOMAIN" "$id"
set -euo pipefail
. /root/mailserver.env
test -s "/etc/letsencrypt/live/mta-sts.$DOMAIN/fullchain.pem"
install -d -m 0755 /var/www/mta-sts/.well-known
cat > /var/www/mta-sts/.well-known/mta-sts.txt <<EOF
version: STSv1
mode: testing
mx: $MAILHOST
max_age: 86400
EOF
a2enmod ssl headers
cat > /etc/apache2/sites-available/mail-mta-sts.conf <<EOF
<VirtualHost *:443>
    ServerName mta-sts.$DOMAIN
    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/mta-sts.$DOMAIN/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/mta-sts.$DOMAIN/privkey.pem
    Alias "/.well-known/mta-sts.txt" "/var/www/mta-sts/.well-known/mta-sts.txt"
    <Directory "/var/www/mta-sts/.well-known">
        Options None
        AllowOverride None
        Require all granted
    </Directory>
    <Location "/.well-known/mta-sts.txt">
        ForceType text/plain
    </Location>
    RedirectMatch 404 "^/(?!\\.well-known/mta-sts\\.txt$).*"
</VirtualHost>
EOF
a2ensite mail-mta-sts.conf
apache2ctl configtest
systemctl enable --now apache2
systemctl reload apache2
id=$(date -u +%Y%m%dT%H%M%S)
printf '_mta-sts.%s. IN TXT "v=STSv1; id=%s"\n' "$DOMAIN" "$id"

Test

set -euo pipefail
. /root/mailserver.env
curl --fail --silent --show-error --resolve "mta-sts.$DOMAIN:443:127.0.0.1" \
  "https://mta-sts.$DOMAIN/.well-known/mta-sts.txt" > /root/mail-tests/mta-sts.txt
grep -Fx 'version: STSv1' /root/mail-tests/mta-sts.txt
grep -E '^mode: (testing|enforce)$' /root/mail-tests/mta-sts.txt
grep -Fx "mx: $MAILHOST" /root/mail-tests/mta-sts.txt
grep -E '^max_age: [0-9]+$' /root/mail-tests/mta-sts.txt
dig "_mta-sts.$DOMAIN" TXT +short

Also check externally with curl without --resolve and test MX/STARTTLS. Keep HTTPS verification enabled; do not use -k. Change the DNS ID when changing policy and account for long-lived caches. This guide publishes your incoming policy; outgoing MTA-STS enforcement requires an additional suitable policy resolver for Postfix.

Switch from testing to enforce

testing is an introductory phase: supporting senders check the policy and can report failures through TLS-RPT, but may still deliver despite MTA-STS violations. Only enforce prohibits these senders from delivering to mismatching MX hosts, without STARTTLS, or with an invalid certificate. Such failures normally defer delivery. MTA-STS only affects senders that support it.

Prerequisites

  • The public policy is reachable over HTTPS with a valid certificate.
  • Every MX host in use, including backup MX hosts, is covered by suitable mx: entries. For multiple hosts, extend the single-host example above before switching.
  • Every MX offers STARTTLS on port 25 from outside your network, with a trusted certificate chain and a certificate matching the MX hostname. Check all published IPv4/IPv6 destinations.
  • TLS-RPT is configured; received reports and your own external tests show no unresolved failures. Missing reports alone are not evidence of success. No fixed waiting period makes the switch automatically safe.

Update the policy and DNS

This block applies to Nginx and Apache. It changes only the mode, preserving all mx: entries and max_age. The static file is replaced atomically; no webserver restart is needed. Run it only after meeting the prerequisites.

Switch the published policy to enforce
set -euo pipefail
. /root/mailserver.env
policy=/var/www/mta-sts/.well-known/mta-sts.txt
test "$(grep -Ec '^mode: (testing|enforce)$' "$policy")" -eq 1
install -d -m 0700 /root/mail-tests
pending=$(mktemp "${policy}.XXXXXX")
trap 'rm -f -- "$pending"' EXIT
sed -E 's/^mode: (testing|enforce)$/mode: enforce/' "$policy" > "$pending"
chmod 0644 "$pending"
mv -- "$pending" "$policy"
trap - EXIT
id=$(openssl rand -hex 16)
printf '%s\n' "$id" > /root/mail-tests/mta-sts-policy-id
printf '_mta-sts.%s. IN TXT "v=STSv1; id=%s"\n' "$DOMAIN" "$id"

Then replace the existing TXT record for _mta-sts.YOUR-DOMAIN at your DNS provider with the printed value. Do not add a second MTA-STS TXT record. The command does not update DNS itself. The new ID announces the changed policy; it does not guarantee immediate refresh of every sender's cache.

Verify public publication

Run this after updating DNS. Unlike the local test above, this request uses public name resolution. Also check from an independent external machine, comparing the new DNS ID and HTTPS content with the expected values.

Verify public HTTPS and DNS after updating the TXT record
set -euo pipefail
. /root/mailserver.env
id=$(cat /root/mail-tests/mta-sts-policy-id)
curl --fail --silent --show-error --connect-timeout 10 --max-time 30 \
  "https://mta-sts.$DOMAIN/.well-known/mta-sts.txt" \
  > /root/mail-tests/mta-sts-public.txt
grep -Fx 'mode: enforce' /root/mail-tests/mta-sts-public.txt
cmp /var/www/mta-sts/.well-known/mta-sts.txt /root/mail-tests/mta-sts-public.txt
dig +short TXT "_mta-sts.$DOMAIN" > /root/mail-tests/mta-sts-dns.txt
grep -F "id=$id" /root/mail-tests/mta-sts-dns.txt
echo 'PASS: public policy is enforce and the resolver returns the new policy ID.'

This verifies publication, not STARTTLS on every MX host or refresh of all sender caches. Continue monitoring TLS-RPT and certificate renewals.

max_age: 86400 means a one-day cache lifetime; it does not automatically switch to enforce. For stable operation, the standard normally expects weeks or longer, for example max_age: 604800 for seven days. Choose a longer lifetime deliberately after successful introduction and publish a new DNS ID with every policy change.

If problems occur

Prioritize fixing MX, STARTTLS or certificate failures. Switching back to testing or disabling the policy with none does not immediately remove cached enforce policies. Keep the HTTPS endpoint available and update the DNS ID even when reverting; simply deleting the policy or TXT record is not an immediate rollback.

Source: RFC 8461 – Policy Application and Operational Considerations.