Outlook Autodiscover Using Apache Virtual Host
Outlook Autodiscover Using Apache Virtual Host
June 28, 2020

Outlook can automatically detect email server configurations while adding email accounts. This tutorial provides the steps to configure the Apache Virtual Host for the Outlook lookup using the autodiscover.example.com subdomain where example.com is your domain.

You may also refer to Outlook Autodiscover Using Nginx Server Block.

Prerequisites

This tutorial assumes that the Apache Web Server, PHP, and Email Server are already installed on the system. You can follow How To Install Apache 2 On Ubuntu 20.04 LTSConfigure Virtual Host On Apache, How To Install Let's Encrypt For Apache On Ubuntu, How To Install PHP 7 On Ubuntu 20.04 LTS, and Install Mail Server On Ubuntu 20.04 LTS Using Postfix, Dovecot, and Roundcube.

It also expects that a CNAME record pointing to your apache server already exists for autodiscover.example.com. An SRV record is also required for _autodiscover._tcp.example.com pointing to autodiscover.example.com.

--------------------------------------------------------------------------------------
Name Type Value TTL
--------------------------------------------------------------------------------------
autodiscover.example.com. CNAME mail.example.com 300
_autodiscover._tcp.example.com. CNAME autodiscover.example.com 300
--------------------------------------------------------------------------------------

Add Virtual Hosts

This section provides the steps to add Virtual Host with basic configuration as shown below.

# Virtual Host - HTTP
sudo nano /etc/apache2/sites-available/autodiscover.example.com.conf

# Content
<VirtualHost *:80>
ServerName autodiscover.example.com
ServerAlias autodiscover.example.com
ServerAdmin admin@example.com

DocumentRoot /var/www/example.com/autodiscover
<Directory /var/www/example.com/autodiscover>
Options -Indexes +FollowSymLinks
DirectoryIndex index.html
AllowOverride All
Require all granted
</Directory>

RewriteEngine on
RewriteCond %{SERVER_NAME} =autodiscover.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

# Save and exit the editor - Press Ctrl + o -> Press Enter -> Ctrl + x

The virtual host for HTTPS will be generated by Let's Encrypt in case you will expand your existing SSL certificate using Let's Encrypt. If you have already added autodiscover subdomain to your SSL certificate, you can also add the virtual host manually as shown below.

# Virtual Host - HTTPS - Example -Let's Encrypt
sudo nano /etc/apache2/sites-available/autodiscover.example.com-le-ssl.conf

# Content
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName autodiscover.example.com
ServerAlias autodiscover.example.com
ServerAdmin admin@example.com

DocumentRoot /var/www/example.com/autodiscover
<Directory /var/www/example.com/autodiscover>
Options -Indexes +FollowSymLinks
DirectoryIndex index.php
AllowOverride All
Require all granted
</Directory>

SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

# Save and exit the editor - Press Ctrl + o -> Press Enter -> Ctrl + x

Add Autodiscover Settings

Now we will create the virtual host directory and add the email server settings for Outlook autodiscover. Create the virtual host directory and configure outlook as shown below.

# Create Directories
sudo mkdir /var/www/example.com/autodiscover
sudo mkdir /var/www/example.com/autodiscover/autodiscover

# Add configuration file
sudo nano /var/www/example.com/autodiscover/autodiscover/autodiscover.php

# Content
<?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.example.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.example.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.example.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>

# Save and exit the editor - Press Ctrl + o -> Press Enter -> Ctrl + x

# Apache permissions
sudo chown -R www-data:www-data /var/www/example.com/autodiscover

You may further update the above script to use SSL for SMTP as shown below.

   <Protocol>
<Type>SMTP</Type>
<Server>mail.example.com</Server>
<Port>465</Port>
<DomainRequired>off</DomainRequired>
<LoginName><?php echo $matches[1]; ?></LoginName>
<SPA>off</SPA>
<Encryption>SSL</Encryption>
<AuthRequired>on</AuthRequired>
<UsePOPAuth>off</UsePOPAuth>
<SMTPLast>off</SMTPLast>
</Protocol>

Also, add the .htaccess file as shown below. This will make sure that all the requests go to the PHP script as shown above.

# Add .htaccess file
sudo nano /var/www/example.com/autodiscover/.htaccess

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

# Save and exit the editor - Press Ctrl + o -> Press Enter -> Ctrl + x

This completes the email server configuration by specifying the protocols supported by the email server. You may add additional protocols supported by your email server.

Enable Virtual Host

After adding the required configurations, enable the virtual host and reload the Apache Web Server as shown below.

# Enable virtual hosts
sudo a2ensite autodiscover.example.com
sudo a2ensite autodiscover.example.com-le-ssl

# Reload Apache
sudo systemctl reload apache2
# OR
sudo service apache2 reload

This enables the virtual hosts to be queried by Microsoft Outlook while adding an email account.

Add Email Account

This section provides the steps to add an email account to Outlook for the first time and subsequent email accounts. I have used Microsoft Outlook 2007 for demonstration purposes. The steps should be similar for other versions of Outlook. Outlook asks for the account details on the first launch as shown in Fig 1.

Outlook Autodiscover - Add Email Account - Startup

Fig 1

Click the Next Button to continue with the setup. It will ask to configure an email account as shown in Fig 2.

Outlook Autodiscover - Add Email Account - Configuration

Fig 2

Choose the option Yes and click the Next Button. It asks for the account details as shown in Fig 3.

Outlook Autodiscover - Add Email Account - Account Details

Fig 3

Now provide your name, email address, and password and click the Next Button. It might also show an SSL certificate warning as shown in Fig 4.

Outlook Autodiscover - Add Email Account - SSL Certificate Warning

Fig 4

Click the Yes Button to accept the SSL certificate. It probes the email server and queries the domain autodiscover.example.com. On finding the server settings and login success, it shows the email server communication details as shown in Fig 5.

Outlook Autodiscover - Add Email Account - Email Probe

Fig 5

It shows the warning due to failure in sending the test email. I have showed the options to change the SMTP details in Fig 9 to resolve this error. Now click the Finish Button to confirm the email account. It will add the email account to the accounts library as shown in Fig 6.

Outlook Autodiscover - Add Email Account - Account Added

Fig 6

Now click Tools -> Account Settings to open the Account Settings Panel as shown in Fig 7. It shows the list of active accounts as shown in Fig 8.

Outlook Autodiscover - Add Email Account - Account Settings

Fig 7

Now keep the account selected and click the option Change. It will show the email account settings as shown in Fig 8.

Outlook Autodiscover - Email Account Settings

Fig 8

Now click the More Settings Button to confirm the email server ports as shown in Fig 9. You might be required to change the SMTP port to 587 and protocol to TLS according to the instructions provided by your email server admin.

Outlook Autodiscover - Email Account Settings

Fig 9

Now click the OK Button to close the Settings wizard. This completes the first email account setup using Microsoft Outlook. Now try to send and receive email using the newly configured account.

Again open the Accounts Settings Panel as shown in Fig 6 and Fig 7 and click the New Option. We can further add the subsequent accounts as shown in Fig 10 to Fig 13.

Outlook Autodiscover - Add Email Account - Email Service

Fig 10

Outlook Autodiscover - Add Email Account - Account Details

Fig 11

Outlook Autodiscover - Add Email Account - Email Probe

Fig 12

Outlook Autodiscover - Add Email Account - Email Added

Fig 13

Similar to the first account, change the SMTP port if required. Outlook will pull the emails from the email server by clicking the Inbox as shown in Fig 14.

Outlook Autodiscover - Email Account - Inbox

Fig 14

This is how we can add email accounts to Microsoft Outlook by probing the email server having the autodiscover.example.com virtual host.

Summary

This tutorial provided the steps to add the virtual host and configure it for Microsoft Outlook to support the autodiscover feature. It also provided the steps to add accounts to Outlook using the autodiscover feature.

Write a Comment

Click on the captcha image to get new code.
Discussion Forum by DISQUS