What is the best practice when one has to implement this sort of ruleset in Postfix?

  • Email sent from local domain1.com, domain2.com to example.com, example1.com ought to be relayed to smtp.external.com.
  • Email sent to everyone else @example.com and example1.com ought to be relayed locally.

I know there is a way to do a relay filtering recipient address using transport_maps, but I do not know how to filter depending on the sender address too.

| improve this question | | | | |

There's a way (although not quite clean in my opinion), matching it with the header_checks parameter. For example:

header_checks = pcre:/etc/postfix/my_relays

Now in /etc/postfix/my_relays:

/^From:.*asender\@domain1\.com/  smtp1:[host1.example.com]
/^From:.*anothersender\@domain2\.com/  smtp2:[host2.example.com]

The smtp1 and smtp2 actions are only needed if you need to authenticate on the host1.example.com or host2.example.com servers by password, otherwise replacing them with the smtp keyword would be enough.

If you need to authenticate against those servers, you need to define what is smtp1 and smtp2 in your master.cf file:

smtp1    unix  -       -       -       -       10       smtp
    -o smtp_sasl_password_maps=hash:/etc/postfix/smtp1.relay
smtp2    unix  -       -       -       -       10       smtp
    -o smtp_sasl_password_maps=hash:/etc/postfix/smtp2.relay

As a last step, your smtp[12].relay files have to include the authentication parameters to connect to those hosts (I remark this is only needed if you need authentication).

[hostX.example.com]   userX:passwordX
| improve this answer | | | | |
  • This command is perfect to filter mail senders, but I want to filter both senders and receivers at the same time. With a certain incoming mail and outgoing mail, filter using an external relay. – Contertulio Jan 20 '17 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.