PHP电子邮件验证脚本

时间:2015-01-19 13:44:18

标签: php validation email

我在这里的第一篇文章,虽然我多次使用这个网站来查找代码修复。

我一直在使用脚本验证我的旧主机上的电子邮件是否正常工作,然而新主机(123-reg)根本没有正确验证它们。

此脚本实际上验证了电子邮件的存在,而不仅仅是其格式。

请参阅以下链接 t43.co.uk/emailcheck.php?email=asdasdasd@hotmail.com(工作正常) smile-database.co.uk/emailcheck.php?email=asdasdasd@hotmail.com(当我知道它不是时,总是说有效)

我的脚本在下面。

function checkEmail( $email, $chFail = false ) 
{
    $msgs = Array(); 
    $msgs[] = 'Received email address: '.$email;

    if( !preg_match( "/^(([^<>()[\]\\\\.,;:\s@\"]+(\.[^<>()[\]\\\\.,;:\s@\"]+)*)|(\"([^\"\\\\\r]|(\\\\[\w\W]))*\"))@((\[([0-9]{1,3}\.){3}[0-9]{1,3}\])|(([a-z\-0-9áàäçéèêñóòôöüæøå]+\.)+[a-z]{2,}))$/i", $email ) ) 
    {
        $msgs[] = 'Email address was not recognised as a valid email pattern<br><br>';
        return $chFail ? Array( false, $msgs ) : false; 
    }

    $msgs[] = 'Email address was recognised as a valid email pattern';
    //get the mx host name

    if( preg_match( "/@\[[\d.]*\]$/", $email ) )
    {
        $mxHost[0] = preg_replace( "/[\w\W]*@\[([\d.]+)\]$/", "$1", $email );
        $msgs[] = 'Email address contained IP address '.$mxHost[0].' - no need for MX lookup';
    }        
    else 
    {
        //get all mx servers - if no MX records, assume domain is MX (SMTP RFC)

        $domain = preg_replace( "/^[\w\W]*@([^@]*)$/i", "$1", $email );
        if( !getmxrr( $domain, $mxHost, $weightings ) ) 
        { 

            $mxHost[0] = $domain;

            $msgs[] = 'Failed to obtain MX records, defaulting to '.$domain.' as specified by SMTP protocol';
        }
        else 
        { 
            array_multisort( $weightings, $mxHost );
            $cnt = ''; $co = 0; foreach( $mxHost as $ch ) { $cnt .= ( $cnt ? ', ' : '' ) . $ch . ' (' . $weightings[$co] . ')'; $co++; }
            $msgs[] = 'Obtained the following MX records for '.$domain.': '.$cnt; 
        }
    }
    //check each server until you are given permission to connect, then check only that one server

    foreach( $mxHost as $currentHost ) 
    {
        $msgs[] = 'Checking MX server: '.$currentHost;

        if( $connection = @fsockopen( $currentHost, 25 ) ) 
        {
            $msgs[] = 'Created socket ('.$connection.') to '.$currentHost;

            if( preg_match( "/^2\d\d/", $cn = fgets( $connection, 1024 ) ) ) 
            {
                $msgs[] = $currentHost.' sent SMTP connection header - no futher MX servers will be checked: '.$cn;

                while( preg_match( "/^2\d\d-/", $cn ) ) 
                { 
                    $cn = fgets( $connection, 1024 );
                    $msgs[] = $currentHost.' sent extra connection header: '.$cn; 
                }

                if( !$_SERVER ) 
                { 
                    global $HTTP_SERVER_VARS; $_SERVER = $HTTP_SERVER_VARS; 
                }

                //attempt to send an email from the user to themselves (not <> as some misconfigured servers reject it)
                echo $_SERVER['HTTP_HOST'] . "<BR>";
                $localHostIP = gethostbyname(preg_replace("/^.*@|:.*$/",'',$_SERVER['HTTP_HOST']));
                echo $localHostIP . "<BR>";
                $localHostName = gethostbyaddr($localHostIP);
                fputs( $connection, 'HELO '.($localHostName?$localHostName:('['.$localHostIP.']'))."\r\n" );
                if( $success = preg_match( "/^2\d\d/", $hl = fgets( $connection, 1024 ) ) ) 
                {
                    $msgs[] = $currentHost.' sent HELO response: '.$hl;

                    fputs( $connection, "MAIL FROM: <$email>\r\n" );
                    if( $success = preg_match( "/^2\d\d/", $from = fgets( $connection, 1024 ) ) ) 
                    {
                        $msgs[] = $currentHost.' sent MAIL FROM response: '.$from;

                        fputs( $connection, "RCPT TO: <$email>\r\n" );
                        if( $success = preg_match( "/^2\d\d/", $to = fgets( $connection, 1024 ) ) ) 
                        {
                            $msgs[] = $currentHost.' sent RCPT TO response: '.$to;
                        } 
                        else 
                        { 
                            $msgs[] = $currentHost.' rejected recipient: '.$to; 
                        }
                    } 
                    else 
                    { 
                        $msgs[] = $currentHost.' rejected MAIL FROM: '.$from; 
                    }
                } 
                else 
                { 
                    $msgs[] = $currentHost.' rejected HELO: '.$hl; 
                }

                fputs( $connection, "QUIT\r\n");
                fgets( $connection, 1024 ); fclose( $connection );
                //see if the transaction was permitted (i.e. does that email address exist)
                $msgs[] = $success ? ('Email address was accepted by '.$currentHost) : ('Email address was rejected by '.$currentHost);                
                return $chFail ? Array( $success, $msgs ) : $success;

            } 
            elseif ( preg_match( "/^550/", $cn ) ) 
            {
                $msgs[] = 'Mail domain denies connections from this host - no futher MX servers will be checked: '.$cn;
                return $chFail ? Array( false, $msgs ) : false;
            } 
            else 
            { 
                $msgs[] = $currentHost.' did not send SMTP connection header: '.$cn; 
            }
        } 
        else 
        { 
            $msgs[] = 'Failed to create socket to '.$currentHost;
        }
    } 
    $msgs[] = 'Could not establish SMTP session with any MX servers';
    return $chFail ? Array( false, $msgs ) : false;
}    

echo "<br><br>Email Validation Check<br><br>";


$return_msgs = checkEmail( $_REQUEST['email'], true );

if ( $return_msgs[0] == 0 ) 
{
    echo "<img src='img/Fail.png'> May be invalid<br>".$_REQUEST['email'];
}
elseif ( $return_msgs[0] == 1 ) 
{
    echo "<img src='img/OK.png'> Valid<br>".$_REQUEST['email'];
}
else
{
    echo "<img src='img/Caution.png'> Caution<br>".$_REQUEST['email'];
}

1 个答案:

答案 0 :(得分:0)

我已阅读有关电子邮件验证的one great topic。 作者建议使用这个正则表达式:/.+@.+\..+/i并对很多“为什么正是这个正则表达式”给出一个很好的描述(用俄语)

  

'somestring'@'somestring'。'somestring'