又一验证EMAIL是否有效的好代码
<?php


/*验证EMAIL是否有效的代码
 *  以下为返回的数组
 *  Item 0:  [true|false]    true就是有效的EMAIL地址,false就是无效罗    
 *
 *  Item 1:  [SMTP Code]是否存在MX邮件服务器
 *
 *  Item 2:  [true|false] true是有效的邮件服务器
 *                    
 *
 *  Item 3:  [MX server]如果MX HOST存在并取得连接,就将此项添为MX SERVER的机名
 *
 * 以下是主代码
 */


function CheckMailWithHost$host$email )
{
    
// used for SMTP HELO argument
    
global $SERVER_NAME;
    
    
// 初始化返回值
    
$return[0] = false;
    
$return[1] = "Invalid email address (bad domain name)\";
    $return
[2] = false;
    $return
[3] = \"\";

    // 打开25端口
    $fp = fsockopen ( $host, 25 );

    // if the \'fp\' was set, then goto work
    if ( $fp )
    
{             
        // work variables
        $s = 0;
        $c = 0;
        $out = 
\"\";
                                
        
        set_socket_blocking ( $fp, true );

        // as long as our \'out\' variable has a
        // null value (
\"\")
        // keep looping (do) until we get
        // something
        //
        do
        
{
            // output of the stream assigned
            // to \'out\' variable
            $out = fgets ( $fp, 2500 );

            // if we get an 
\"220\" code (service ready code (i.e greeting))
            // increment our work (code (c)) variable, and null
            // out our output variable for a later loop test
            //
            if ( ereg ( 
\"^220\", $out ) )
            
{                                                                                
                $return
[2] = true;
                $return
[3] = $mxhosts[$i];
            
}
                                                        
            // Get the status of the socket to determine if there is still something to read.
            $stat=socket_get_status( $fp );            
                                            
        
} while ( $stat[\"unread_bytes\"] > 0 );

        // talk to the MX mail server,
        // validating ourself (HELO)
        fputs ( $fp, 
\"HELO $SERVER_NAME\\r\\n\" );

        // get the mail servers reply, assign to
        // \'output\' (ignored)
        $output = fgets ( $fp, 2000 );

        // give a bogus 
\"MAIL FROM:\" header to
        // the server
        fputs ( $fp, 
\"MAIL FROM: <nobody@\" . $SERVER_NAME .\">\\r\\n\" );
        // get output again (ignored)
        $output = fgets ( $fp, 2000 );

        // give RCPT TO: header for the email
        // address we are testing
        fputs ( $fp, 
\"RCPT TO: <$email>\\r\\n\" );                

        // get final output for validity testing
        // (used)
        $output = fgets ( $fp, 2000 );                        
        
        // test the reply code from the mail
        // server for the 250 (okay) code
        if ( ereg ( 
\"^250\", $output ) )
        
{
            // set our true/false(ness)
            // array item for testing
            $return
[0] = true;
        
}
        else
        
{
            // otherwise, bogus address,
            // fillin the 2nd array item
            // with the mail servers reply
            // code for user to test if they
            // want
            $return
[0] = false;
            $return
[1] = $output;
        
}
                
        
        // tell the mail server we are done
        // talking to it
        fputs ( $fp, 
\"QUIT\\r\\n\" );

        // close the file pointer
        fclose( $fp );
    
}

    return $return;
}

function validateEmail ( $email )
{                        
    // initialize the return values as if no MX record appears for the specified domain
    $return
[0] = false;
    $return
[1] = \"Invalid email address (bad domain name)\";
    $return
[2] = false;
    $return
[3] = \"\";
    

    // assign our user part and domain parts respectively to seperate             
    // variables
    list ( $user, $domain )  = split ( 
\"@\", $email, 2 );
    
    // split up the domain name into sub-parts
    $arr = explode ( 
\".\", $domain );

    // figure out how many parts to the host/domain name portion there are
    $count = count ( $arr );
                
    // flag to indicate success
    $bSuccess = false;
        
    // we try this for each qualification step of domain name
    // (from full qualified to TopLevel)
    for ( $i = 0; $i < $count - 1 && !$bSuccess; $i = $i +    1 )
    
{
            // create the domain name
        $domain = 
\"\";    
        for ( $j = $i; $j < $count; $j = $j + 1 )
        
{            
            $domain = $domain . $arr
[$j];
            if ( $j < $count - 1 )
                $domain = $domain . 
\".\";
        
}        
        
        // check that an MX record exists for Top-Level Domain, and if so
        // start our email address checking
        if ( checkdnsrr ( $domain, 
\"MX\" ) )     
        
{
            // Okay...valid dns reverse record; test that MX record for 
            // host exists, and then fill the \'mxhosts\' and \'weight\'
            // arrays with the correct information

            if ( getmxrr ( $domain, $mxhosts, $weight ) )                
            
{            
                // sift through the \'mxhosts\' connecting to each host
                for ( $i = 0; $i < count ( $mxhosts && !bSuccess ); $i++ )
                
{                    
                    $return = CheckMailWithHost( $mxhosts
[0], $email );
                    if ( $return
[0] == true )
                    
{                        
                        $bSuccess = true;                        
                        break;                    
                    
}                    
                
}                
            
}
        
}
        else
        // no mx records found --> try to use the domain itself
        
{    
            $return = CheckMailWithHost( $domain, $email );
            if ( $return
[0] == true )
            
{
                $bSuccess = true;   
                break;
            
}            
        
}
    
} 
        
    // return the array for the user to test against
    return $return;
}

?>