What are we doing?
- Setting up Multiple sender_dependent_relayhost_maps in Postfix
Why?
The sender_dependent_relayhost_maps flag allows mail from different hostnames to be sent via different relays. For example, I relay mail for one particular domain through a Google Apps For Work account and other mail through Sendgrid.
How?
This guide assumes you have at least a Postfix SMTP server configured. Follow the guide here to setup Postfix.
Add the sender_dependent_relayhost_maps flag to Postfix’s config file
sudo nano /etc/postfix/main.cf
Add the following line-
sender_dependent_relayhost_maps = hash:/etc/postfix/relayhost_maps
Remove or comment out the relayhost = flag
#relayhost =
ctrl, then x followed by y to save and quit
Create and populate relayhost_maps
sudo nano /etc/postfix/relayhost_maps
Here, add domains or addresses (examples shown respectively), followed by the desired relay address
@example.com [smtp-relay.gmail.com]:587 [email protected] smtp.jonathanneilly.co.uk
ctrl, then x followed by y to save and quit
Create a db hash of the relay file
sudo postmap /etc/postfix/relayhost_maps
Reduce permissions to the bare minimum on the relay file
sudo chown postfix:postfix /etc/postfix/relayhost_maps.db
sudo chmod 400 /etc/postfix/relayhost_maps.db
Remove non-hashed relay file
sudo rm /etc/postfix/relayhost_maps
Force changes to take effect
sudo service postfix reload