Office365 Exchange with cPanel Exim

benjam1nk

Registered
Aug 27, 2015
3
0
1
Cleveland, Ohio
cPanel Access Level
Root Administrator
I am in the process of revisiting a question I have been attempting to solve for probably way too long; A question with regard to utilizing Office365 Exchange in combination with cPanel Exim to distribute domain emails between the two servers.

The MX records for my domain point to my Office365 Exchange account. At this first stop, I have exchange email boxes, distribution groups, etc, all working as expected. My cPanel server is set up as a smarthost. Any emails which are not slated for an address on the exchange server will then be directed to the smarthost, on which the domain is also correlated with a cPanel account. This also works perfectly.

What does not work perfectly is when I attempt to send an email to the same domain from within the cPanel server, as the address is being treated as a local account, and is not being relayed out to the world to hit the Office365 exchange server.

My stop gap solution to this is as follows, and is placed in the "Routers" section of my Exim configuration.

office365exchangeForward:
driver = manualroute
condition = ${lookup{[email protected]$domain}lsearch{/home/benjam1nk/etc/exim_office365hostedaddresses} {yes}{no}}
transport = remote_smtp
route_list = * mydomain-com.mail.protection.outlook.com


This solution works, but requires manual intervention, which leads me to believe an easier way to accomplish the same thing has to exist. I presently utilize a powershell script to pull all email addresses from Office365, which then updates the file from which Exim checks, and in turn utilizes the above manual route. The problem being this file can become out of sync when addresses are modified on the Exchange Account.

Does anyone have any suggestions of how to simplify this mailflow, and eliminate the manual update of the file holding all remotely hosted email addresses for the domain?

I apologize in advance if this question seems somewhat convoluted.

Thank you.

Edit: I should also mention, this solution should work for email addresses associated with multiple domains and subdomains.
 
Last edited:

cPanelLauren

Product Owner
Staff member
Nov 14, 2017
13,295
1,273
313
Houston
Hello,

I believe the following, while working for this portion is actually the issue:

Any emails which are not slated for an address on the exchange server will then be directed to the smarthost, on which the domain is also correlated with a cPanel account. This also works perfectly.
my assumption is that you have the domain in /etc/localdomains (you've selected local mail exchanger in the dns zone file) - this correlates to the following:


Local Mail Exchanger — The server always accepts mail for this domain. The system will deliver mail to the local mailbox.
Note:
Choose this option if your server uses smart hosts or another gateway service to filter mail.


What this means is that any mail sent from the server for internal mail will be considered a local delivery and will not be delivered to the primary MX record. I believe that if you want to use this configuration and be able to send mail local mail, you'd need to set this as the Backup Mail Exchanger, this does mean though that you would have to ensure the lowest priority MX record is the exchange MX

Backup Mail Exchanger — The server functions as a backup mail exchanger. The system will hold mail for this domain until a lower number mail exchanger becomes available.

Note:
You must configure the primary MX record to point to the appropriate exchanger.
 

benjam1nk

Registered
Aug 27, 2015
3
0
1
Cleveland, Ohio
cPanel Access Level
Root Administrator
Thank you for your response. I attempted this solution, but does not appear to solve my issue.

When a domain is set to use the backup mail exchanger, any emails sent from the outside world to an address hosted locally will end up in a mail loop between office365 exchange smarthost, and the cpanel mail server, eventually timing out and sending a delivery error the the sender.

Hello,

I believe the following, while working for this portion is actually the issue:



my assumption is that you have the domain in /etc/localdomains (you've selected local mail exchanger in the dns zone file) - this correlates to the following:






What this means is that any mail sent from the server for internal mail will be considered a local delivery and will not be delivered to the primary MX record. I believe that if you want to use this configuration and be able to send mail local mail, you'd need to set this as the Backup Mail Exchanger, this does mean though that you would have to ensure the lowest priority MX record is the exchange MX
 

cPanelLauren

Product Owner
Staff member
Nov 14, 2017
13,295
1,273
313
Houston