How can I route a SMTP message by FROM domain (not To, CC, or BCC)

Solution 1:

Assuming your Exchange machine forwards all email to another that runs sendmail, you can decide to route based on sender domain by using the $&f macro in rule set 0.

LOCAL_RULE_0
# first make sure that local mail gets delivered properly
R$-        $@OK
R$* < @ $=w . > $*        $@OK
# now deal with senders
R$*        $: $>canonify $&f $| $1
# stuff [email protected] sends goes through server1.relay
Ruser1 < @ domain1.com. > $* $| $* < @ $* . > $*         $#esmtp $@ [server1.relay] $: $2 < @ $3 . > $4
# stuff [email protected] sends goes through server2.relay
Ruser2 < @ domain2.com. > $* $| $* < @ $* . > $*         $#esmtp $@ [server2.relay] $: $2 < @ $3 . > $4
# stuff users from domain3.com send goes through server3.relay
R$* < @ domain3.com. > $* $| $* < @ $* . > $*        $#esmtp $@ [server3.relay] $: $3 < @ $4 . > $5

The above rule should be added to your /etc/mail/sendmail.mc. Next you should build your sendmail.cf and restart sendmail. Assuming you are on a Debian or Ubuntu system, this is done by issuing sendmailconfig. Keep in mind that in the above example the left and right hand sides of the rules are separated by tabs and not spaces, so you must type and not copy-paste the rule set.

Solution 2:

Well, I'm biased towards (and familiar with) Linux with Postfix for front-line MTA work, so here's a solution with that:

Sender-dependent transport maps

   domain transport:nexthop
          Deliver  mail  for domain through transport to next-hop.