Skip to content

Instantly share code, notes, and snippets.

@Nilpo
Last active January 8, 2024 06:16
Show Gist options
  • Save Nilpo/50511b736dc88dd9d915e14bbd3447bd to your computer and use it in GitHub Desktop.
Save Nilpo/50511b736dc88dd9d915e14bbd3447bd to your computer and use it in GitHub Desktop.
Autodiscover.xml generator in PHP for Outlook (and other) mail client configuration.
<?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>example.com</Server>
<Port>143</Port>
<DomainRequired>off</DomainRequired>
<LoginName><?php echo $matches[1]; ?></LoginName>
<SPA>off</SPA>
<SSL>auto</SSL>
<AuthRequired>on</AuthRequired>
</Protocol>
<Protocol>
<Type>POP3</Type>
<Server>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>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>
RewriteEngine On
# Blocks all autodiscover requests to prevent 404 errors in the log
#RewriteCond %{REQUEST_URI} ^/autodiscover/autodiscover.xml [NC]
#RewriteRule .* - [F,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ autodiscover.php [NC,L]
_autodiscover._tcp.example.com. SRV 10 10 443 autodiscover.example.com.
<?xml version="1.0" encoding="utf-8"?>
<Autodiscover xmlns="https://schemas.microsoft.com/exchange/autodiscover/outlook/requestschema/2006">
<Request>
<EMailAddress>[email protected]</EMailAddress>
<AcceptableResponseSchema>https://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a</AcceptableResponseSchema>
</Request>
</Autodiscover>
<?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>example.com</Server>
<Port>143</Port>
<DomainRequired>off</DomainRequired>
<LoginName>[email protected]</LoginName>
<SPA>off</SPA>
<SSL>auto</SSL>
<AuthRequired>on</AuthRequired>
</Protocol>
<Protocol>
<Type>POP3</Type>
<Server>example.com</Server>
<Port>995</Port>
<DomainRequired>off</DomainRequired>
<LoginName>[email protected]</LoginName>
<SPA>off</SPA>
<SSL>on</SSL>
<AuthRequired>on</AuthRequired>
</Protocol>
<Protocol>
<Type>SMTP</Type>
<Server>example.com</Server>
<Port>587</Port>
<DomainRequired>off</DomainRequired>
<LoginName>[email protected]</LoginName>
<SPA>off</SPA>
<Encryption>TLS</Encryption>
<AuthRequired>on</AuthRequired>
<UsePOPAuth>off</UsePOPAuth>
<SMTPLast>off</SMTPLast>
</Protocol>
</Account>
</Response>
</Autodiscover>
@Nilpo
Copy link
Author

Nilpo commented Jan 8, 2024

nslookup -q=srv _autodiscover._tcp.example.com

@Nilpo
Copy link
Author

Nilpo commented Jan 8, 2024

nslookup -q=CNAME autodiscover.domain.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment