OK, I think I am done messing with this for a while. Here is my recap:
I ended up going with the https://github.com/smartlyway/email-autoconfig-php version. All dynamic, including iPhone config files. Just need to add the PHP code into index.php files so no .htaccess redirection required.
This code only works with top level domains xxx.com, xxx.org etc. You will have to change the regex if your domain has subdomains or otherwise is different. Also this assumes email address emailaccount@domains.com uses mailbox named domain_emailaccount, and if the email address has periods in it like email.account@domain.com that the mailbox name is domain_email_account. If yours are different, you would need to change the regex and logic in the PHP and HTML. There is some formatting included for visual appeal only.
Accessing the iPhone mobileconfig profiles is done by opening this URL in Safari on the iPhone passing in the email address at the end
https://autoconfig.domain.com/email.mobileconfig?emailaccount@domain.com
The iPhone will prompt to install the profile. User has to provide the password.
Previous config-v1.1.xml is not changed from above.
Autodiscover.xml/index.php
<?php
$raw = file_get_contents('php://input');
$matches = array();
preg_match('/<EMailAddress>([^@]+)@([^\.]+).*<\/EMailAddress>/', $raw, $matches);
$accountname = ucwords(str_replace(array(" ",".","-","_")," ",$matches[1]), " ");
$domain = ucfirst(strtolower($matches[2]));
$account = str_replace(".", "_", $matches[1]);
$username = strtolower($matches[2] . "_" . $account);
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
echo "<!-- " . $accountname . " " . $domain . " " . $account . " " . $username . " -->\n";
?>
<Autodiscover xmlns="http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006">
<Response xmlns="http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a">
<User>
<DisplayName><?php echo $domain . " " . $accountname; ?></DisplayName>
</User>
<Account>
<AccountType>email</AccountType>
<Action>settings</Action>
<Protocol>
<Type>IMAP</Type>
<Server>mail.us.opalstack.com</Server>
<Port>993</Port>
<DomainRequired>off</DomainRequired>
<SPA>off</SPA>
<SSL>on</SSL>
<AuthRequired>on</AuthRequired>
<LoginName><?php echo $username; ?></LoginName>
</Protocol>
<Protocol>
<Type>SMTP</Type>
<Server>smtp.us.opalstack.com</Server>
<Port>587</Port>
<DomainRequired>off</DomainRequired>
<SPA>off</SPA>
<Encryption>TLS</Encryption>
<AuthRequired>on</AuthRequired>
<LoginName><?php echo $username; ?></LoginName>
</Protocol>
</Account>
</Response>
</Autodiscover>
email.mobileconfig/index.php
<?php
header('Content-Type: application/xml');
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$email = filter_var($_GET["email"], FILTER_SANITIZE_EMAIL);
$matches = array();
preg_match('/([^@]+)@([^\.]+)\.(.*)/', $email, $matches);
echo "<!-- email = " . $email . ": " . $matches[1] . "-" . $matches[2] . "-" . $matches[3] . " -->\n";
$email = strtolower($email);
$accountname = ucwords(str_replace(array(" ",".","-","_")," ",$matches[1]), " ");
$domain = ucfirst(strtolower($matches[2]));
$account = str_replace(".", "_", $matches[1]);
$username = strtolower($matches[2] . "_" . $account);
$payloadidstr = strtolower($matches[3] . "." . $matches[2] . "." . $matches[1]);
echo "<!-- " . $email . ": " . $accountname . "-" . $domain . " " . $account . " " . $username . " " . $payloadidstr . " -->\n";
?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>PayloadContent</key>
<array>
<dict>
<key>EmailAccountDescription</key>
<string><?php echo $domain . " " . $accountname; ?></string>
<key>EmailAccountName</key>
<string><?php echo $domain . " " . $accountname; ?></string>
<key>EmailAccountType</key>
<string>EmailTypeIMAP</string>
<key>EmailAddress</key>
<string><?php echo $email ?></string>
<key>IncomingMailServerAuthentication</key>
<string>EmailAuthPassword</string>
<key>IncomingMailServerHostName</key>
<string>mail.us.opalstack.com</string>
<key>IncomingMailServerPortNumber</key>
<integer>993</integer>
<key>IncomingMailServerUseSSL</key>
<true/>
<key>IncomingMailServerUsername</key>
<string><?php echo $username; ?></string>
<key>OutgoingMailServerAuthentication</key>
<string>EmailAuthPassword</string>
<key>OutgoingMailServerHostName</key>
<string>smtp.us.opalstack.com</string>
<key>OutgoingMailServerPortNumber</key>
<integer>587</integer>
<key>OutgoingMailServerUseSSL</key>
<true/>
<key>OutgoingMailServerUsername</key>
<string><?php echo $username; ?></string>
<key>OutgoingPasswordSameAsIncomingPassword</key>
<true/>
<key>PayloadDescription</key>
<string><?php echo $domain . " " . $accountname; ?> email autoconfiguration profile</string>
<key>PayloadDisplayName</key>
<string><?php echo $domain . " " . $accountname; ?> Email</string>
<key>PayloadIdentifier</key>
<string><?php echo $payloadidstr; ?>.autoconfig</string>
<key>PayloadType</key>
<string>com.apple.mail.managed</string>
<key>PayloadUUID</key>
<string>BDD6C1C4-B470-49C1-AB6B-72CE1DBF728C</string>
<key>PayloadVersion</key>
<integer>1</integer>
<key>SMIMEEnableEncryptionPerMessageSwitch</key>
<false/>
<key>SMIMEEnablePerMessageSwitch</key>
<false/>
<key>SMIMEEnabled</key>
<false/>
<key>SMIMEEncryptByDefaultUserOverrideable</key>
<false/>
<key>SMIMEEncryptionEnabled</key>
<false/>
<key>SMIMESigningEnabled</key>
<false/>
<key>SMIMESigningUserOverrideable</key>
<false/>
<key>allowMailDrop</key>
<true/>
<key>disableMailRecentsSyncing</key>
<false/>
</dict>
</array>
<key>PayloadDescription</key>
<string><?php echo $domain . " " . $accountname; ?> email autoconfiguration</string>
<key>PayloadDisplayName</key>
<string><?php echo $domain . " " . $accountname; ?> email</string>
<key>PayloadIdentifier</key>
<string><?php echo $payloadidstr; ?>.autoconfig</string>
<key>PayloadOrganization</key>
<string><?php echo $domain; ?></string>
<key>PayloadRemovalDisallowed</key>
<false/>
<key>PayloadType</key>
<string>Configuration</string>
<key>PayloadUUID</key>
<string>8A15C321-3F7D-42A0-8F1D-03C414B79EB8</string>
<key>PayloadVersion</key>
<integer>1</integer>
</dict>
</plist>