Use different relay in postfix

Solution 1:

In postfix, you can use transport_maps to specify different relay hosts for different domains. In file /etc/postfix/main.cf, you need to include a line:

transport_maps = hash:/etc/postfix/transport

The transport maps file has the following syntax:

# Syntax: .domain transport:relay_host
# Specifies specific domains for local delivery
mydomain1.com :
mydomain2.com :

# Specify domains that need to be relayed through 192.168.1.1
anotherdomain1.com relay:192.168.1.1
anotherdomain2.com relay:192.168.1.1

Don't forget to run postmap transport to generate the .db file and then reload/restart postfix process.

Solution 2:

Transport maps (in Khaled's answer) can change the relay of mail based on the domain.

You can also use header_checks and body_checks to change transports (and perform other actions) based on matching headers and content found in the body of the email. These can be regex or hash based. My example below is regex based.

In /etc/postfix/main.cf:

header_checks = regexp:/etc/postfix/header_checks

In /etc/postfix/header_checks:

/^Subject: Host [a-z0-9]* is down!/ FILTER relay:192.168.1.1
/^Subject: [^ ]* has posted a new blog entry./ FILTER relay:192.168.1.2
/^Message-ID: <[0-9a-z]*@dbserver.local>/ FILTER relay:192.168.1.1
/^Message-ID: <[0-9a-z]*@mydomain.com>/ FILTER relay:192.168.1.2

The body_checks work the same way as the header_checks.

You can set arbitrary headers in your app if you want something unique to filter on.