How to Choose a Smarthost Based on From Address in Exim4 on Debian

eximsmarthostsmtp

I want to route emails with the From: .*@host1.com through smtp.server1.com and emails with the From: .*@host2.com through smtp.server2.com.

For the moment, I have smarthost configured with dpkg-reconfigure exim4-config so that update-exim4.conf.conf contains the line dc_smarthost='smtp.server1.com::587'. That is, everything routes through smtp.server1.com.

I tried adding another router just before the definition of smarthost: in the config file, setting senders = .*@host2.com

smarthost_server2:
  debug_print = "R: smarthost_server2 for $local_part@$domain"
  driver = manualroute
  domains = ! +local_domains
  transport = remote_smtp_smarthost
  senders = .*@host2.com
  route_list = * smtp.server2.com byname
  host_find_failed = ignore
  same_domain_copy_routing = yes
no_more

but exim still routes everyting via smtp.server1.com. Do I use the senders condition correctly?

AFAIU, this should be a rather common setup for cases when people with emails .@google.com and .@gmail.com do not want to reveal in their @gmail.com email headers the fact that they are also google employees, so the routhing should be different.

Best Answer

OK, I got it. senders is what /etc/mailname provides, not the domain part of the From:

The following works:

begin routers

smarthost1:
  driver = manualroute
  domains = ! +local_domains
  condition = ${if match_domain{${domain:$h_From:}}{domain.com}{yes}{no}}
  transport = remote_domain_com_smtp
  route_data = domain.com::587
  ignore_target_hosts = <; 0.0.0.0 ; 127.0.0.0/8 ; ::1

smarthost2:
  driver = manualroute
  domains = ! +local_domains
  condition = ${if match_domain{${domain:$h_From:}}{domain.org}{yes}{no}}
  transport = remote_domain_org_smtp
  route_data = domain.org::587
  ignore_target_hosts = <; 0.0.0.0 ; 127.0.0.0/8 ; ::1


begin transports

remote_domain_com_smtp:
  driver = smtp
  message_size_limit = ${if > {$max_received_linelength}{998} {1}{0}}
  hosts_require_auth = domain.com
  headers_remove = received
  return_path = ${address:$h_from:}


remote_domain_org_smtp:
  driver = smtp
  message_size_limit = ${if > {$max_received_linelength}{998} {1}{0}}
  hosts_require_auth = domain.org
  return_path = ${address:$h_from:}

where domain.com and domain.org are respectively the domains I want to choose between.

Related Topic