Setup Postfix Sender Dependent Relay Hosts on Ubuntu


What are we doing?

  • Setting up Multiple sender_dependent_relayhost_maps in Postfix



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.


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/

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   []:587
[email protected]

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


Leave a Reply

Your email address will not be published. Required fields are marked *