Autodiscover mail setting for automatically Outlook setup

Some mail clients will use a system called “Autodiscover” to figure out which settings to use for the pop/imap/smtp settings.

You can set this up if you’d like, it basically requires a subdomain and a SRV record.

You can add the SRV record into any domain you want to use these settings for.

If you’ve got a global SSL certificate in exim/dovecot for your hostname, this would be a good way to ensure clients use the correct value, so they don’t get SSL certificate errors.

https://help.directadmin.com/item.php?id=661

Let’s assume you’re going to have your clients with infotechviet.com connect to mail.infotechviet.com  for both IMAP and SMTP.
We’ll create a subdomain called autodiscover.infotechviet.com to store the XML.

Setup a SRV record in the infotechviet.com DNS zone:

_autodiscover._tcp.infotechviet.com. 3600 IN SRV 10 10 443 autodiscover.infotechviet.com.

Next, create the subdomain autodiscover.infotechviet.com in DA, and add the following code into a file called autodiscover.php:

<?php
//get raw POST data so we can extract the email address
$data = file_get_contents("php://input");
preg_match("/\<EMailAddress\>(.*?)\<\/EMailAddress\>/", $data, $matches);

//set Content-Type
header("Content-Type: application/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>
           <Protocol>
               <Type>IMAP</Type>
               <Server>mail.infotechviet.com</Server>
               <Port>993</Port>
               <DomainRequired>off</DomainRequired>
               <LoginName><?php echo $matches[1]; ?></LoginName>
               <SPA>off</SPA>
               <SSL>on</SSL>
               <AuthRequired>on</AuthRequired>
           </Protocol>
           <Protocol>
               <Type>POP3</Type>
               <Server>mail.infotechviet.com</Server>
               <Port>995</Port>
               <DomainRequired>off</DomainRequired>
               <LoginName><?php echo $matches[1]; ?></LoginName>
               <SPA>off</SPA>
               <SSL>on</SSL>
               <AuthRequired>on</AuthRequired>
           </Protocol>
           <Protocol>
               <Type>SMTP</Type>
               <Server>mail.infotechviet.com</Server>
               <Port>587</Port>
               <DomainRequired>off</DomainRequired>
               <LoginName><?php echo $matches[1]; ?></LoginName>
               <SPA>off</SPA>
               <Encryption>TLS</Encryption>
               <AuthRequired>on</AuthRequired>
               <UsePOPAuth>off</UsePOPAuth>
               <SMTPLast>off</SMTPLast>
           </Protocol>
       </Account>
   </Response>
</Autodiscover>

Note that the SRV record is using port 443 for autodiscover.infotechviet.com, so make sure you’ve got a valid certificate setup for this subdomain. You can test by going to a https://autodiscover.infotechviet.com to ensure you get a green lock.  The script is looking for XML input in the <EMailAddress> tag to insert into the <LoginName> result field.

If needed, you can set SMTP to use port 465, but you’d have to change the <Encryption> from TLS to SSL, as the protocol is different on 465.
Port 587 requires smtp-auth but skips some spam checks and uses STARTTLS to enable SSL.  Port 465 is full SSL but clients might have issues sending if their IP/range is in an RBL.

Lastly, we’ll need to setup an .htaccess file so that any request to the autodiscover.infotechviet.com subdomain results in the autodiscover.php being called.   In the subdomain’s DocumentRoot, add this code:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ – [NC,L] RewriteRule ^.*$ autodiscover.php [NC,L]

RELATED POSTS
Share the Post: