cPanel: Setting up a smarthost for specific domains

Have you ever wanted to relay emails destined for only specific recipient domains to a third-party server (smarthost) rather than being delivered directly from your cPanel server?   If the answer is yes, you’ve come to the right place.

These instructions are for a cPanel server (at this time WHM 54) but really would apply in general to any box running Exim.   Of course the WHM-specific fluff would not be applicable in that case and you would have to edit exim.conf manually.

A.  Create a file to hold your static routes

touch /etc/staticroutes

chown root:mail /etc/staticroutes

B1.  Log into WHM 106 or lower and make some changes

Log into WHM and then go to Exim Configuration Manager –> Advanced Editor

Find the empty box right under Section: ROUTERSTART and add the following:

static_route:
driver = manualroute
transport = remote_smtp
route_data = ${lookup{$domain}partial-lsearch{/etc/staticroutes}}

Then click Save down at the bottom and wait for it to save changes and restart Exim.

B2.  Log into WHM 108 or newer and make some changes

Log into WHM and then go to Exim Configuration Manager –> Advanced Editor

Find the empty box right under Section: ROUTERSTART and add the following:

static_route:
driver = manualroute
.ifdef SRSENABLED
# if outbound, and forwarding has been done, use an alternate transport
transport = ${if eq {$local_part@$domain} \
{$original_local_part@$original_domain} \
{remote_smtp} {remote_forwarded_smtp}}
.else
transport = remote_smtp
.endif
route_data = ${lookup{$domain}partial-lsearch{/etc/staticroutes}}

Then click Save down at the bottom and wait for it to save changes and restart Exim.

C.  edit /etc/staticroutes

Add a domain / smarthost mapping

Example:

example.com: 192.168.0.1

This tells exim to send all outbound mail destined for the example.com domain to 192.168.0.1 (the smarthost) for further delivery.

D.  Make sure the smarthost allows relaying

Make sure the smarthost box allows the relaying of mail without authentication from the cPanel box’s main IP address.   (since the smarthost box could be any MTA, you’ll have to figure out how to do this on your own).

If we can assume that the smarthost is also a cPanel box, then you can just add the main IP address of the cPanel box that you want to relay from into /etc/trustedmailhosts on the cPanel box that you want to relay to (smarthost).

5 comments on cPanel: Setting up a smarthost for specific domains

  1. Hi there

    Great article really informative and the kind of configuration we are looking for.

    In the Static Routes file is there a way to include a wildcard entry? For example all domains send out through 192.168.0.1 by default except domain example.com 192.168.0.2 and example.co.uk 192.168.0.3?

    We have a lot of domains and don’t want to have to add new domains as they are created manually but rather they would send out through the default route unless otherwise stated.

    1. Matt,

      I may have tried that before, but if I have I forget the answer.

      I believe you can simply use a * to denote a default action on all domains not explicitly listed. I _believe_ you should be able to do:

      *: 192.168.0.1
      example.com: 192.168.0.2
      example.co.uk: 192.168.0.3

      Be sure you are understanding what I’m writing about. I’m not referring to mail being sent from cPanel using a specific IP 192.168.0.1 bound to the cPanel ethernet interface. I’m specifically talking about cPanel sending out mail to a specific intermediate mailserver (smarthost) that is taking on the role of finally delivery to the recipient domain.

      So the 192.168.0.1 through 192.168.0.3 aren’t IP addresses attached to the cPanel interface, but rather are the IP addresses of servers elsewhere that you want to hand off your mail to so that they can perform the final delivery.

      You may have the correct understanding already, but I just wanted to clarify that for you.

      In my initial example, “example.com” is some domain that my users are sending to (like AOL.COM). For some reason, I might not want my cPanel server to talk to AOL.COM mailservers directly and instead I might want mail destined for AOL.COM to be delivered to 192.168.0.1 (some box I control that will make the direct connection to AOL.COM servers to deliver the mail.

      bob@domainOnMyServer.com sends an email to jim@example.com results in the email from bob@domainOnMyServer.com to jim@example.com taking this path:

      myCpanelServer –> 192.168.0.1 –> MX for example.com.

      One might also do this if for some reason example.com mailservers are not accepting mail sent from your cPanel server because they blacklisted you. You might try to send outbound example.com to an intermediate server that you control, which then makes the connection to example.com mailservers so that they don’t see your cPanel IP address. (granted, we all know this isn’t a good idea because any big named email provider would figure out what you were doing and block the intermediate server IP as well). But this is just an example.

      Mike

  2. To be even more clear (which means I will probably make it even more confusing) …

    *: 192.168.0.1
    example.com: 192.168.0.2
    example.co.uk: 192.168.0.3

    In that configuration any email sent from your cPanel server to an external domain will be delivered to 192.168.0.1 by default — so 192.168.0.1 better be configured to further deliver that mail to the recipient domain’s mailsystem.

    And any email send to example.com will be delivered to 192.168.0.2 for further processing, and example.co.uk to 192.168.0.3 for further processing.

    In the above scenario, your cPanel server would not be making any outbound connections to any external domains’ mailservers. It would be passing all outbound email to an intermediate server, and those intermediate servers must be configured to send on the mail to the destination domain.

    If you want your cPanel server to send all mail destined for external domains directly to the MXs for those external domains, then do not add the wildcard.

    mike

  3. Hi Mike

    Thanks for the reply.
    I believe I had the correct understanding as we have an external smarthost setup at present so all our servers send out through one smarthost.

    However we are wanting to setup a second smarthost and have certain cPanel users (ones that send out alot of mail for example) to send out through the second smarthost. Would the configuration be the same as you described above?

    Thanks

  4. Matt,

    I don’t use a smarthost for all mail, so I can’t give you a definitive answer. The * wildcard entry _should_ be fine, but the only way you will know is to test it yourself.

    Assuming a * wildcard works, my directions should be appropriate. However, your mileage may vary. Sometimes you just have to bite the bullet and test. The worst case scenario is you have mail hosed up for a second until you revert it back. The best case scenario is everything works as planned.

    mike

Leave a Reply to Matt Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.