exim: select smarthost according to sender address

When sending mails from my systems, I usually want to use different smarthosts according to the sender address. If I use the sender address vwegert@web.de, I want the mail to be delivered via smtp.web.de, if I use one of my @volker-wegert.de addresses, I want to use the smarthost at Manitu and so on. With exim, this is possible with a few configuration adaptions.

First create a file named /etc/exim/smarthosts that will contain the routing information. The contents look like this:

:     smarthost=smtp.web.de             auth_name=vwegert auth_pass=secret
*@volker-wegert.de: smarthost=smtp.volker-wegert.de   auth_name=vwegert auth_pass=veeerysecret

The contents are fairly self-explanatory. As you can see, wildcards are possible for the sender address. Since this file contains sensitive information, a restrictive permission setting (-rw-------) sould be used. Now head on to exim.conf. In the router configuration, disable domain_literal if it's not already disabled and replace it with the following entry:

   condition = ${extract{smarthost}{${lookup{$sender_address}wildlsearch{/etc/exim/smarthosts}{$value}fail}}}
   driver = manualroute
   domains = ! +local_domains
   route_list = "* ${extract{smarthost}{${lookup{$sender_address}wildlsearch{/etc/exim/smarthosts}{$value}fail}}}"
   transport = remote_smtp

Caution: The order in which the routers are defined matters!

This router definition can be read as "If I find a line in /etc/exim/smarthosts whose key as wildcard matches the sender adress and where a smarthost is set, then use this smarthost as next MTA and transfer the message using SMTP." This is not quite enough because I want to authenticate using the password when sending mail. This requires an additional line in the transport remote_smtp:

  hosts_try_auth = smtp.volker-wegert.de:smtp.web.de

as well as the following configuration in the authenticators section:

     driver = cram_md5
     public_name = CRAM-MD5
     client_name = "${extract{auth_name}{${lookup{$sender_address}wildlsearch{/etc/exim/smarthosts}{$value}fail}}}"
     client_secret = "${extract{auth_pass}{${lookup{$sender_address}wildlsearch{/etc/exim/smarthosts}{$value}fail}}}"

That's it - have fun.


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

hmmm this isnt working for me

my users file
/etc/exim4$ cat smtp_users
*@gmail.com: smarthost=smtp.gmail.com::587 auth_name=mxxxxo@gmail.com auth_pass=xxxxxx

and then Im gettting message frozen
from the exim log when it tries to deliver for the 1st time.

2011-11-20 12:44:50 1RS6lN-0005cr-OZ ** xxxxx@yahoo.co.uk R=smarthost_auto T=remote_smtp: SMTP error from remote mail server after MAIL FROM: SIZE=1619: host smtp.gmail.com []: 530-5.5.1 Authentication Required. Learn more at\n530 5.5.1 http://mail.google.com/support/bin/answer.py?answer=14257 j5sm3447713wix.20
2011-11-20 12:44:51 1RS6lO-0005cu-R7 <= <> R=1RS6lN-0005cr-OZ U=Debian-exim P=local S=1615
2011-11-20 12:44:51 1RS6lN-0005cr-OZ Completed

when i look at the frozen message
144P Received: from Debian-exim by mxxxx.co.uk with local (Exim 4.72)
id 1RS6ck-0005bE-9a

; Sun, 20 Nov 2011 12:35:54 +0000
038 Date: Sun, 20 Nov 2011 12:35:54 +0000
046I Message-Id:
040 X-Failed-Recipients: pxxxx@yahoo.co.uk
029 Auto-Submitted: auto-replied
051F From: Mail Delivery System
022T To: mxxxx@gmail.com
059 Subject: Mail delivery failed: returning message to sender
I *think* the next step should still work because it still needs to go to gmail
and it means that the lookup is failing.
Any idea why ?
exim -d -M 1RS6ck-0005bE-9a
--------> smarthost_auto router <--------
local_part=mxxx domain=gmail.com
checking domains
gmail.com in "www.mxxx.co.uk:home.nw"? no (end of list)
gmail.com in "! +local_domains"? yes (end of list)
checking "condition"
search_open: wildlsearch "/etc/exim4/smtp_users"
search_find: file="/etc/exim4/smtp_users"
key="" partial=-1 affix=NULL starflags=0
LRU list:
internal_search_find: file="/etc/exim4/smtp_users"
type=wildlsearch key=""
smarthost_auto router skipped: condition failure