New guide: how to warm up a dedicated IP without hurting your reputation. Read now →
Log in
MigoSMTP
SMTP and APIUpdated 4 Nov 2026

Postfix Relay Configuration: relayhost, SASL Authentication and TLS

How to configure Postfix to relay all outgoing mail through an authenticated SMTP relay: relayhost, SASL password maps, mandatory TLS on 587 or 465, sender rewriting, testing, queue checks and common log errors.

MO
Written by Mateus Oliveira
Senior SMTP and MTA Engineer
Reviewed by Sofia Marino
  • Published8 May 2024
  • Read time7 min
  • Length1,468 words
Share this guide

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.

Core parametersrelayhost, smtp_sasl_auth_enable, smtp_sasl_password_maps, smtp_tls_security_level
Port 587STARTTLS: smtp_tls_security_level = encrypt
Port 465Implicit TLS: also set smtp_tls_wrappermode = yes
Secrets file/etc/postfix/sasl_passwd, mode 600, compiled with postmap

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

ParameterPurposeTypical value
relayhostSend all non-local mail to this host[smtp.migosmtp.com]:587
smtp_sasl_auth_enableAuthenticate to the remote serveryes
smtp_sasl_password_mapsWhere credentials are storedhash:/etc/postfix/sasl_passwd
smtp_sasl_security_optionsDisallow anonymous mechanismsnoanonymous
smtp_tls_security_levelTLS policy for outgoing connectionsencrypt
smtp_tls_CAfileCA bundle used to verify the relay/etc/ssl/certs/ca-certificates.crt
smtp_tls_wrappermodeImplicit TLS for port 465yes (465 only)
inet_interfacesWhere Postfix listensloopback-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-certificates

The 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.db

The 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 postfix

With 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 postfix

Wrapper 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 postfix

Also 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

  1. Send a test: echo 'Relay test' | mail -s 'Postfix relay' [email protected].
  2. Watch the log: sudo tail -f /var/log/mail.log on Debian-style systems, or journalctl -u postfix -f where mail goes to the journal.
  3. Look for status=sent in the line from the smtp process, plus the TLS line when loglevel is 1.
  4. List the queue with postqueue -p; flush with postqueue -f after fixing a problem.
  5. Inspect the effective configuration with postconf -n to confirm your changes were loaded.
  6. 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 messageCauseFix
SASL authentication failed; server said: 535Wrong username or passwordFix sasl_passwd, run postmap, reload. See fixing 535 errors
no mechanism availablelibsasl2-modules missingInstall the package and restart Postfix
SASL authentication failed: no worthy mechs foundNo mechanism offered by the server is installed or allowedInstall libsasl2-modules; review smtp_sasl_mechanism_filter and TLS-related security options
Server certificate not trustedCA file missing or wrong pathCheck smtp_tls_CAfile and ca-certificates
Cannot start TLS: handshake failure on 465wrappermode not set for implicit TLSSet smtp_tls_wrappermode = yes for 465 only
Connection timed outOutbound port blocked or wrong hostTest the port from the host; see connection timeouts
Relay access deniedSender or recipient not allowed by relaySee relay denied errors
Sender address rejectedEnvelope sender not verified at the relayRewrite the sender to an authorised address

Hardening and next steps

  • Keep inet_interfaces = loopback-only on send-only hosts so Postfix is not an open relay; check with open relay guidance.
  • Set mynetworks narrowly. 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 -n for 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.

Explore the SMTP relay

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.

Topics#postfix#relayhost#sasl#tls#smtp relay#linux
Was this guide helpful?Your answer helps us improve our documentation.

Start sending in minutes.

Create an account, add your domain and send your first email.

Get started Talk to sales