How to send email from different domains using different smarthosts

Jan 17, 2009
20
9
53
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.

celsopecuraro_exim_smarthosts1.jpg


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).

celsopecuraro_exim_smarthosts2.jpg


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.

Code:
#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}}}}

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

celsopecuraro_exim_smarthosts2b.jpg



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

...and paste the following code:

Code:
#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}}}}
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:

Code:
#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

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

celsopecuraro_exim_smarthosts4.jpg


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

celsopecuraro_exim_smarthosts5.jpg


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:

Code:
"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 domain7.com and domain8.com in the server, these two domains will use the server SMTP service, because both are out of "/etc/exim_smarthosts" file.


Code:
# sending by Sendgrid
domain1.com: domain=domain1.com user=SENDGRID_smtp_user pass=SENDGRID_api_password smtp=smtp.sendgrid.net port=587
domain2.com: domain=domain2.com user=SENDGRID_smtp_user pass=SENDGRID_api_password smtp=smtp.sendgrid.net 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
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:

allanalmeida

Registered
Jan 25, 2020
1
0
1
Brasil
cPanel Access Level
Website Owner
É possível usar várias contas SendGrid?

A cota de e-mail acabou no meu servidor e eu pensei em usar várias contas diferentes do SendGrid. Alguém já testou?
 
Jan 17, 2009
20
9
53
É possível usar várias contas SendGrid?

A cota de e-mail acabou no meu servidor e eu pensei em usar várias contas diferentes do SendGrid. Alguém já testou?
É possível usar várias contas do sendgrid. Basta configurar o arquivo exim_smarthosts para que determinado usuário use determinada conta do sendgrid.
 

AlbertoFX

Member
Feb 15, 2016
5
0
51
Mexico
cPanel Access Level
Root Administrator
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.

View attachment 55573


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).

View attachment 55577


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.

Code:
#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}}}}

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

View attachment 55581



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

...and paste the following code:

Code:
#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}}}}
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:

Code:
#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

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

View attachment 55589


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

View attachment 55593


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:

Code:
"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 domain7.com and domain8.com in the server, these two domains will use the server SMTP service, because both are out of "/etc/exim_smarthosts" file.


Code:
# sending by Sendgrid
domain1.com: domain=domain1.com user=SENDGRID_smtp_user pass=SENDGRID_api_password smtp=smtp.sendgrid.net port=587
domain2.com: domain=domain2.com user=SENDGRID_smtp_user pass=SENDGRID_api_password smtp=smtp.sendgrid.net 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
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.

Thanks so much for your information.
Just a quick question, if I want left out some domains *without* smarthost, how can I configure that? or just simply do not type it on "/etc/exim_smarthosts" file ?
Thanks in advance!
 

AlbertoFX

Member
Feb 15, 2016
5
0
51
Mexico
cPanel Access Level
Root Administrator
Hi, I respond myself. If you don't put domain45.com ( by example ) in /etc/smarthost then the email try be send by normal/default system. This mean the server try to send the email without smarthost wrote en /etc/smarthost.
btw, I tested with sendgrid.
Thanks!
 

zthosting

Registered
Mar 31, 2020
1
0
1
Pakistan
cPanel Access Level
DataCenter Provider
Hi dear DigitalComunic,
Is it possible I can set smart hosts for some domains but remaining all domains use Exim default settings? Mean in My case I am using Paid Mailchannel on one server but I want to use MailChannels for only 1 or 2 domains and remaining all domains send email through server default settings mean exim.

Waiting for your kind response.
 
Jan 20, 2019
11
2
3
Costa Rica
cPanel Access Level
Root Administrator
Hi DigitalComunic,

I have a Cpanel server with all the accounts that send mail by smarthost to another cpanel server that sends it to the internet (server2).

On the server2, the one that sends mail to the internet, I have several IPs.

I want this server (server2) to receive mail from server 1 to send emails from some domains by one IP and from other domains by another IP.

Is this possible to do?
remember that this server only sends email is SMARHOST does not have accounts.

Try to modify (in server2) /etc/mailips as follows:

*: 19x.xxx.xxx.xxx
exmple.com:18x.xxx.xxx.xxx

But it didn't work, all emails kept coming out on the IP 19x.xxx.xxx.xxx

Any ideas....
 

Lyttek

Well-Known Member
Jan 2, 2004
775
5
168
This worked for me!

But...

I had to tweak something and I'm not sure how to get around it if I need to.

The smart host I'm sending to doesn't use password authentication, but instead looks at the IP of the originating server and the domain to see if it's authorized. So I had to comment out the Transport line:

hosts_require_auth = $host_address

With that line enabled, the emails sit in the queue with an error of 'requires authentication but server did not advertise AUTH...'

However, if at some point I need two different smart hosts, one of which requires authentication, how would I enable AUTH for one but not the other?
 

idevelop.pro

Member
Jul 7, 2022
5
1
3
India
cPanel Access Level
Website Owner
it works, good job!
I have more improvement for this

1. what if I want to write down the same SMTP details for multiple domains
currently have to write each domain in a new line with duplicate SMTP details
it's easy for some numbers but what if there are 100,1000,2000 or more domains that use the same SMTP

2. currently the domain not listed use server SMTP right?
what if I want the not listed domains in exim_smarthosts should use external SMTP that I will give SMTP details?

I hope it makes sense
 
Last edited:

mynetfocus

Registered
Mar 29, 2015
1
0
51
Malaysia
cPanel Access Level
Root Administrator
How to modify exim advanced editor to provide separate smarthosts for routing emails to external smtp hosts to send emails to specific domains i.e. gmail.com, hotmail.com, *abc.com, etc?

if a file /etc/staticroutes containing:

gmail.com: user=SENDGRID_smtp_user pass=SENDGRID_api_password smtp=smtp.sendgrid.net port=587
hotmail.com: user=MAILJET_smtp_user pass=MAILJET_api_password smtp=in-v3.mailjet.com port=587
*abc.com: user=SENDPULSE_smtp_user pass=SENDPULSE_api_password smtp=smtp-pulse.com port=587

anyone with expertise to provide such solution which will work together with exim_smarthosts solution?
 

brt

Well-Known Member
Jul 9, 2015
105
10
68
US
cPanel Access Level
Root Administrator
it works, good job!
I have more improvement for this

1. what if I want to write down the same SMTP details for multiple domains
currently have to write each domain in a new line with duplicate SMTP details
it's easy for some numbers but what if there are 100,1000,2000 or more domains that use the same SMTP

2. currently the domain not listed use server SMTP right?
what if I want the not listed domains in exim_smarthosts should use external SMTP that I will give SMTP details?

I hope it makes sense
@idevelop.pro Were you able to figure either of these out? If so what did you do? I have a feeling some sort of matching could be done for domain. I essentially need to use a couple specific smart hosts for a couple domains/accounts and the rest use my regular smarthost.