Skip to content

Postfix transport maps — domain-based routing and selective relay

Use Postfix transport maps to route specific domains through a smarthost, send some traffic over a secondary outbound IP, or dispatch certain recipients to LMTP for delivery.

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:

  1. Full address: user@example.com
  2. Local part with wildcard: user@
  3. Subdomain: .example.com
  4. Domain: example.com
  5. Catchall: omitted — falls back to default_transport

Each value is a transport:nexthop pair:

  • transport — the name of a service in master.cf (smtp, lmtp, relay, error, or a custom one).
  • nexthop — the destination. For smtp and relay, this is an MX-looking-up host or [host.example.com]:25 to bypass MX lookup. For lmtp, this is a Unix socket or TCP host:port. For error, 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

  1. Write the transport map. Use /etc/postfix/transport as the canonical file:

    sudo $EDITOR /etc/postfix/transport

    One key→transport per line. Order doesn’t matter — Postfix does its own specificity matching.

  2. Compile to hash database. Postfix reads the .db file, not the text file:

    sudo postmap hash:/etc/postfix/transport
    ls -l /etc/postfix/transport*
  3. Wire into main.cf:

    transport_maps = hash:/etc/postfix/transport
  4. Reload and verify:

    sudo postfix check
    sudo systemctl reload postfix
    sudo postmap -q "partner.com" hash:/etc/postfix/transport

    The postmap -q lookup returns the value for the key — confirms the compiled DB matches the source file.

  5. 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 -f

    The log should show relay=relay.partnerco.example.com[...]:587 not 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 postmap was not run; Postfix keeps reading the old .db until reload — the change appears to do nothing.
  • A wildcard line .example.com is added but the parent example.com is missing; messages to bare.example.com route through the wildcard but example.com itself follows the default.
  • The smtp-marketing service in master.cf references smtp as 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 relayhost global is set alongside specific transport_maps entries — relayhost only applies to addresses with no transport match; if your map’s catchall is missing, mail to unmatched domains goes through relayhost whether you wanted it to or not.
  • A transport map referencing [host.example.com]:25 is treated as a hostname-with-port and Postfix skips MX lookup, but DNS still has to resolve the A record — Connection refused here 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.