1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
function smtp_validate($email)
{
$result = false;
list($name, $domain) = explode('@', $email);
$max_conn_time = 30;
$sock = '';
$port = 25;
$max_read_time = 5;
$users = $name;
$hosts = array();
$mxweights = array();
getmxrr($domain, $hosts, $mxweights);
$mxs = array_combine($hosts, $mxweights);
asort($mxs, SORT_NUMERIC);
$mxs[$domain] = 100;
$timeout = $max_conn_time / count($mxs);
while (list($host) = each($mxs))
{
if ($sock = fsockopen($host, $port, $errno, $errstr, (float)$timeout))
{
stream_set_timeout($sock, $max_read_time);
break;
}
}
if ($sock)
{
$reply = fread($sock, 2082);
preg_match('/^([0-9]{3}) /ims', $reply, $matches);
$code = isset($matches[1]) ? $matches[1] : '';
$msg = "HELO " . $domain;
fwrite($sock, $msg . "\r\n");
$reply = fread($sock, 2082);
$msg = "MAIL FROM: <" . $name . '@' . $domain . ">";
fwrite($sock, $msg . "\r\n");
$reply = fread($sock, 2082);
$msg = "RCPT TO: <" . $name . '@' . $domain . ">";
fwrite($sock, $msg . "\r\n");
$reply = fread($sock, 2082);
preg_match('/^([0-9]{3}) /ims', $reply, $matches);
$code = isset($matches[1]) ? $matches[1] : '';
if ($code == '250')
{
$result = true;
}
elseif ($code == '451' || $code == '452')
{
$result = true;
}
else
{
$result = false;
}
$msg = "quit";
fwrite($sock, $msg . "\r\n");
fclose($sock);
}
return $result;
}
smtp_validate($email); |
Partager