I have recently improved this.. "quite significantly" to be less messy.
primarydomain.com <- should be the domain you are primarily using for your mail servers assuming most of you run something like server1.primarydomain.com server2.primarydomain.com and mail.primarydomain.com connects to default roundcube.
Why this is better...
- Less DNS records (Everyone already hates having 5-10 default dns records that enhance forces on us...)
- iOS's garbage MAIL APP (sometimes) works!
- Outlook works perfectly
- Thunderbird works perfectly
New steps:
Create subdomain under your primary enhance domain
autodiscover.primarydomain.com
The directory structure (for some reason) has to be:
autodiscover.primarydomain.com/autodiscover/<files here>
Create a autodiscover.php file:
<?php
// Read the incoming Outlook autodiscover POST payload (XML)
$input = file_get_contents('php://input');
preg_match('/<EMailAddress>(.*?)<\/EMailAddress>/', $input, $matches);
$email = $matches[1] ?? '';
// Extract domain from the email
$domain = '';
if (strpos($email, '@') !== false) {
$domain = substr(strrchr($email, "@"), 1);
}
$mailHost = $domain ? 'mail.' . $domain : '';
header('Content-Type: application/xml; charset=utf-8');
// Output the autodiscover XML
echo '<?xml version="1.0" encoding="utf-8"?>
<Autodiscover xmlns="http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006">
<Response xmlns="http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a">
<Account>
<AccountType>email</AccountType>
<Action>settings</Action>
';
echo '
<Protocol>
<Type>IMAP</Type>
<Server>' . $mailHost . '</Server>
<Port>993</Port>
<SSL>true</SSL>
<LoginName>' . $email . '</LoginName>
<UseSPA>false</UseSPA>
</Protocol>
';
echo '
<Protocol>
<Type>POP3</Type>
<Server>' . $mailHost . '</Server>
<Port>995</Port>
<SSL>true</SSL>
<LoginName>' . $email . '</LoginName>
<UseSPA>false</UseSPA>
</Protocol>
';
echo '
<Protocol>
<Type>SMTP</Type>
<Server>' . $mailHost . '</Server>
<Port>465</Port>
<SSL>true</SSL>
<LoginName>' . $email . '</LoginName>
<AuthRequired>true</AuthRequired>
<UseSPA>false</UseSPA>
</Protocol>
';
echo '
</Account>
</Response>
</Autodiscover>';
Create .htaccess file -
RewriteEngine On
RewriteRule ^autodiscover\.xml$ autodiscover.php [L]
Default DNS Record - SRV RECORD - (do not use $$origin$$ anymore no need)
Hostname:
_autodiscover._tcp
Priority:
0
Weight:
0
Port:
443
Target Hostname:
autodiscover.primarydomain.com
CNAME Records:
If someone can get the CNAME record setup to work, I'd be very happy. I kept running into SSL issues, outlook would knock it back because the SSL cert was of course for the primarydomain and there was a mismatch of some sort. I guess it could work if you had some sort of "higher" level of SSL maybe?