<?
/* Mail_VRFY_Class was written by chris sandy chris@jynx.net 
    based on Jeremy Kister's Mail::VRFY perl module. 
    http://search.cpan.org/~jkister/Mail-VRFY/
    
Return Codes

0 The email address appears to be valid.
1 No email address was supplied.
2 There is a syntactical error in the email address.
3 There are no MX or A DNS records for the host in question.
4 There are no SMTP servers accepting connections.
5 All SMTP servers are misbehaving and wont accept mail.

Methods 

   syntax - check syntax of email address only (no network testing).
   compat - check syntax, and MX connectivity
   extended - compat + talk SMTP to see if server will reject RCPT TO

Corrent format to pass to Mail_VRFY_Class is:  chris@example.com 

Example:

#!/usr/local/bin/php -q
<?
include 'mail_vrfy_class.php';

        // this is an example using CLI  (example.php <addr> <method> <timeout>) 
        // You can use this script via web based as well. 
        //        $addr = $_POST['email'];
        //        $method = $_POST['method'];
        //       $timeout = '15000';
        
        $vrfy = new Mail_Vrfy;
        $addr = $argv[1];
        $method = $argv[2]; // method types (syntax,compat,extended)

        unset($debug); // $debug = 1;  // for debugging
        $timeout = $argv[4]; // default is 15000 seconds

                if($vrfy->CheckAddress($addr,$method,$debug,$timeout) > 0)
                {
                        print "\nSorry, your email address is not valid.\n";
                } 
                else 
                {
                        print $addr." is a valid email address.\n";
                }
?>
*/
ini_set('display_errors',"0"); 

class 
Mail_Vrfy{
function 
syntax($addr,$method syntax,$debug)
{
$addr_confirm explode("@"$addr);
        if (
sizeof($addr_confirm) != 2)
        {
        if(isset(
$debug)) print "invalid email address\n";
        return 
2;
        }
        
$check_tld explode("."$addr_confirm[1]);
        
$size sizeof($check_tld);
        if(
$size ){
            if(isset(
$debug)) print "invalid email address. no ltd\n"
        return 
2;
        }
            if(
strlen($addr) > 256)
            {
                if(isset(
$debug)) print "email address is more than 256 characters\n";
                return 
2;
            }
            if(
strlen($addr_confirm[1]) > 255)
            {
                if(isset(
$debug)) print "domain in email address is more than 255 characters\n";
                return 
2;
            }
            return 
0;
}
        function 
compat($addr$method compat$debug $timeout 15000)
        {
        
$syntax $this->syntax($addr,$method,$debug);
            if(
$syntax 0){
                return 
$syntax;
                }else{
                    
$addr_confirm explode("@"$addr);
                if (!
getmxrr($addr_confirm[1], $mxhosts)) {
                    if(isset(
$debug)) print "There are no MX records for the host in question.\n"; return 3;
                }
                    while (list(
$nul$mxhost) = each($mxhosts))
                    {
                          
$conn fsockopen($mxhost25$timeout);
                        if (!
$conn){
                        if(isset(
$debug)) print "There are no SMTP servers accepting connections.\n"
                        return 
4;
                        }
                        
fclose($conn);
                    }
                }
                return 
0;
            }

        function 
extended($addr,$method,$debug,$timeout 15000)
        {
            
$check_compat $this->compat($addr$method$debug $timeout 15000);
            if(
$check_compat 0){
                return 
$check_compat;
                }else{
                    
/* we get our mx records */
                    
$addr_confirm explode("@"$addr);
                    
getmxrr($addr_confirm[1], $mxhosts);
                        
/* test each mx record */
                    
while (list($nul$mxhost) = each($mxhosts))
                    {
                        if(isset(
$debug)) print "mx host: $mxhost \n";
                        
$conn fsockopen($mxhost25$timeout);
                              if (!
$conn){
                            if(isset(
$debug)) print "$mxhost is not accepting connections.\n"
                            
$mx_flag=1;
                            } else {
                            if(isset(
$debug)) print "$mxhost is accepting connections.\n"
                            unset(
$mx_flag);
                            
                                
//while (!feof($conn)) {
                                
$output fread($conn1024);
                                if(isset(
$debug)) print "banner output = $output \n";
                                
/* check for a banner */
                                
if (!preg_match("/^220/i"$output)){
                                if(isset(
$debug)) print "$mxhost misbehaving: bad banner\n";
                                    
$misbehave=1;
                                } else {
                                if(isset(
$debug)) print "$mxhost we got a good banner\n";
                                    unset(
$misbehave);
                                }
                                
/* test for a helo */
                                
$me trim(`hostname -f`);
                                
/* send our helo from hostname */
                                
$helo "helo ".$me."\r\n";
                                
fputs($conn$helo);
                                
$output fread($conn128);
                            
                                if(isset(
$debug)) print "helo output = $output \n";
                                if (!
preg_match("/^250/i"$output)){
                                    if(isset(
$debug)) print "$mxhost misbehaving: bad reply to HELO\n";
                                    
$badhelo=1;
                                } else {
                                    if(isset(
$debug)) print "$mxhost gave us a good reply to HELO\n";
                                    unset(
$badhelo);
                                }
                            
                            
$myfrom "MAIL FROM:<>\r\n";
                            
$rcpt "RCPT TO:<$addr>\r\n";
                            
/* send our mail from: */
                            
fputs($conn$myfrom);
                            
$mailfrom_out fread($conn,256);
                            if(isset(
$debug)) print "mailfrom_out = $mailfrom_out \n";
                            
/* send our rcpt to: <> */
                            
fputs($conn$rcpt);
                            
$rcpt_out fread($conn,256);
                                
/* we should get a 250 */
                                
if(isset($debug)) print "rcpt_out = $rcpt_out \n";
                            if (
preg_match("/^4[0-9]{2}/i"$rcpt_out)){
                                if(isset(
$debug)) print "$mxhost temp failed\n";
                                
$tmpfail=1;
                            } else {
                                if(isset(
$debug)) print "$mxhost no temp failure\n";
                                unset(
$tmpfail);
                            }
                            
                            if (
preg_match("/^5[0-9]{2}/i"$rcpt_out)){
                                if(isset(
$debug)) print "$mxhost misbehaving: rejected\n";
                                
$rejected=1;
                            } else {
                                if(isset(
$debug)) print "$mxhost didnt reject us\n";
                                unset(
$rejected);
                            }
                            
                            if (!
preg_match("/^250(.*)/i"$rcpt_out)){
                                if(isset(
$debug)) print "$mxhost misbehaving: bad reply to RCPT TO $rcpt_out\n";
                                
$badrcpt 1;
                            } else {
                            if(isset(
$debug)) print "$mxhost gave us a good reply to RCPT TO $rcpt_out\n";
                                unset(
$badrcpt);
                            }
                                
$quit "QUIT\r\n";
                                
fputs($conn$quit);
                                
//}
                                
fclose($conn);
                            }
                        
                    }
                    
            }
                    
            
/* results */
            
if(isset($mx_flag)) return 4// mx servers are not accepting mail
            
if(isset($misbehave)) return 5;
            if(isset(
$badhelo)) return 5;
            if(isset(
$badrcpt)) return 5;
            if(isset(
$tmpfail)) return 6;
            return 
0
        }
    function 
CheckAddress($addr,$method,$debug,$timeout)
    {
    
        if(empty(
$addr)){
            return 
1;
        }
            
preg_match("/<(.*)>/i",$addr,$match_out);
                if(
sizeof($match_out) > 0)
                {
                    
$addr=$match_out[1];
                }
        
        if(
$method == 'syntax')
        {
            return 
$this->syntax($addr,$method,$debug);
        }elseif(
$method == 'compat')
        {
            return 
$this->compat($addr,$method,$debug,$timeout);
        } elseif(
$method == 'extended')
        {
            return 
$this->extended($addr,$method,$debug,$timeout);
        } else 
        {
            return 
$this->compat($addr,$method,$debug,$timeout);
        }

    }
    
    
}

?>