Howto: redirect a copy of your emails to another server while saving it locally

consultorpc

Well-Known Member
PartnerNOC
Jun 18, 2003
51
0
156
Hello,

We are posting this here in case it helps someone. With a few config changes on exim you can redirect your messages to an external server while keeping a copy locally.

First thing is to add a route for it.

Go to WHM -> Exim Configuration Editor -> Advanced Editor. Search for "ROUTERS CONFIGURATION" and add the following text on the textarea below it:

Code:
smarthost:
	driver = manualroute
	domains = yourdomain.com
	unseen
	transport = remote_smtp
	headers_add = X-Custom-Forwarded: true
        condition = ${if match {$h_X-Custom-Forwarded:}{true}{false}{true}}
	route_data = "aspmx.l.google.com"
In this case, we are redirecting all message sent to yourdomain.com to google's server. Change it according to your needs.

After that, you have to create a system filter. Create file /etc/custom_cpanel_exim_system_filter ( by default Cpanel use /etc/cpanel_exim_system_filter but upcp usually overwrites it when a new version is out ) and add this:

Code:
if first_delivery
	and $h_X-Custom-Forwarded contains "true" 
then
   seen
   finish
endif
This filter is necessary to avoid infinite loops in case the other email server is also redirecting back to you.

Now, go to WHM -> Exim Configuration Editor -> search for filter add edit System Filter File to point to /etc/custom_cpanel_exim_system_filter.

Save the new configuration and you are done.

Note: watch exim log to make sure everything is working fine.

In case something went wrong, you can always revert things by removing the changes on /etc/exim.conf.local and running /scripts/buildeximconf to build a new configuration. Don't forget to restart exim.

Hope it helps someone.
 
Last edited:

cPanelTristan

Quality Assurance Analyst
Staff member
Oct 2, 2010
7,607
43
348
somewhere over the rainbow
cPanel Access Level
Root Administrator
Is there a reason that the advice is being provided to manually edit /etc/exim.conf.local rather than putting this into WHM > Exim Configuration > Advanced Editor section?
 

consultorpc

Well-Known Member
PartnerNOC
Jun 18, 2003
51
0
156
No, just thought it was easier to explain this way rather than saying to edit 5th box on advanced editor, which could make someone insert it at the wrong place.

After all the result is the same, no?
 

cPanelTristan

Quality Assurance Analyst
Staff member
Oct 2, 2010
7,607
43
348
somewhere over the rainbow
cPanel Access Level
Root Administrator
The file /etc/exim.conf.local doesn't exist on my newly formatted machine. It only exists if you've made changes in WHM > Exim Configuration Editor > Advanced Editor in the past, so it isn't the same result for many people as that file will not exist.

To show the results, here is my machine before I made changes in Advanced Editor:

Code:
[email protected] [/etc]# ls -lah /etc/exim.conf*     
-rw-r--r-- 1 root root 47K Aug  8 17:10 /etc/exim.conf
-rw-r--r-- 1 root root 26K Feb 18 09:11 /etc/exim.conf.dist
-rw-r--r-- 1 root root  95 Aug  3 17:03 /etc/exim.conf.localopts
-rw-r--r-- 1 root root 30K Feb 18 09:11 /etc/exim.conf.mailman2.dist
-rw-r--r-- 1 root root 30K Feb 18 09:11 /etc/exim.conf.mailman2.exiscan.dist
Here is my machine after I added something in Advanced Editor:

Code:
[email protected] [/etc]# ls -lah /etc/exim.conf*
-rw-r--r-- 1 root root 47K Aug 10 11:22 /etc/exim.conf
-rw-r--r-- 1 root root 26K Feb 18 09:11 /etc/exim.conf.dist
-rw-r--r-- 1 root root 227 Aug 10 11:22 /etc/exim.conf.local
-rw-r--r-- 1 root root  95 Aug  3 17:03 /etc/exim.conf.localopts
-rw-r--r-- 1 root root 30K Feb 18 09:11 /etc/exim.conf.mailman2.dist
-rw-r--r-- 1 root root 30K Feb 18 09:11 /etc/exim.conf.mailman2.exiscan.dist
I would highly suggest advising to use WHM > Exim Configuration Editor > Advanced Editor in your guide, since otherwise you will have most people who do not have that file exist. Feel free to test this on a new machine to see the results similar to mine above.
 

rclemings

Well-Known Member
Nov 5, 2007
52
5
58
I used this code (#1) to have server1 send all of its mail through server2. However, I quickly found that every message was being sent twice, once by server1 and again by server2. Removing "unseen" from the smarthost block fixed that. Just in case someone else runs into the same issue.
 
  • Like
Reactions: cPanelMichael