Linux, Postfix

Relaying emails dependent on either sender or receiver

Introduction:

I use a Relay SMTP service for sending my emails but I want to use it only for certain conditions like the sender address or the receiver address. Here is how it’s done:

Relay to relay host depending on sender address

Add in /etc/postfix/main.cf

sender_dependent_relayhost_maps = hash:/etc/postfix/relayhost_map

Create file /etc/postfix/relayhost_map and add this content:

@example.com [relay.server.com]:25
someone@example1.com [relay.server.com]:25

Run the following commands:

postmap /etc/postfix/transport
postfix reload

Result:
– Every mail sent from @example.com or from someone@example1.com will be relayed to relay.server.com on port 25
– All other emails will be delivered directly using SMTP

Relay to relay host depending to receiver address

Add in /etc/postfix/main.cf:

transport_maps = hash:/etc/postfix/transport

Add in /etc/postfix/transport:

@example4.com relay:[relay.server.com]:25
someone@example5.com relay:[relay.server.com]:25
someoneelse@example6.com local
*                        smtp

Run the following commands

postmap /etc/postfix/transport
postfix reload

Result:
– Mails sent to @example4.com or to someone@example5.com will be relayed to relay.server.com on port 25.
– Mails going to someoneelse@example6.com will be delivered directly to local postfix server.
– All other emails will be delivered normally via smtp transport.

For more info on this subject see:
https://www.linuxbabe.com/mail-server/postfix-transport-map-relay-map-flexible-email-delivery

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.