Email

Relay Different Domains via Multiple SmartHosts in WHM

If you’re running a shared web-hosting server then you may occasionally have the odd request by a user to relay their mail through their chosen relayer (such as MailGun, Sendgrid, Mailjet etc). This guide will assist you in setting up your WHM to relay different domains via Multiple SmartHosts.

cPanels Blog here explains very well what a SmartHost is. We are going to be following the content of this guide somewhat below with a few extra steps to allow us to specify the domains.

Note: This requires root access to WHM to complete.

Setting up Multiple SmartHosts.

We will assume you’ve already added the domain to your chosen SmartHost(s) control panel, verified the domain and received the appropriate SMTP details to start relaying mail.

In WHM -> Exim Configuration Manager

Login to WHM, go to Exim Configuration Manager, select the Advanced Tab and find the AUTH section similar to the image below.

Copy in the contents of the box below.

#Section: AUTH
#Smart Host Sending
sendbysmarthosts:
driver = plaintext
public_name = LOGIN
hide client_send = : ${extract{user}{${lookup{$sender_address_domain}lsearch{/etc/exim_smarthosts}}}}: ${extract{pass}{${lookup{$sender_address_domain}lsearch{/etc/exim_smarthosts}}}}

Next up we want to add the PREROUTERS section. 

#Section: PREROUTERS
#Smart Host Sending
sendbysmarthostsrouter:
driver = manualroute
domains = ! +local_domains
condition =  "${if eq{${lookup{$sender_address_domain}partial-lsearch{/etc/exim_smarthosts}{$value}}}{}{false}{true}}"
ignore_target_hosts = 0.0.0.0 : 127.0.0.0/8
headers_add = "${perl{mailtrapheaders}}"
transport = sendbysmarthoststransport
route_list = * ${extract{smtp}{${lookup{$sender_address_domain}lsearch{/etc/exim_smarthosts}}}}

And that’s it for the WHM side of things so go ahead and save the configuration.

Multiple SmartHosts – File Modifications in SSH / Terminal

We need to edit a few files so go ahead and open up an SSH session or you can use the Terminal inside WHM.

Let’s start by creating a new file in the etc directory called exim_smarhosts, i.e. /etc/exim_smarthosts 

It should be quite clear but the purpose of this file is to specify the domains and their associated SMTP credentials. In the example below I have demonstrated my own domain.  In this file you only need to add the domains that will be relayed via SmartHost, any domains not specified will continue to be sent out normally through your own server.

# sending by Mailgun
domain1.com: domain=domain1.com user=MAILGUN_smtp_user pass=MAILGUN_api_password smtp=smtp.eu.mailgun.org port=587
benyarwood.co.uk: domain=benyarwood.co.uk [email protected] pass=asupersecretpassword smtp=smtp.eu.mailgun.org port=587

# sending by Mailjet
domain3.com: domain=domain3.com user=MAILJET_smtp_user pass=MAILJET_api_password smtp=in-v3.mailjet.com port=587
domain4.com: domain=domain4.com user=MAILJET_smtp_user pass=MAILJET_api_password smtp=in-v3.mailjet.com port=587

# sending by Sendpulse
domain5.com: domain=domain5.com user=SENDPULSE_smtp_user pass=SENDPULSE_api_password smtp=smtp-pulse.com port=587
domain6.com: domain=domain6.com user=SENDPULSE_smtp_user pass=SENDPULSE_api_password smtp=smtp-pulse.com port=587 

Save and exit.

That’s it! We just need to rebuild the exim configuration to perform a quick syntax check and activate the changes.

/scripts/buildeximconf

At this point I’d recommend heading into cPanel -> Webmail for one of the domains and sending a test email to an outside recipient to check delivery works okay. Also check that you can still send mail from accounts that AREN’T being relayed.

Adapted from a cPanel forum post here

For more WHM related guides please see here

Leave a 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.