Postfix email relay server with multiple SASL relay hosts depending on sender
Running an email server nowadays is far different from what it used to be and if you want better delivery rates, it usually involves someone else doing the sending. Some will simply use a free external SMTP service (GMail, Yahoo, etc.) and this works fine if you are only sending a few email notifications per day (<100). While most web applications allow for a quick configuration entering a username, password, and external SMTP server address, typically this style of configuration will cause a substantial delay for the user while the external connection is made and the email is sent.
For optimal performance, a relay host is configured on a local SMTP relay gateway. Your web app is then configured to use the local SMTP relay for sending. This ensures connections are fast and minimal time is spent waiting for the email to be "sent" from your web app. The queued messages now at your local SMTP gateway will then be relayed through your external SMTP relay host to their final destinations. Multiple relay hosts and SASL authentication parameters can be configured depending on the originating from sender address so multiple accounts can use the same Postfix SMTP relay gateway.
/etc/postfix/main.cf
| smtp_sender_dependent_authentication = yes
sender_dependent_relayhost_maps = hash:/etc/postfix/sender_relay
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
relayhost = [mail.isp.example]
# Alternative form:
# relayhost = [mail.isp.example]:submission
|
/etc/postfix/sasl_passwd
| # Per-sender authentication; see also /etc/postfix/sender_relay.
user1@example.com username2:password2
user2@example.net username2:password2
# Login information for the default relayhost.
[mail.isp.example] username:password
# Alternative form:
# [mail.isp.example]:submission username:password
|
/etc/postfix/sender_relay
| # Per-sender provider; see also /etc/postfix/sasl_passwd.
user1@example.com [mail.example.com]:submission
user2@example.net [mail.example.net]
|
The final touches
Don't forget to run the following three commands depending on exactly what has been updated when changes are made.
| postmap /etc/postfix/sasl_passwd
postmap /etc/postfix/sender_relay
/etc/init.d/postfix restart
|

External Resources