Transport maps are how Postfix routes a message somewhere other than “look up MX, deliver.” This article is the pattern catalog we use when a client needs per-domain routing — one tenant goes through an external relay (because their IP reputation is tied to a vendor), another goes to LMTP for local delivery, the test domain goes to a sinkhole, and the default still works. Transport maps complement the alias maps from postfix-virtual-aliases-recipient.
How to verify
Check current transport configuration before adding anything.
sudo postconf | grep -E 'transport|default_transport|relay_transport'
sudo postconf -M
ls -l /etc/postfix/transport*
[ -f /etc/postfix/transport.db ] && sudo postmap -s /etc/postfix/transport | head -20
The -M flag lists the master.cf services — you’ll need to confirm relay, smtp, lmtp, and any custom transports are defined before referencing them in the map.
What’s happening
Postfix’s delivery pipeline picks a transport for each message based on the recipient address. The default is to look up MX records and use the smtp transport. A transport map overrides this for matching addresses, sending them to a specific transport with optional nexthop.
The map is a hash-like key→value file. The key is matched against the recipient in order of specificity:
- Full address:
user@example.com - Local part with wildcard:
user@ - Subdomain:
.example.com - Domain:
example.com - Catchall: omitted — falls back to
default_transport
Each value is a transport:nexthop pair:
transport— the name of a service inmaster.cf(smtp,lmtp,relay,error, or a custom one).nexthop— the destination. Forsmtpandrelay, this is an MX-looking-up host or[host.example.com]:25to bypass MX lookup. Forlmtp, this is a Unix socket or TCP host:port. Forerror, this is the SMTP failure message to return.
Empty fields are legal: domain.com smtp: means “use smtp, but look up MX as normal” — useful for forcing a transport even when you’ve changed the default.
Common transport map shapes
1. Smarthost a specific domain
You don’t want to deliver partner.com directly because your outbound IP isn’t allow-listed there; route through a relay:
# /etc/postfix/transport
partner.com smtp:[relay.partnerco.example.com]:587
.partner.com smtp:[relay.partnerco.example.com]:587
The brackets [...] tell Postfix to skip MX lookup and connect directly to the host. The leading dot matches all subdomains.
2. Local LMTP delivery for one tenant
One domain’s mailboxes live in Dovecot; everything else relays out:
example.com lmtp:unix:private/dovecot-lmtp
.example.com lmtp:unix:private/dovecot-lmtp
The path is relative to Postfix’s chroot — /var/spool/postfix/private/dovecot-lmtp.
3. Sinkhole / silently discard a domain
Test domains shouldn’t actually send mail; use the discard transport:
test.example.local discard:silently dropping outbound to test.example.local
Or use error: to bounce with a controlled message:
banned.example.com error:5.7.1 Communication with banned.example.com is not permitted
4. Outbound on a different IP
You have two public IPs; send marketing mail from one and transactional from another:
mg.example.com smtp-marketing:
example.com smtp:
Then in master.cf, add a new service:
smtp-marketing unix - - - - - smtp
-o smtp_bind_address=203.0.113.20
-o smtp_helo_name=mg.example.com
5. Route to a smart-host but auth on submission
Outbound through a vendor that requires SMTP AUTH on port 587:
* smtp:[smtp.mailgun.org]:587
And in main.cf:
relayhost = [smtp.mailgun.org]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = encrypt
The /etc/postfix/sasl_passwd file:
[smtp.mailgun.org]:587 postmaster@mg.example.com:strong-vendor-pw
Then postmap hash:/etc/postfix/sasl_passwd and chmod 600.
The procedure
-
Write the transport map. Use
/etc/postfix/transportas the canonical file:sudo $EDITOR /etc/postfix/transportOne key→transport per line. Order doesn’t matter — Postfix does its own specificity matching.
-
Compile to hash database. Postfix reads the
.dbfile, not the text file:sudo postmap hash:/etc/postfix/transport ls -l /etc/postfix/transport* -
Wire into
main.cf:transport_maps = hash:/etc/postfix/transport -
Reload and verify:
sudo postfix check sudo systemctl reload postfix sudo postmap -q "partner.com" hash:/etc/postfix/transportThe
postmap -qlookup returns the value for the key — confirms the compiled DB matches the source file. -
Send a probe through each routed path. From the box:
swaks --from postmaster@example.com --to test@partner.com --server localhost sudo journalctl -u postfix -fThe log should show
relay=relay.partnerco.example.com[...]:587not the default MX.
SQL-backed transport maps
When transport rules live in SQL (multi-tenant deployments), the same lookup table pattern from PostfixAdmin applies. /etc/postfix/sql/transport.cf:
user = postfix
password = pa-db-pw
hosts = 127.0.0.1
dbname = postfix
query = SELECT transport FROM transport WHERE domain='%s'
And main.cf:
transport_maps = mysql:/etc/postfix/sql/transport.cf
No postmap step — SQL queries are live.
Common pitfalls
- The text file was edited but
postmapwas not run; Postfix keeps reading the old.dbuntil reload — the change appears to do nothing. - A wildcard line
.example.comis added but the parentexample.comis missing; messages tobare.example.comroute through the wildcard butexample.comitself follows the default. - The
smtp-marketingservice inmaster.cfreferencessmtpas its protocol but the slot name is the same as a different active service — Postfix prints a confusing “duplicate service” warning and ignores one of them; service names must be unique. - The
relayhostglobal is set alongside specifictransport_mapsentries —relayhostonly applies to addresses with no transport match; if your map’s catchall is missing, mail to unmatched domains goes throughrelayhostwhether you wanted it to or not. - A transport map referencing
[host.example.com]:25is treated as a hostname-with-port and Postfix skips MX lookup, but DNS still has to resolve the A record —Connection refusedhere usually means the brackets were forgotten and MX lookup returned nothing.
Stack Harbor uses transport maps as the routing fabric on multi-tenant mail edges in the engagements we run — per-domain delivery shape declared in code, compiled or SQL-served depending on the deployment, and verified by postmap -q per route in the smoke test. If you have a Postfix that grew a hand-edited transport over years and nobody knows why a specific domain takes a specific path, that audit is part of our Managed Operations practice.