Exim Smarthost Multiple domains and credentials Mailgun

By working off of the Answer by Caracos(which I tried but couldn't get working) I was able to find this forum post and after modifying the details for Mailgun its working like a charm. I also like this solution because it is scalable to other mail relay services if needed.
https://forums.cpanel.net/threads/how-to-send-email-from-different-domains-using-different-smarthosts.641949/

I'll just outline the settings I used for Mailgun but refer to the link for more detailed explanations, pictures and other examples like Sendgrid, Mailjet and Sendpulse.


  1. In WHM, go to the "Exim Configuration Manager" (Home > Service Configuration > Exim Configuration Manager). After, click in the "Advanced Editor" tab.
    (Probably a good idea to backup settings before making these changes)

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}}}}

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}}}}

Section: TRANSPORTSTART

#Smart Host Sending
sendbysmarthoststransport:
driver = smtp
port = ${extract{port} 
{${lookup{$sender_address_domain}lsearch{/etc/exim_smarthosts}}}}
hosts_require_auth = $host_address
hosts_require_tls = $host_address

Scroll to the bottom of the page and Save the Exim configuration and make sure Exim restarts.


  1. Create the file named "exim_smarthosts" in "/etc" directory with Mailgun or other smarthost parameters.

This is some example data, change the domain, user and pass.

#Sending by Mailgun
domain1.com: domain=domain1.com [email protected] pass=MAILGUN_password smtp=smtp.mailgun.org port=587
domain2.com: domain=domain2.com [email protected] pass=MAILGUN_password smtp=smtp.mailgun.org port=587
domain3.com: domain=domain3.com [email protected] pass=MAILGUN_password smtp=smtp.mailgun.org port=587

The 3rd example uses domain2.com credentials for domain3.com, this will work but the emails will include a via mg.domain2.com


Now email sent out from cPanel created email accounts on their respective domains will be intercepted by exim (allowed to connect to external SMTP servers) and relayed through Mailgun if the credentials in /etc/exim_smarthosts are correct and DNS settings are correct. However the WHM security settings still restrict all cPanel accounts from connecting to any external SMTP service so we need to do one more step to utilize the above setup for app based emailing.

FYI: This is the setting that should be kept on restricting external SMTP connections
WHM Admin >> Tweak Settings >> Mail >> Restrict outgoing SMTP to root, exim, and mailman

To send email from Joomla, Wordpress or other CMS or App follow the next steps


  1. a) Create an email account in cPanel and get the secure login credentials.
    cPanel >> Email Accounts >> (New Email) >> Connect Devices >> Set Up Email Client

    b) Use this Username, Password and Outgoing Server details in your Joomla, WordPress or other CMS/App/Plugin


Now your CMS's, plugins and apps are only connecting to an internal SMTP account but when that account sends out it is relayed through the external SMTP setup in the first steps.

BIG THANKS to DigitalComunic on cPanel forums whose diligent work and troubleshooting has given us a robust workaround that should be useful for years to come!!