How to send email from different domains using different smarthosts

Jan 17, 2009
Hi all.

This is related to this topic and anothers old topics: Two different smarthosts for two different domains in exim

This is an old need for me.

I did sometime ago, (with a lot of trial and error, of course). I got my cpanel server working with different domains sending by different smarthosts smtp services. Only configuring in WHM exim configuration manager, this way, changes will not be overwrited at cpanel updates.

I'm using for a long time with success, and this is how I did.

1 - Exim advanced editor configuration

In WHM, go to the "Exim Configuration Manager" (Home > Service Configuration > Exim Configuration Manager). After, click in the "Advanced Editor" tab.


Next, scroll to the "Section: AUTH" editable field. To locate the field, you can use the browser search with the term "Section: AUTH" (without quotes off course).


Copy the code below into the "Section: AUTH" field. Notice that lines that start with # are comments only. You can change the comments, but the rest of the code should not be changed.

#Section: AUTH
#Smart Host Sending
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}}}}

The field filled will be like this, notice the green verified sign to the right confirming the code is valid for Exim.


Now, scroll to the "Section: PREROUTERS" editable field...

...and paste the following code:

#Smart Host Sending
driver = manualroute
domains = ! +local_domains
condition =  "${if eq{${lookup{$sender_address_domain}partial-lsearch{/etc/exim_smarthosts}{$value}}}{}{false}{true}}"
ignore_target_hosts = :
headers_add = "${perl{mailtrapheaders}}"
transport = sendbysmarthoststransport
route_list = * ${extract{smtp}{${lookup{$sender_address_domain}lsearch{/etc/exim_smarthosts}}}}
Note the line "ignore_target_hosts", you should check if your server needs some specific IP to be ignored there. On my server it was not necessary. Also note that in the line "condition = " and line "headers_add = ", the double quotes are required. And also in line "condition = ", there we are using "partial-lsearch" instead of "lsearch" only.

The last Exim configuration will be in the "Section: TRANSPORTSTART" editable field. Paste the following code in this field:

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

Now, scroll to the bottom of the page and Save the Exim configuration.


After saving, make sure Exim has restarted correctly, like bellow.


Be careful if in any editable field that I mentioned has any code there. In this case, before you add or replace the codes, you'll need to analyze whether the codes will conflict or not. In my case, there was no code in any of the 3 editable fields.

Step 2: Creating a file with smarthosts parameters.

To complete all, you should create a file in the "/etc" directory of your server, called "exim_smarthosts". Within this file will be the settings of the Smarthosts and the domains that will use them. The file must be created by the root user and the permissions must be 0644.

Within this file, you must save the login, smtp, and port parameters of smarthosts, so that they are used at the time of sending the email. Each line starts with an index, which is the name of the domain followed by a colon. After the colon, each parameter has an own indicator that follows this model:

"domain", is the same domain name used in index of the line.
"user", is the user (or ID) used in smarthost's SMTP.
"pass", is the password (or API secret, or API key, etc.) used for the SMTP user in smarthost.
"smtp", the SMTP server address of smarthost.
"port", the port used in Smarthost (586,25 or another).

Here's an example of how the "/etc/exim_smarthosts" file will look using 3 different smarthosts for 6 different domains. The data is obviously fictitious, you should adapt the parameters according to your smarthosts settings that you will use. In this scenario, if we have and in the server, these two domains will use the server SMTP service, because both are out of "/etc/exim_smarthosts" file.

# sending by Sendgrid user=SENDGRID_smtp_user pass=SENDGRID_api_password port=587 user=SENDGRID_smtp_user pass=SENDGRID_api_password port=587

# sending by Mailjet user=MAILJET_smtp_user pass=MAILJET_api_password port=587 user=MAILJET_smtp_user pass=MAILJET_api_password port=587

# sending by Sendpulse user=SENDPULSE_smtp_user pass=SENDPULSE_api_password port=587 user=SENDPULSE_smtp_user pass=SENDPULSE_api_password port=587
Remember, each change in "exim_smarthosts" file requires a Exim restart, do it before your tests.

Well, is this. I'll be satisfied and happy if this configuration helps some hosting admins. There are other possible syntaxes for all the exim settings I mentioned, which may also work. The ones I put in this article are the ones I'm currently using with better performance and reliability in my Cpanel hosting server.

This article is at my linkedin too.
If you note anything wrong or incorrect with this article, please warn me and I will have a pleasure to correct.

Best regards.
Last edited: