# multiple relayhost



## indul (May 10, 2010)

Hi Members,

I have a FreeBSD 8.0 server with postfix. My ISP blocked the outgoing port 25, so I relay all the mails to another server with relayhost=server1.tld:587 and smtp auth. It works fine.
I would like to add a backup relayhost. If the first fail, the second server relays the mails.
How can I do that?

Thanks for your answers.


----------



## DutchDaemon (May 11, 2010)

I know of no ready mechanism for something like that. Maybe a second configuration file with the other SMTP server in it, and a script that checks the availability of the first SMTP server (e.g. using net/tcping), reloading Postfix with the alternate configuration file when it becomes unavailable, and switching back to the original configuration file when it comes back, would do the trick.


----------



## phoenix (May 11, 2010)

Don't put [] around the relayhost entry.  That way, Postfix will do a proper DNS MX lookup on it.  And will fail to secondary MX entries correctly.  The [] is used to tell postfix to just connect directly to that host without doing any DNS MX lookups.

Then you just need to configure the MX entry for the domain correctly on whatever DNS server you use.


----------



## DutchDaemon (May 11, 2010)

But this is a relayhost setting, not an MX server record. He's not delivering directly to the destination MX, but to an intermediate smarthost. So he needs an SMTP fallback, not an MX fallback


----------



## SirDice (May 11, 2010)

http://www.postfix.org/postconf.5.html#fallback_relay


----------



## phoenix (May 11, 2010)

Hrm, according to the docs, what I listed should work.  "In the case of SMTP, specify a domain name, hostname, hostnameort, [hostname]ort, [hostaddress] or [hostaddress]ort. The form [hostname] turns off MX lookups."

IOW, if you specify the relayhost in [], then it only does an A record lookup and then delivers mail to that IP.

If you specify the relayhost without the [], then it does a standard MX record lookup.  Which means it will follow the standard MX process of "try the IP with the highest precedence, if it fails, try the next one".

Of course, that would negate the need for a fallback_relay, so maybe it doesn't work the way the docs make it sound like it should.


----------



## DutchDaemon (May 11, 2010)

It appears that that setting is only applicable to the following scenario:

a. You try to send email to the destination domain's MX server #1 (say, weight 10) - not reachable (port 25 block) -->
b. You try to send email to the destination domain's fallback MX server #2 (say, weight 20) - not reachable (port 25 block) -->
c. You try to send email to an alternative SMTP server for further processing

Problems:

1. This will probably delay the email by 2 * 75 seconds to begin with (I think that's a pretty standard smtp timeout value, i.e. 75 seconds per try)
2. This will still fail when the alternative SMTP server is unreachable.

What OP wants, is:

1. forget about straight MX delivery altogether (no direct MX delivery is even attempted, everything is 'smarthosted') -->
2. drop mail at the alternative SMTP server if and when available -->
3. configure and use a fallback alternative SMTP server for when option 2 fails.

So unless one can define two smarthosts under the fallback_relay setting, preferably in some fixed order and without any attempts to perform direct-to-MX delivery, OP is out of luck with that setting


----------



## SirDice (May 11, 2010)

odd, as I found that link here

http://irbs.net/internet/postfix/0507/0792.html

Mind you, I don't use postfix. But the way I read that info was, _if_ relayhost can't be contacted it would use fallback_relayhost. Which is exactly what the OP is asking, and also what the OP on the postfix mailinglist asked.


----------



## DutchDaemon (May 11, 2010)

Maybe the documentation could be improved .. I read this:



> Optional list of relay hosts for _SMTP destinations that can't be found or that are unreachable_



as: this is the server that will be used to deliver your mail to _if all else (i.e. trying to deliver to the destination domain's MX record(s)) fails_.

Which is not the definition of a smarthost (= SMTP server to send all outbound mail to _immediately_), of which OP wants two: a primary smarthost and a fallback smarthost, with no direct-to-MX delivery attempted whatsoever. I think that 'fallback relayhost' means that direct-to-MX will always be attempted first, leading to delays.


----------



## SirDice (May 12, 2010)

Hmm.. Both an MX mail server and a smarthost are SMTP destinations, at least from the MTA's point of view. Did anyone actually try it?


----------



## nlopez (Nov 22, 2010)

I can confirm that this works in the way the original poster requested. I tested today with the following main.cf setup for use with dnsexit:


```
# http://www.postfix.org/STANDARD_CONFIGURATION_README.html
myorigin = $mydomain
inet_interfaces = loopback-only
local_transport = error:local delivery is disabled
# http://www.dnsexit.com/support/mailrelay/postfix.html
relayhost = [relay.dnsexit.com]
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/relay_passwd
smtp_sasl_security_options =
# http://www.postfix.org/postconf.5.html#fallback_relay
fallback_relay = relaybackup.dnsexit.com
```


----------

