Your application servers send mail directly to the world. It works until a third of your IPs end up on a UCEPROTECT list because one cron loop went into a tight retry loop against a Gmail address that hard-bounced for five hours. You spend three weeks getting off the list and decide every outbound message goes through a single relay that talks to a sending provider over enforced TLS, authenticates with credentials nobody hard-codes in application config, and rate-limits the loops that were the actual problem. This is that relay.
What’s happening
A smarthost is a local SMTP server whose only job is to receive mail from your fleet on the private network, queue it, and ship it to a single upstream — Postal, Stalwart, Mailgun, SES, Postmark, whatever — that holds the IP reputation and the real DKIM keys. The smarthost gives you three things you can’t get from sendmail calls scattered across application servers:
- One choke point for outbound mail. Rate caps, retry policy, queue inspection, header rewriting all happen here. If a worker goes berserk and tries to send 50k retries an hour, the smarthost queues them and drips them out at the cap you set. No upstream sees the spike.
- One place for credentials. Application servers connect to the smarthost on the private network with no auth (
mynetworks); only the smarthost holds the upstream SASL credentials, in a file readable by root. Credential rotation is one file on one host. - Enforced TLS to the upstream.
smtp_tls_security_level = verifyplussmtp_tls_CAfilemeans the relay refuses to send if the upstream’s certificate isn’t valid for the hostname you configured. The defaultmaywould silently downgrade to cleartext on a TLS handshake error — useful for inbound, dangerous for outbound to a third-party.
What we deploy: Postfix 3.6+ on Ubuntu 22.04, configured as relayhost to one upstream, smtp_sasl_auth_enable = yes with credentials in /etc/postfix/sasl_passwd (mode 600, hashed), strict TLS to upstream, no inbound port 25 listener, postscreen disabled (no untrusted inbound), and rate caps via smtp_destination_rate_delay and default_destination_concurrency_limit. The local listener binds to a private IP on port 587 with permit_mynetworks for the application subnet and reject for anything else.
How to verify
After install, three things to check:
# 1. The relay is listening only on the private interface
ss -lntp | grep -E ':(25|587)\b'
# Should show port 587 on 10.x.x.x (or your private IP), nothing on 0.0.0.0
# 2. TLS to upstream is enforced and working
postconf -n | grep smtp_tls
# smtp_tls_security_level = verify
# smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
# 3. End-to-end test from an app server
swaks --to deliverability-check@example.com \
--from monitor@your-domain.example \
--server 10.10.0.50:587 \
--header "Subject: smarthost test $(date +%s)"
# Then on the smarthost:
journalctl -u postfix -f
# Look for "relay=upstream-smtp.example.net" and "status=sent"
If the test message logs Trust anchor not found or Verify failure, your upstream’s hostname doesn’t match the cert or your CA bundle is missing the chain — fix this rather than relaxing verify to encrypt.
The fix
Save as /root/setup-postfix-smarthost.sh and edit the variables at the top before running. The script installs Postfix, writes a hardened main.cf, generates the SASL credential map, configures rate caps, sets a private-IP bind, and includes a post-install smoke test.
#!/usr/bin/env bash
set -euo pipefail
### Ubuntu 22.04 Postfix smarthost installer
### Outbound-only relay to a single upstream over enforced TLS
### Listens on a private IP only; application servers connect on port 587
if [[ "$(id -u)" -ne 0 ]]; then
echo "Run as root."
exit 1
fi
# === EDIT THESE ===
RELAY_HOSTNAME="smarthost.internal.example.com"
PRIVATE_IP="10.10.0.50"
APP_SUBNET="10.10.0.0/24"
UPSTREAM_HOST="smtp.upstream.example"
UPSTREAM_PORT="587"
UPSTREAM_USER="postfix-relay@your-domain.example"
UPSTREAM_PASS="CHANGE_ME_UPSTREAM"
# Cap delivery to upstream so a runaway loop can't blow the upstream quota
RATE_DELAY="1s"
CONCURRENCY_LIMIT="10"
# ==================
echo "==> Updating base OS"
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get -y upgrade
DEBIAN_FRONTEND=noninteractive apt-get -y install \
postfix postfix-pcre libsasl2-modules ca-certificates \
swaks mailutils fail2ban ufw chrony
systemctl enable --now chronyd || systemctl enable --now chrony
echo "==> Setting hostname"
hostnamectl set-hostname "${RELAY_HOSTNAME}"
echo "==> Writing /etc/postfix/main.cf"
cat > /etc/postfix/main.cf <<EOF
# Core
compatibility_level = 3.6
myhostname = ${RELAY_HOSTNAME}
mydomain = $(echo ${RELAY_HOSTNAME} | cut -d. -f2-)
myorigin = \$mydomain
inet_interfaces = 127.0.0.1, ${PRIVATE_IP}
inet_protocols = ipv4
# Do NOT accept mail for any domain locally
mydestination =
local_recipient_maps =
local_transport = error:local delivery disabled on this host
# Who can submit
mynetworks = 127.0.0.0/8, ${APP_SUBNET}
smtpd_relay_restrictions =
permit_mynetworks,
reject_unauth_destination
smtpd_recipient_restrictions =
permit_mynetworks,
reject_unauth_destination
# No incoming TLS — this host is not on the public internet for SMTP
smtpd_tls_security_level = none
# === Outbound to upstream ===
relayhost = [${UPSTREAM_HOST}]:${UPSTREAM_PORT}
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous
smtp_tls_security_level = verify
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
smtp_tls_protocols = >=TLSv1.2
smtp_tls_mandatory_protocols = >=TLSv1.2
smtp_tls_ciphers = high
smtp_tls_mandatory_ciphers = high
smtp_tls_loglevel = 1
smtp_tls_session_cache_database = btree:\${data_directory}/smtp_scache
# Rate cap to upstream
default_destination_concurrency_limit = ${CONCURRENCY_LIMIT}
smtp_destination_rate_delay = ${RATE_DELAY}
# Queue handling
maximal_queue_lifetime = 1d
bounce_queue_lifetime = 1d
minimal_backoff_time = 300s
maximal_backoff_time = 1800s
queue_run_delay = 300s
# Size limits
message_size_limit = 26214400
mailbox_size_limit = 0
# Hide internal hostnames in headers
smtp_header_checks = pcre:/etc/postfix/header_checks.pcre
# Logs
maillog_file = /var/log/postfix.log
EOF
echo "==> Writing SASL credential map"
cat > /etc/postfix/sasl_passwd <<EOF
[${UPSTREAM_HOST}]:${UPSTREAM_PORT} ${UPSTREAM_USER}:${UPSTREAM_PASS}
EOF
chmod 600 /etc/postfix/sasl_passwd
postmap /etc/postfix/sasl_passwd
chmod 600 /etc/postfix/sasl_passwd.db
echo "==> Writing header_checks"
cat > /etc/postfix/header_checks.pcre <<'EOF'
# Strip Received headers that leak internal IPs
/^Received: from .*\(.* \[10\..*\]\)/ IGNORE
/^X-Originating-IP:/ IGNORE
EOF
echo "==> Configuring master.cf to disable inbound port 25"
# Keep submission (587) on private interface only; remove the public smtp(25) listener
sed -i 's/^smtp\s*inet/#smtp inet/' /etc/postfix/master.cf
# Add the submission service bound to private IP
cat >> /etc/postfix/master.cf <<EOF
${PRIVATE_IP}:587 inet n - y - - smtpd
-o smtpd_tls_security_level=none
-o smtpd_client_restrictions=permit_mynetworks,reject
-o smtpd_recipient_restrictions=permit_mynetworks,reject_unauth_destination
EOF
echo "==> Firewall"
ufw default deny incoming
ufw default allow outgoing
ufw allow ssh
ufw allow from ${APP_SUBNET} to ${PRIVATE_IP} port 587 proto tcp
ufw --force enable
echo "==> fail2ban"
cat > /etc/fail2ban/jail.d/sshd.local <<'EOF'
[sshd]
enabled = true
port = ssh
maxretry = 4
findtime = 10m
bantime = 1h
EOF
systemctl enable --now fail2ban
echo "==> Starting Postfix"
postfix check
systemctl enable --now postfix
systemctl restart postfix
echo "==> Smoke test"
sleep 2
ss -lntp | grep -E ':(25|587)\b' || true
cat <<EOF
========================================
DONE
========================================
Smarthost: ${RELAY_HOSTNAME} (${PRIVATE_IP})
Listen: ${PRIVATE_IP}:587 (private-only)
Upstream: ${UPSTREAM_HOST}:${UPSTREAM_PORT} (TLS verify)
Rate cap: ${CONCURRENCY_LIMIT} concurrent, ${RATE_DELAY} between deliveries
Test from an app server:
swaks --to you@example.com \\
--from monitor@your-domain.example \\
--server ${PRIVATE_IP}:587
Watch the log here:
tail -f /var/log/postfix.log
If "verify failure" appears, your upstream cert hostname doesn't match
${UPSTREAM_HOST}. Fix the hostname or the cert — do NOT downgrade to
smtp_tls_security_level = encrypt.
EOF
Make it executable, run it, then update each application server’s SMTP config to point at the relay’s private IP and port 587 with no authentication (the application subnet is trusted via mynetworks):
chmod 700 /root/setup-postfix-smarthost.sh
/root/setup-postfix-smarthost.sh
Two follow-up items operators forget. First, monitor the queue depth: postqueue -p | tail -1 returns “Mail queue is empty” or a line like -- 14 Kbytes in 5 Requests. Wire that into your monitoring stack with a threshold (anything over a few hundred messages is worth a look on a smarthost — they should drain quickly). Second, the rate cap is a backstop, not a setting you can ignore. If default_destination_concurrency_limit = 10 and your normal volume needs concurrency 50, mail will pile up in the queue and senders will see latency. Tune the cap to your actual throughput plus headroom; the protection is against the abnormal loop, not against your normal volume.
If you’d rather have Stack Harbor stand this up and run it under Managed Cloud Ops with queue depth and deferral-rate alerting in our monitoring plane, start the conversation.