Implementing Autodiscover.xml for Non-Exchange Email Servers: A Linux Configuration Guide


12 views

Many email clients (Outlook, Thunderbird, mobile clients) use the Autodiscover protocol to automatically configure mail server settings. While Microsoft Exchange popularized this feature, the underlying XML-based mechanism can be replicated for non-Exchange setups.

To implement this on Linux:

  • Web server (Apache/Nginx) with SSL
  • Valid SSL certificate (Let's Encrypt recommended)
  • Access to DNS records for your domain
  • Basic understanding of XML structure

First, set up these DNS records:

autodiscover.example.com.  IN  A      192.0.2.1
_autodiscover._tcp.example.com. IN SRV 0 0 443 autodiscover.example.com.

Here's a basic template for IMAP/POP3 servers:

<?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.example.com</Server>
        <Port>993</Port>
        <SSL>on</SSL>
        <AuthRequired>on</AuthRequired>
      </Protocol>
      <Protocol>
        <Type>SMTP</Type>
        <Server>mail.example.com</Server>
        <Port>587</Port>
        <SSL>on</SSL>
        <AuthRequired>on</AuthRequired>
        <UsePOPAuth>on</UsePOPAuth>
        <SPA>off</SPA>
      </Protocol>
    </Account>
  </Response>
</Autodiscover>

For Apache:

<VirtualHost *:443>
    ServerName autodiscover.example.com
    DocumentRoot /var/www/autodiscover
    SSLEngine on
    SSLCertificateFile /path/to/cert.pem
    SSLCertificateKeyFile /path/to/privkey.pem
    SSLCertificateChainFile /path/to/chain.pem
    
    <Location /autodiscover/autodiscover.xml>
        Header set Content-Type "application/xml"
    </Location>
</VirtualHost>

Use these tools to verify:

  1. Test URL: https://autodiscover.example.com/autodiscover/autodiscover.xml
  2. Microsoft Remote Connectivity Analyzer
  3. Outlook test account creation

For dynamic responses, create a PHP script:

<?php
header('Content-Type: application/xml');
$email = $_GET['email'] ?? '';
?>
<?xml version="1.0" encoding="utf-8"?>
<Autodiscover xmlns="http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006">
  <Response>
    <!-- Dynamic content based on $email -->
  </Response>
</Autodiscover>

While Microsoft Exchange popularized the Autodiscover protocol, the underlying XML-based service discovery mechanism can be implemented independently for any email system. The protocol follows a simple principle: clients request autodiscover.xml from predetermined URLs to automatically configure email accounts.

Here's a minimal working example for an IMAP server:


<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.yourdomain.com</Server>
        <Port>993</Port>
        <LoginName>%EMAILADDRESS%</LoginName>
        <DomainRequired>off</DomainRequired>
        <SPA>off</SPA>
        <SSL>on</SSL>
        <AuthRequired>on</AuthRequired>
      </Protocol>
      <Protocol>
        <Type>SMTP</Type>
        <Server>mail.yourdomain.com</Server>
        <Port>587</Port>
        <LoginName>%EMAILADDRESS%</LoginName>
        <DomainRequired>off</DomainRequired>
        <SPA>off</SPA>
        <SSL>on</SSL>
        <AuthRequired>on</AuthRequired>
        <UsePOPAuth>off</UsePOPAuth>
        <SMTPLast>off</SMTPLast>
      </Protocol>
    </Account>
  </Response>
</Autodiscover>

For Apache, create a .htaccess file with:


<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{REQUEST_URI} ^/autodiscover/autodiscover\.xml$ [NC]
  RewriteRule ^(.*)$ /path/to/autodiscover.xml [L]
</IfModule>

For Nginx, add to your server configuration:


location = /autodiscover/autodiscover.xml {
    alias /path/to/autodiscover.xml;
    default_type application/xml;
}

Add these DNS records to improve client discovery:


autodiscover.yourdomain.com.  IN  CNAME  yourdomain.com.
_autodiscover._tcp.yourdomain.com.  IN  SRV  0 0 443 yourdomain.com.

Use curl to verify your setup:


curl -v https://yourdomain.com/autodiscover/autodiscover.xml

Or test with an actual email client using these common endpoints:

  1. https://yourdomain.com/autodiscover/autodiscover.xml
  2. https://autodiscover.yourdomain.com/autodiscover/autodiscover.xml
  3. http://autodiscover.yourdomain.com/autodiscover/autodiscover.xml

For a multi-tenant setup, create a dynamic PHP script:


<?php
header('Content-Type: application/xml');
$domain = $_SERVER['HTTP_HOST'];
?>
<?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.<?php echo $domain; ?></Server>
        <Port>993</Port>
        <LoginName>%EMAILADDRESS%</LoginName>
        <DomainRequired>off</DomainRequired>
        <SPA>off</SPA>
        <SSL>on</SSL>
        <AuthRequired>on</AuthRequired>
      </Protocol>
    </Account>
  </Response>
</Autodiscover>