pix
pix
andrew yager.com - andrew's home on the web
 
spacer Home arrow IT Stuff arrow webdev arrow Outlook 2007 Email Autodiscovery with PHP Server

Saturday, 04 September 2010

pix
pix
image bar at top of page
pix
pix
pix
Main Menu
Home
Blog
General News
Christianity Stuff
IT Stuff
Personal Stuff
Links
Contact Me
Search
News Feeds
Twitter Feed

 

 
Outlook 2007 Email Autodiscovery with PHP Server PDF Print E-mail
Written by Andrew Yager   
Saturday, 13 September 2008
I have recently been doing a lot of work with Exchange 2007 and Outlook 2007 clients. They have this wonderful feature called "autodiscovery", which when your exchagne server is correctly configured allows clients to automatically pull all of their settings from the server and just work. Microsoft also introduced autodiscovery for POP3/IMAP and SMTP services. This is great, but when our clients go to set up Outlook it doesn't correctly autodetect the settings required for the mail server. Enter the autodiscover.xml script. We have written a PHP script that dynamically generates the correct autodiscover.xml file based on the clients email address. Read on for more...

We run a qmail/vpopmail hosting environment for email, with a seperate device for outbound email access. We use authentication for all of our services, including SMTP, but as we virtual-host we need the full email address for authentication. By default Outlook only uses the username component (the part before the @) to authenticate, and we needed to change this behaviour.

Microsoft documents the autodiscovery functionality and XML format on their technet website at http://technet.microsoft.com/es-es/library/cc511504.aspx . A bit of close examination allowed us to see that Outlook sends an HTTP POST request that includes the target Email Address. We parse this using a simple Regular Expression, and then write it back into the response.

But how do we actually get Outlook to read this request? Well - it's not that hard. We create a SRV record for each mail domain that points to our secure server (_autodiscover._tcp IN SRV 0 100 443 secure.rwts.com.au.). Outlook then rewrites this to https://secure.rwts.com.au/autodiscover/autodiscover.xml when the user adds the email account. We have enabled mod_rewrite on our server to redirect requests into our autodiscover.php processor.

This is the our autodiscover.php script:


<?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");
?>
<?php 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>POP3</Type>
<Server>mail.rwts.com.au</Server>
<Port>110</Port>
<LoginName><?php echo $matches[1]; ?></LoginName>
<DomainRequired>off</DomainRequired>
<SPA>off</SPA>
<SSL>off</SSL>
<AuthRequired>on</AuthRequired>
<DomainRequired>off</DomainRequired>
</Protocol>
<Protocol>
<Type>IMAP</Type>
<Server>mail.rwts.com.au</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>SMTP</Type>
<Server>mx1.rwts.com.au</Server>
<Port>25</Port>
<DomainRequired>off</DomainRequired>
<LoginName><?php echo $matches[1]; ?></LoginName>
<SPA>off</SPA>
<SSL>off</SSL>
<AuthRequired>on</AuthRequired>
<UsePOPAuth>on</UsePOPAuth>
<SMTPLast>on</SMTPLast>
</Protocol>
</Account>
</Response>
</Autodiscover>

This is our rewrite script (.htaccess):


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

pix
pix

copyright 2006 andrew yager and rwts