> " . ($data == "" ? "" : htmlspecialchars($data)) . "
";
}
fputs($socket, $data);
$answer = fread($socket, 1);
$remains = socket_get_status($socket);
if ($remains-- > 0) {
$answer .= @fread($socket, $remains['unread_bytes']);
}
if ($echo) {
echo "" . htmlspecialchars($answer) . "
";
}
return $answer;
}
function parseResponse($resp) {
$lines = explode("[\r\n]+", $resp);
$code = 0;
$msg = array();
foreach($lines as $line) {
$tokens = preg_split("/[ -]/", $line, 2);
if (!$code) $code = intval($tokens[0]);
array_push($msg, $tokens[1]);
}
$msg = implode(" | ", $msg);
return array($code, $msg, strtolower($msg), trim($resp));
}
function checkFailed($resp) {
return $resp[RS_CODE] > 250;
}
function errorMessage($resp) {
return " ошибка при проверке: " . codeMessage($resp) . "
";
}
function contains($resp, $str) {
return substr_count($resp[RS_MSG_LOWER], strtolower($str)) > 0;
}
function checkEmail($email, $from, $echo) {
// получаем данные об MX-записи домена, указанного в email
$domain = end(explode("@", $email));
$mx = @dns_get_record($domain, DNS_MX);
if (!$mx) {
return ($email . " MX запись не существует
");
}
$mxKey = array_rand($mx);
$mxCount = count($mx);
$mx = $mx[$mxKey]['target'];
if ($echo) {
echo "MX запись домена $domain существует: $mx (" . ($mxKey + 1) . "/$mxCount)";
}
// открываем сокет и создаем поток
try {
$socket = @fsockopen($mx, 25, $errno, $errstr, 10);
if (!$socket) {
//echo "$errstr ($errno)\n";
if ($errno == 10060) {
return $email . " таймаут подключения к MX серверу $mx (" . ($mxKey + 1) . "/$mxCount)
";
} else if (substr_count($errstr, "php_network_getaddresses: getaddrinfo failed") > 0) {
return $email . " полученный MX сервер не существует
";
} else {
return ($email . " " . iconv("Windows-1251", "UTF-8", $errstr) . " ($errno)
");
}
} else {
// 1. отправляем пустую строку, чтобы получить приветствие сервера
$resp = parseResponse(sWrite($socket, "", $echo));
if (checkFailed($resp)) return $email . errorMessage($resp);
// 2. представляемся сами
$fromHost = end(explode("@", $from));
$resp = parseResponse(sWrite($socket, "HELO $fromHost\r\n", $echo));
if (checkFailed($resp)) return $email . errorMessage($resp);
$resp = parseResponse(sWrite($socket, "MAIL FROM: <$from>\r\n", $echo));
if (checkFailed($resp)) return $email . errorMessage($resp);
// 2.запрашиваем разрешение на отправку письма адресату
$response = sWrite($socket, "RCPT TO: <$email>\r\n", $echo);
$resp = parseResponse($response);
// закрываем соединение
sWrite($socket, "QUIT\r\n", $echo);
fclose($socket);
// ниже идет простейшая обработка полученного ответа
//echo "\nCheck report:\n";
$result = "";
$code = $resp[RS_CODE];
if ($code == 550 && contains($resp, "is not allowed to send mail as")) {
$result = "Ваш IP не соответствеут имейлу $from";
} else if ($code == 554 && contains($resp, "Relay access denied")) {
$result = "заблокирован";
} else if ($code == 550 ||
$code == 553 && contains($resp, "no such user") ||
$code == 511 && contains($resp, "no mailbox here by that name")
) {
$result = "не существует";
} else {
if ($resp[RS_CODE] == 250) {
if (contains($resp, "ok") || contains($resp, "accepted")) {
$result = "существует";
} else {
$result = "существует, но он какой-то странный: $response";
}
} else {
$message = $resp[RS_MSG_LOWER];
if (contains($resp, "recipient address rejected:")) {
$msg = trim(end(explode("recipient address rejected:", $message)));
if ($msg == "blocked") {
$result = "заблокирован";
} else if (contains($resp, "greylisted")) {
$result = "заблокирован";
} else if (contains($resp, "Temporary deferral")) {
$result = "временно заблокирован";
} else if ($msg == "access denied") {
$result = "заблокирован или требуется аутентификация";
} else {
$result = "адрес отклонен: '$msg' [$code]";
}
} else {
$result = "ошибка при проверке: " . codeMessage($resp);
}
}
} // rgstr01@rambler.ru не был распознан: 540 5.7.1 : recipient address rejected: Blocked
return "$email $result
";
}
} catch (Exception $e) {
if ($echo) {
echo 'Выброшено исключение: ', $e->getMessage(), "\n";
}
return $email . " ошибка при проверке " . $e->getMessage() . "
";
}
}
?>
"Проверка заблокирована: У Вас отсутствует PTR-запись для IP-адреса"
);
if ($codes[$code]) return $codes[$code];
if (contains($resp, "RBL")) {
return "E-mail с которого осуществляется проверка или Ваш IP находится в черном списке";
}
if (contains($resp, "has reached its limit")) {
return "Достигнут лимит проверок с Вашего IP адреса";
}
if (contains($resp, "Service temporarily unavailable")) {
return "MX сервер временно не доступен или Ваш IP находится в черном списке";
}
if (contains($resp, "Protocol error")) {
return "Протокол MX сервера не поддерживается данным скриптом";
}
if (contains($resp, "We do not relay without RFC2554 authentication")) {
return "Требуется аутентификация SMTP сервера";
}
if (contains($resp, "IP not in whitelist for")) {
return "Ваш IP должен быть в белом списке для проверки этого имейла";
}
if (contains($resp, "please try another server") ||
contains($resp, "I don't like your hostname for IP address")) {
return "Проверка заблокирована: У Вас отсутствует PTR-запись для IP-адреса";
}
if (contains($resp, "blocked - see") ||
contains($resp, "Your access to this mail system has been rejected") ||
contains($resp, "GreyListed") ||
contains($resp, "BARRED") ||
contains($resp, "Greylisting in action") ||
contains($resp, "Client host rejected") ||
contains($resp, "Client host") && contains($resp, "blocked") ||
contains($resp, "host") && contains($resp, "blacklisted") ||
contains($resp, "IP address is black listed")) {
return "Проверка заблокирована: Ваш IP/Hostname находится в черном списке";
}
if (contains($resp, "dynamically-assigned IP ranges")) {
return "Проверка заблокирована: Использование динамических IP адресов запрещено";
}
return $resp[RS_MSG];
}
?>