Exim: How to deliver mail locally and through SMTP relay

exim

I currently have all emails from Exim being forwarded to an outbound STMP server on our network. Here is a section of my Exim config that is sending mail out:

send_to_gateway:
driver = manualroute
transport = remote_smtp
route_list = * outbound-stmp.domain.com

This works great for emails that are sent out to external addresses from our web server. Where it doesn't work is when cron jobs send an email to the local root account. These local emails are being sent to our outbound server and bounced because they can't be sent to something like "root@localhost".

Is it possible through Exim to send emails destined for local accounts to stay on the local machine, and for any emails that are not local to have them go through the relay?

Best Answer

You need to define a router for your gateway, another router for local delivery, and the conditions under which they are used.

So, to locally deliver mail addressed to the *.localhost suffix, define the domainlist local_domains that includes "localhost", and include a domains clause in the first router. Exim checks the domains clause to decide if the router should be used.

exim.conf:

...

#                    MAIN CONFIGURATION SETTINGS                     #
domainlist local_domains = @ : localhost : localhost.localdomain : mydomain.com

...

begin routers

...

send_to_gateway:
  driver = manualroute
  domains = !+local_domains
  transport = remote_smtp
  route_list = * outbound-stmp.domain.com

...

localuser:
  driver = accept
  check_local_user
  transport = local_delivery
  cannot_route_message = Unknown user

...

The domains = !+local_domains condition means "use this router when the domain is not in the local_domains list." ! means not, + means dereference the following variable/list