<?php
function isEmail($email$check_mx true) {
    
// all characters except @ and whitespace
    
$name '[^@\s]+';

    
// letters, numbers, hyphens separated by a period
    
$sub_domain '[-a-z0-9]+\.';

    
// country codes
    
$cc '[a-z]{2}';

    
// top level domains
    
$tlds "$cc|com|net|edu|org|gov|mil|int|biz|pro|info|arpa|aero|coop|name|museum";

    
$email_pattern "/^$name@($sub_domain)+($tlds)$/ix";

    if (
preg_match($email_pattern$email$check_pieces)) {
        
// check mail exchange or DNS
        
if ($check_mx) {
            
$host substr(strstr($check_pieces[0], '@'), 1);
            if (
in_array($host$skip_check)) {
                return 
true;
            }

            
//Check DNS records
            
if(checkdnsrr($host"MX")) {
                if(!
getmxrr($host$mxhost$mxweight)) {
                    return 
false;
                }
            } else {
                
$mxhost[] = $host;
                
$mxweight[] = 1;
            }

            
$weighted_host = array();
            for(
$i 0$i count($mxhost); $i ++) {
                
$weighted_host[($mxweight[$i])] = $mxhost[$i];
            }
            
ksort($weighted_host);

            foreach(
$weighted_host as $host) {
                if (!(
$fp = @fsockopen($host25$errno$errstr))) {
                    continue;
                }

                
$stopTime time() + 12;
                
$gotResponse FALSE;
                
stream_set_blocking($fpFALSE);

                while(
true) {
                    
$strresp fgets($fp1024);
                    if(
substr($strresp03) == "220") {
                        
$stopTime time() + 12;
                        
$gotResponse true;
                    } elseif ((
$strresp == "") && ($gotResponse)) {
                        break;
                    } elseif(
time() > $stopTime) {
                        break;
                    }
                }
                if(!
$gotResponse) {
                    continue;
                }
                
stream_set_blocking($fptrue);

                
fputs($fp"HELO {$_SERVER['SERVER_NAME']}\r\n");
                
fgets($fp1024);

                
fputs($fp"MAIL FROM: <httpd@{$_SERVER['SERVER_NAME']}>\r\n");
                
fgets($fp1024);

                
fputs($fp"RCPT TO: <$email>\r\n");
                
$line fgets($fp1024);

                
fputs($fp"QUIT\r\n");

                
fclose($fp);
                if(
substr($line03) != "250") {
                    return 
false;
                } else {
                    return 
true;
                }
            }
            return 
false;
        }
        return 
true;
    }
    return 
false;
}
?>