Skip to content

Instantly share code, notes, and snippets.

@rhurling
Last active April 23, 2018 08:52
Show Gist options
  • Save rhurling/4a863e7d46fde1e826aabd3c3d3d18e6 to your computer and use it in GitHub Desktop.
Save rhurling/4a863e7d46fde1e826aabd3c3d3d18e6 to your computer and use it in GitHub Desktop.
autodiscover.xml generation via php - based on github.com/Radiergummi/php-autodiscover
RewriteEngine On
RewriteRule ^autodiscover.xml$ autodiscover.php [NC,L]
<?
$valid_mx_host = "w0108377.kasserver.com";
//get raw POST data so we can extract the email address
$data = file_get_contents("php://input");
// retrieve email address from client request
preg_match("/\<EMailAddress\>(.*?)\<\/EMailAddress\>/", $data, $email);
$email = $email[1];
// get domain from email address
$domain = substr(strrchr($email, "@"), 1);
if (empty($domain)) {
http_response_code(404);
exit;
}
$mx_hosts = dns_get_record($domain, DNS_MX);
if (empty($mx_hosts) || count($mx_hosts) > 1 || $valid_mx_host !== $mx_hosts[0]["target"]) {
http_response_code(404);
exit;
}
// POP settings
$popServer = $valid_mx_host;
$popPort = '995';
$popSSL = 'on';
// IMAP settings
$imapServer = $valid_mx_host;
$imapPort = '993';
$imapSSL = 'on';
// SMTP settings
$smtpServer = $valid_mx_host;
$smtpPort = '587';
$smtpSSL = 'TLS';
//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" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="$
<Response xmlns="http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a">
<Account>
<AccountType>email</AccountType>
<Action>settings</Action>
<Protocol>
<Type>IMAP</Type>
<Server><?php echo $imapServer ?></Server>
<Port><?php echo $imapPort ?></Port>
<DomainRequired>off</DomainRequired>
<LoginName><?php echo $email ?></LoginName>
<SPA>off</SPA>
<SSL><?php echo $imapSSL ?></SSL>
<AuthRequired>on</AuthRequired>
</Protocol>
<Protocol>
<Type>POP3</Type>
<Server><?php echo $popServer ?></Server>
<Port><?php echo $popPort ?></Port>
<LoginName><?php echo $email ?></LoginName>
<DomainRequired>off</DomainRequired>
<SPA>off</SPA>
<SSL><?php echo $popSSL ?></SSL>
</Protocol>
<Protocol>
<Type>SMTP</Type>
<Server><?php echo $smtpServer ?></Server>
<Port><?php echo $smtpPort ?></Port>
<DomainRequired>off</DomainRequired>
<LoginName><?php echo $email ?></LoginName>
<SPA>off</SPA>
<AuthRequired>on</AuthRequired>
<UsePOPAuth>on</UsePOPAuth>
<?php echo $smtpSSL == 'TLS' ? '<Encryption>TLS</Encryption>' : '<SSL>' . $smtpSSL . '</SSL>' ?>
</Protocol>
</Account>
</Response>
</Autodiscover>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment