11

I wish to set up Postfix to use an external relay depending on the destination hostname, ie:

  • If destination hostname is *.outlook.com, use relay some_smtp.example.com
  • If any other destination hostname, use local relay

What I mean by destination hostname is the hostname obtained from MX record. If the recipient domain has MX record microsoft-com.mail.protection.outlook.com, then use a different relay

I know it is possible to specify a relay depending on the sender address (using sender_dependent_relayhost_maps), but it's impractical in my situation.

The goal is to use a different relay for finicky destination hosts: maybe Mandrill, or another Postfix installation.

5 Answers 5

7

You can setup a transport map to selectively choose mails with @outlook.com as destination address and relay them via some_smtp.example.com as shown below.

Add the entry below to /etc/postfix/transport

outlook.com smtp:[some_smtp.example.com]

Add the entry below to /etc/postfix/main.cf

transport_maps = hash:/etc/postfix/transport

Restart postfix after the following command

sudo postmap /etc/postfix/transport
1
  • Thanks for your answer. Please see my edit about "destination hostname". Should have been more specific. Jan 29, 2015 at 16:48
7

Arul's answer was perfect for transport based on recipient domain. However, bencaue you refer to MX record hostname instead recipient domain, the answer was non-applicable.

One solution is using check_recipient_mx_access. Snippet from official docs

check_recipient_mx_access type:table

Search the specified access(5) database for the MX hosts for the RCPT TO domain, and execute the corresponding action. Note: a result of "OK" is not allowed for safety reasons. Instead, use DUNNO in order to exclude specific hosts from blacklists. This feature is available in Postfix 2.1 and later.

For your case, just put check_recipient_mx_access hash:/etc/postfix/finickydestination in appropriate place smtpd_*_restriction. In that file put the hostname

# /etc/postfix/finickydestination
.outlook.com FILTER smtp:[some_smtp.example.com]

Don't forget to postmap the file and execute postfix reload.

Reference(s):

1
  • 1
    This of course is not working (unfortunately). Put in the smtpd_recipient_restriction, results in: postfix/smtpd: warning: do not specify lookup tables inside SMTPD access maps postfix/smtpd: warning: define a restriction class and specify its name instead.
    – user221326
    Mar 31, 2015 at 14:50
2

As @user221326 (i don't have enough rep to comment myself) pointed out @masegaloeh answer will NOT work as check_recipient_mx_access expects a access table with a ACTION

More info in the man page

The action you want is FILTER so something like

.outlook.com FILTER smtp:[some_smtp.example.com]

Note if you have multiple FILTER only the last will fire so ensure this comes after everything else.

Furthermore .outlook.com will only catch sub/super domains (e.g. whatever.protection.outlook.com) if smtpd_access_maps is NOT in parent_domain_matches_subdomains, otherwise you want outlook.com (no leading dot)

Lastly keep in mind that this action applies to the whole message as soon as one recipients MX matches the domain, which should be a non-issue as long as your defined target isn't a MDA.

Cheers

1
  • This change is now propagated to the original @masegaloeh answer.
    – Marki555
    Jul 29, 2021 at 8:28
1

2021 Update

I did try all this, but it did not seems to work. The solution I present was "assembled" from several sources.


in main.cf you need to add following:

smtpd_sender_restrictions   =
        check_recipient_mx_access   pcre:/etc/postfix/mxtransport

then you do /etc/postfix/mxtransport as "usual":

/google\.com$/              FILTER smtp:[smtp.com]:25
/googlemail\.com$/          FILTER smtp:[smtp.com]:25
/protection\.outlook\.com$/ FILTER smtp:[smtp.com]:25

/spamexperts\.com$/         FILTER smtp:[smtp.com]:25
/spamexperts\.net$/         FILTER smtp:[smtp.com]:25
/spamexperts\.eu$/          FILTER smtp:[smtp.com]:25

This assures, that emails sent to google / outlook / spamexperts clients (for example [email protected], [email protected], [email protected] etc), will go via third party route (smtp.com) instead via local IP.


I am still using Postfix 2.10.1 (CentOS 7)

1
  • 1
    If it saves others the head scratching - the PCRE dictionary isn't installed out of the box with Ubuntu, but can be grabbed with sudo apt install postfix-pcre
    – Moby Duck
    Nov 20, 2021 at 8:36
0

As commented above: "For your case, just put check_recipient_mx_access hash:/etc/postfix/finickydestination in appropriate place smtpd_*_restriction. In that file put the hostname"

smtpd_sender_restrictions is not the appropriate place, as at the point in the smtp transaction, postfix does not know who the receiver is.

Also if you had any more FILTER statements, such as spam/virus scanning, it would override that FILTER statement, if they happened after smtpd_sender_restrictions.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .