<?php
function check_email($email) {
$err = '';
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
return 'Invalid email format';
}
list($alias, $host) = explode("@", $email);
if (!checkdnsrr($host, "MX")) {
return 'No MX record/invalid domain';
}
getmxrr($host, $mxhosts);
$connected = false;
foreach ($mxhosts as $mxhost) {
$socket = @fsockopen($mxhost, 25, $errno, $errstr, 10);
if ($socket) {
$connected = true;
break;
}
}
if (!$connected) {
return 'Cannot connect to email server';
}
$out = fgets($socket, 1024);
if (!preg_match("/^220/", $out)) {
fclose($socket);
return 'No response from server';
}
fwrite($socket, "HELO $host\r\n");
fgets($socket, 1024);
fwrite($socket, "MAIL FROM: <{$email}>\r\n");
$from = fgets($socket, 1024);
fwrite($socket, "RCPT TO: <{$email}>\r\n");
$to = fgets($socket, 1024);
fwrite($socket, "QUIT\r\n");
fclose($socket);
if (!preg_match("/^250/", $from) || !preg_match("/^250/", $to)) {
return 'Server rejected address';
}
return $err;
}