Postfix smtp_fallback_relay for deferred messages to a single domain

postfix

I use Postfix to send messages to a mail server outside my organization which frequently rejects/defers my mail. My Postfix server sees that these messages are deferred and tries again, eventually getting through. Final delivery can take up to an hour, which makes my users unhappy. In comparison, mail from my Postfix server to other hosts works normally.

I have now found out about a second, unofficial MX for this domain that does not reject/defer mail. This second MX does not appear when doing a DNS MX query for the domain. Therefore, for the problem domain I would like to use this second MX as a fallback. That is: whenever mail is deferred by the primary MX, try again on the unofficial second MX.

I see that there is already a postfix configuration "smtp_fallback_relay". However the documentation seems to indicate that I can not restrict usage of the fallback to a single domain. The documentation also doesn't mention deferred message handling.

So is there a way to configure a single-domain, deferred-retry fallback host in Postfix?

For reference, I am including my postconf output (the host names and ip addresses are fake):

alias_database = hash:/etc/aliases
alias_maps = hash:/etc/aliases, hash:/etc/postfix/legacy_mailman, ldap:/etc/postfix/ldap-aliases.cf
append_dot_mydomain = no
biff = no
config_directory = /etc/postfix
default_destination_concurrency_limit = 2
inet_interfaces = all
inet_protocols = all
local_destination_concurrency_limit = 2
local_recipient_maps = $alias_maps
mailbox_size_limit = 0
mydestination = myhost.my.network, localhost.my.network, localhost, my.network
myhostname = myhost.my.network
mynetworks = 127.0.0.0/8, [::ffff:127.0.0.0]/104, [::1]/128, 10.10.10.0/24
myorigin = my.network
readme_directory = no
recipient_delimiter = +
relay_domains = $mydestination
relayhost = 
smtp_fallback_relay = the.problem.host
smtp_header_checks = 
smtpd_banner = $myhostname ESMTP $mail_name
virtual_alias_maps = hash:/etc/postfix/virtual

Best Answer

smtp_fallback_relay does not handle deferred messages; it will be tried immediately when the primary destination is unavailable.
This is normally what you want - as the setting indicates, it is a fallback for relaying mail.

I suggest you investigate WHY this mail server rejects your messages.

That said, there are several ways to approach this.
The first - and easiest - solution would be to fake this MX locally so postfix includes it during normal delivery.
To achieve this, set smtp_host_lookup to native; this will consult your /etc/hosts file as well as DNS (via the native system resolver, or, more specifically, via whatever is configured in nsswitch.conf.)

You could also install something like dnsmasq to allow you to override or add specific DNS entries to existing domains; if both relays have MX records this is the only DNS-based solution, since /etc/hosts only supports A records.

The postfix solution involves some lookup trickery, which is only possible when using SQL maps:

You have to add a transport_maps entry consisting of a lookup that can return an alternative result based on out-of-band manipulation; for instance, a periodic check for MX availability (via cron or the like) could replace a regex lookup table that alters the nexthop for this specific domain; postfix will pick up changes to these tables immediately.

Related Topic