File: //usr/local/www/mailconfig/index.php
<?php
$email = htmlspecialchars($_GET['email']);
if (!isValidEmailAddr($email)) {
header('HTTP/1.1 400 Bad Request');
$body = "メールアドレスの形式が不明です。";
print mb_convert_encoding($body, 'UTF-8', 'auto');
exit;
}
$initdomain = htmlspecialchars($_GET['initdomain']);
if (!isValidDomain($initdomain)) {
header('HTTP/1.1 400 Bad Request');
$body = "ドメインの形式が不明です。";
print mb_convert_encoding($body, 'UTF-8', 'auto');
exit;
}
if (empty($_GET['type']) || $_GET['type'] === 'imap') {
$type = 'EmailTypeIMAP';
$port = '993';
} else if ($_GET['type'] === 'pop') {
$type = 'EmailTypePOP';
$port = '995';
} else {
header('HTTP/1.1 400 Bad Request');
$body = "プロトコルの形式が不明です。";
print mb_convert_encoding($body, 'UTF-8', 'auto');
exit;
}
$uuid1 = getuuid();
$uuid2 = getuuid();
$body = <<<BODY
<?xml version="1.0" encoding="UTF-8"?>
<!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>$email</string>
<key>EmailAccountName</key>
<string>$email</string>
<key>EmailAccountType</key>
<string>$type</string>
<key>EmailAddress</key>
<string>$email</string>
<key>IncomingMailServerAuthentication</key>
<string>EmailAuthPassword</string>
<key>IncomingMailServerHostName</key>
<string>$initdomain</string>
<key>IncomingMailServerPortNumber</key>
<integer>$port</integer>
<key>IncomingMailServerUseSSL</key>
<true/>
<key>IncomingMailServerUsername</key>
<string>$email</string>
<key>OutgoingMailServerAuthentication</key>
<string>EmailAuthPassword</string>
<key>OutgoingMailServerHostName</key>
<string>$initdomain</string>
<key>OutgoingMailServerPortNumber</key>
<integer>587</integer>
<key>OutgoingMailServerUseSSL</key>
<true/>
<key>OutgoingMailServerUsername</key>
<string>$email</string>
<key>OutgoingPasswordSameAsIncomingPassword</key>
<true/>
<key>PayloadDescription</key>
<string>メール設定を構成します</string>
<key>PayloadDisplayName</key>
<string>$email</string>
<key>PayloadIdentifier</key>
<string>com.apple.mail.managed.$uuid1</string>
<key>PayloadType</key>
<string>com.apple.mail.managed</string>
<key>PayloadUUID</key>
<string>$uuid1</string>
<key>PayloadVersion</key>
<integer>1</integer>
<key>PreventAppSheet</key>
<true/>
<key>SMIMEEnablePerMessageSwitch</key>
<false/>
<key>SMIMEEnabled</key>
<false/>
<key>SMIMEEncryptionEnabled</key>
<false/>
<key>SMIMESigningEnabled</key>
<false/>
<key>allowMailDrop</key>
<false/>
<key>disableMailRecentsSyncing</key>
<false/>
</dict>
</array>
<key>PayloadDisplayName</key>
<string>さくらインターネット メール構成プロファイル</string>
<key>PayloadIdentifier</key>
<string>$email</string>
<key>PayloadRemovalDisallowed</key>
<false/>
<key>PayloadType</key>
<string>Configuration</string>
<key>PayloadUUID</key>
<string>$uuid2</string>
<key>PayloadVersion</key>
<integer>1</integer>
</dict>
</plist>
BODY;
header('Content-Type: application/x-apple-aspen-config');
header('Content-Disposition: attachment; filename="mail.mobileconfig"');
print mb_convert_encoding($body, 'UTF-8', 'auto');
function isValidEmailAddr($addr) {
$parts = explode('@', $addr, 2);
return isset($parts[1])
&& filter_var('a@' . $parts[1], FILTER_VALIDATE_EMAIL) !== false
&& preg_match('/\A[\w+\-=_\.]+\z/', $parts[0]);
}
function isValidDomain($initdomain) {
return isset($initdomain)
&& filter_var('a@' . $initdomain, FILTER_VALIDATE_EMAIL) !== false;
}
function getuuid() {
$PATTERN = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
$chars = str_split($PATTERN);
foreach ($chars as $i => $char) {
if ($char === 'x') {
$chars[$i] = dechex(mt_rand(0, 15));
} elseif ($char === 'y') {
$chars[$i] = dechex(mt_rand(8, 11));
}
}
return strtoupper(implode('', $chars));
}