To relay all outgoing Postfix mail through an authenticated provider, set relayhost to the provider's host and port in square brackets, enable SASL authentication with smtp_sasl_auth_enable, store the credentials in a hashed map referenced by smtp_sasl_password_maps, and require TLS with smtp_tls_security_level = encrypt. Then run postmap on the credentials file and reload Postfix.
This guide gives a complete working configuration for port 587 (STARTTLS) and port 465 (implicit TLS), explains each parameter, shows how to rewrite the sender, how to test and read the queue, and how to fix the log errors that appear most often. Commands assume a Debian or Ubuntu style layout; paths and package names differ on other distributions.
When a relay host makes sense
Sending directly from a server to recipients' mail servers requires a clean IP address, a matching reverse DNS name and consistent reputation, and many networks block outbound port 25 altogether. A relay host (also called a smarthost) changes that: Postfix accepts and queues the mail locally, then hands it to a provider that handles the delivery, reputation and retries across the internet. You keep Postfix for local submission, queueing and aliases, and let a specialist carry the last mile.
A relay does not remove your responsibilities. The From domain still needs SPF, DKIM and DMARC alignment with the sending service; see authenticating your sending domain. For background on what a relay does, see SMTP relay explained.
Parameters at a glance
| Parameter | Purpose | Typical value |
|---|---|---|
| relayhost | Send all non-local mail to this host | [smtp.migosmtp.com]:587 |
| smtp_sasl_auth_enable | Authenticate to the remote server | yes |
| smtp_sasl_password_maps | Where credentials are stored | hash:/etc/postfix/sasl_passwd |
| smtp_sasl_security_options | Disallow anonymous mechanisms | noanonymous |
| smtp_tls_security_level | TLS policy for outgoing connections | encrypt |
| smtp_tls_CAfile | CA bundle used to verify the relay | /etc/ssl/certs/ca-certificates.crt |
| smtp_tls_wrappermode | Implicit TLS for port 465 | yes (465 only) |
| inet_interfaces | Where Postfix listens | loopback-only for a send-only host |
The square brackets around the host in relayhost tell Postfix to use the name as given and skip MX lookups. Leave them out and Postfix will look for MX records of that name, which is rarely what you want for a relay.
Step 1: install the SASL libraries
sudo apt update
sudo apt install postfix libsasl2-modules ca-certificatesThe libsasl2-modules package supplies the PLAIN and LOGIN mechanisms that Postfix uses for client authentication. Without it you will see errors that no mechanism is available. During installation choose the "Internet with smarthost" or "Satellite system" type if prompted; you can adjust everything in main.cf afterwards.
Step 2: store the credentials
sudo sh -c 'cat > /etc/postfix/sasl_passwd <<EOF
[smtp.migosmtp.com]:587 smtp-username:smtp-password
EOF'
sudo chmod 600 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd
sudo chmod 600 /etc/postfix/sasl_passwd.dbThe key in this file must match the relayhost value exactly, including the brackets and port. If you change the port, change the key and run postmap again. Keep the file readable only by root, since it holds a live password.
Step 3: configure main.cf for port 587
sudo postconf -e 'relayhost = [smtp.migosmtp.com]:587'
sudo postconf -e 'smtp_sasl_auth_enable = yes'
sudo postconf -e 'smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd'
sudo postconf -e 'smtp_sasl_security_options = noanonymous'
sudo postconf -e 'smtp_tls_security_level = encrypt'
sudo postconf -e 'smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt'
sudo postconf -e 'smtp_tls_loglevel = 1'
sudo postconf -e 'inet_interfaces = loopback-only'
sudo postfix check && sudo systemctl reload postfixWith encrypt, Postfix refuses to deliver unless the connection is encrypted, which protects the password. Do not use may for an authenticated relay: it would silently fall back to plain text if the upgrade failed. Setting smtp_tls_loglevel to 1 logs the negotiated protocol and certificate verification result, which is useful while you test; lower it later. For the difference between ports and TLS modes, see choosing between ports 25, 465 and 587 and SSL, TLS and STARTTLS.
Variant: port 465 with implicit TLS
# /etc/postfix/sasl_passwd key changes to match
[smtp.migosmtp.com]:465 smtp-username:smtp-password
sudo postconf -e 'relayhost = [smtp.migosmtp.com]:465'
sudo postconf -e 'smtp_tls_wrappermode = yes'
sudo postconf -e 'smtp_tls_security_level = encrypt'
sudo postmap /etc/postfix/sasl_passwd
sudo systemctl reload postfixWrapper mode makes the Postfix SMTP client start TLS immediately after connecting, which is what port 465 expects. Do not set it for port 587. If you need both ports for different relays, define a transport map rather than changing the global setting.
Step 4: rewrite the sender if needed
Local mail often comes from addresses like [email protected], which a relay may reject and receivers will not trust. Rewrite them to an address on your authenticated domain.
# /etc/postfix/sender_canonical
root [email protected]
@server.internal @example.com
sudo postmap /etc/postfix/sender_canonical
sudo postconf -e 'sender_canonical_maps = hash:/etc/postfix/sender_canonical'
sudo systemctl reload postfixAlso check that the envelope sender is acceptable to your relay, since many relays require a verified domain. See envelope from.
Test the relay and read the queue
- Send a test:
echo 'Relay test' | mail -s 'Postfix relay' [email protected]. - Watch the log:
sudo tail -f /var/log/mail.logon Debian-style systems, orjournalctl -u postfix -fwhere mail goes to the journal. - Look for
status=sentin the line from the smtp process, plus the TLS line when loglevel is 1. - List the queue with
postqueue -p; flush withpostqueue -fafter fixing a problem. - Inspect the effective configuration with
postconf -nto confirm your changes were loaded. - Check the received message headers for SPF and DKIM results. See reading email headers.
If the queue grows, read the reason with postqueue -p; deferred messages show the last error. The meaning of deferrals is covered in deferred mail explained, and queue behaviour in mail queues.
Using different relays for different senders
If one host sends mail for several brands or streams, you can route by sender instead of using a single global relay. Postfix supports this with sender_dependent_relayhost_maps, a lookup table that maps a sender address or domain to a relay, and smtp_sender_dependent_authentication = yes so that the matching credentials from the password map are used for each relay. Add a line per relay to sasl_passwd, add the sender mapping, run postmap on both files, and reload. This is useful for keeping transactional and promotional traffic apart; see separating mail streams. Test each sender separately, because a typo in one mapping only affects that sender and is easy to miss.
Remember what Postfix does not do for you. It does not sign messages with DKIM unless you add a milter such as OpenDKIM, and when a relay provider signs on your behalf, your DNS must publish the provider's selector records. It also does not manage suppression of bounced addresses: that is the job of your application or provider. Keep the local queue lifetime reasonable with maximal_queue_lifetime, so that a message does not retry for days after it stopped being useful; the default is several days, and shortening it suits time-sensitive mail such as one-time codes.
Troubleshooting
| Log message | Cause | Fix |
|---|---|---|
| SASL authentication failed; server said: 535 | Wrong username or password | Fix sasl_passwd, run postmap, reload. See fixing 535 errors |
| no mechanism available | libsasl2-modules missing | Install the package and restart Postfix |
| SASL authentication failed: no worthy mechs found | No mechanism offered by the server is installed or allowed | Install libsasl2-modules; review smtp_sasl_mechanism_filter and TLS-related security options |
| Server certificate not trusted | CA file missing or wrong path | Check smtp_tls_CAfile and ca-certificates |
| Cannot start TLS: handshake failure on 465 | wrappermode not set for implicit TLS | Set smtp_tls_wrappermode = yes for 465 only |
| Connection timed out | Outbound port blocked or wrong host | Test the port from the host; see connection timeouts |
| Relay access denied | Sender or recipient not allowed by relay | See relay denied errors |
| Sender address rejected | Envelope sender not verified at the relay | Rewrite the sender to an authorised address |
Hardening and next steps
- Keep
inet_interfaces = loopback-onlyon send-only hosts so Postfix is not an open relay; check with open relay guidance. - Set
mynetworksnarrowly. A broad value lets other machines relay through you. - Use a sending-only credential and rotate it after any staff change.
- Monitor queue length and bounce rates; alert when the queue stays non-empty. See SMTP monitoring.
- Back up main.cf and the maps before changes, and use
postconf -nfor support requests, with passwords removed.
Once relaying works, add bounce handling and delivery events from your provider so that the application stops sending to rejected addresses.
Relay Postfix through MigoSMTP Use relayhost with SASL on port 587 or 465 and keep local queueing.
References and further reading
Frequently asked questions
What does relayhost do in Postfix?
It sends all mail that Postfix does not deliver locally to the named host instead of looking up each recipient domain. Square brackets skip MX lookups.
How do I authenticate Postfix to a relay?
Enable smtp_sasl_auth_enable, store the host and credentials in a file referenced by smtp_sasl_password_maps, and run postmap on it.
Which TLS setting should I use for relaying?
Use smtp_tls_security_level = encrypt on port 587, and add smtp_tls_wrappermode = yes for port 465.
Why does Postfix say SASL authentication failed?
Usually the username or password is wrong, the map key does not match relayhost, or postmap was not run after editing.
Can Postfix queue mail if the relay is down?
Yes. Postfix keeps messages in its queue and retries, which is a key reason to use it over a client with no queue.