检查电子邮件是否存在php [英] Check if email exist php

查看:115
本文介绍了检查电子邮件是否存在php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我有一个php脚本来检查电子邮件地址是否存在。



但是,似乎yahoo,hotmail,aol和其他提供商正在接受任何电子邮件,而不是拒绝无效的电子邮件。



只有Gmail,像stackoverflow.com这样的许多域名都拒绝了无电子邮件。



检查我的脚本,让我知道如果我可以做一些检查雅虎和其他人。



html post form

 < html> 
< body>
< form action =checkemail.phpmethod =POST>
< b>电子邮件< / b> < input type =textname =email>
< input type =submit>
< / form>
< / body>
< / html>

php

 <?php 

/ *验证电子邮件地址。 * /
函数jValidateEmailUsingSMTP($ sToEmail,$ sFromDomain =gmail.com,$ sFromEmail =email@gmail.com,$ bIsDebug = false){

$ bIsValid = true ; //假设地址默认是有效的。
$ aEmailParts = explode(@,$ sToEmail); //提取用户/域..
getmxrr($ aEmailParts [1],$ aMatches); //获取mx记录..

if(sizeof($ aMatches)== 0){
return false; // no mx records ..
}

foreach($ aMatches as $ oValue){

if($ bIsValid&&!isset($ sResponseCode )$ {

//打开连接..
$ oConnection = @fsockopen($ oValue,25,$ errno,$ errstr,30);
$ oResponse = @fgets($ oConnection);

if(!$ oConnection){

$ aConnectionLog ['Connection'] =ERROR;
$ aConnectionLog ['ConnectionResponse'] = $ errstr;
$ bIsValid = false; //无法连接..

} else {

$ aConnectionLog ['Connection'] =SUCCESS;
$ aConnectionLog ['ConnectionResponse'] = $ errstr;
$ bIsValid = true; //这么好..

}

if(!$ bIsValid){

if($ bIsDebug)print_r($ aConnectionLog) ;
返回false;

}

//向服务器发出问候。
fputs($ oConnection,HELO $ sFromDomain\r
$ oResponse = fgets($ oConnection);
$ aConnectionLog ['HELO'] = $ oResponse;

//从...发送电子邮件
fputs($ oConnection,MAIL FROM:< $ sFromEmail> \r\\\
);
$ oResponse = fgets($ oConnection);
$ aConnectionLog ['MailFromResponse'] = $ oResponse;

//发送电子邮件到..
fputs($ oConnection,RCPT TO:< $ sToEmail> \r\\\
);
$ oResponse = fgets($ oConnection);
$ aConnectionLog ['MailToResponse'] = $ oResponse;

//获取响应代码..
$ sResponseCode = substr($ aConnectionLog ['MailToResponse'],0,3);
$ sBaseResponseCode = substr($ sResponseCode,0,1);

//说再见..
fputs($ oConnection,QUIT\r\\\
);
$ oResponse = fgets($ oConnection);

//获取退出代码和响应。
$ aConnectionLog ['QuitResponse'] = $ oResponse;
$ aConnectionLog ['QuitCode'] = substr($ oResponse,0,3);

if($ sBaseResponseCode ==5){
$ bIsValid = false; //地址无效..
}

//关闭连接..
@fclose($ oConnection);

}

}

if($ bIsDebug){
print_r($ aConnectionLog); //输出调试信息..
}

return $ bIsValid;

}
$ email = $ _POST ['email'];

$ bIsEmailValid = jValidateEmailUsingSMTP($ email,gmail.com,email@gmail.com);
echo $ bIsEmailValid? < b取代;有效<!/ B> 中:无效!:(;
?>


解决方案

如果您需要确保电子邮件地址存在,请发送电子邮件,它应该包含一个带有随机ID的链接,只有当该链接被点击并且包含正确的随机ID ,用户的帐户被激活(或发布广告,发送订单,或者您正在做的任何事情)。



这是验证存在的唯一可靠的方法的电子邮件地址,并确保填写表单的人员可以访问。


I have a question, i have a php script to check if the email address exist.

But appear that yahoo, hotmail, aol and others providers are accepting any emails and not rejecting the invalid emails.

Only Gmail, and many domains like stackoverflow.com are rejecting the no vaild emails.

Check my script and let me know if i can do some to check the yahoo and others.

html post form

<html>
<body>
<form action="checkemail.php" method="POST">
<b>E-mail</b> <input type="text" name="email">
<input type="submit">
</form>
</body>
</html>

php

<?php

/* Validate an email address. */
function jValidateEmailUsingSMTP($sToEmail, $sFromDomain = "gmail.com", $sFromEmail = "email@gmail.com", $bIsDebug = false) {

    $bIsValid = true; // assume the address is valid by default..
    $aEmailParts = explode("@", $sToEmail); // extract the user/domain..
    getmxrr($aEmailParts[1], $aMatches); // get the mx records..

    if (sizeof($aMatches) == 0) {
        return false; // no mx records..
    }

    foreach ($aMatches as $oValue) {

        if ($bIsValid && !isset($sResponseCode)) {

            // open the connection..
            $oConnection = @fsockopen($oValue, 25, $errno, $errstr, 30);
            $oResponse = @fgets($oConnection);

            if (!$oConnection) {

                $aConnectionLog['Connection'] = "ERROR";
                $aConnectionLog['ConnectionResponse'] = $errstr;
                $bIsValid = false; // unable to connect..

            } else {

                $aConnectionLog['Connection'] = "SUCCESS";
                $aConnectionLog['ConnectionResponse'] = $errstr;
                $bIsValid = true; // so far so good..

            }

            if (!$bIsValid) {

                if ($bIsDebug) print_r($aConnectionLog);
                return false;

            }

            // say hello to the server..
            fputs($oConnection, "HELO $sFromDomain\r\n");
            $oResponse = fgets($oConnection);
            $aConnectionLog['HELO'] = $oResponse;

            // send the email from..
            fputs($oConnection, "MAIL FROM: <$sFromEmail>\r\n");
            $oResponse = fgets($oConnection);
            $aConnectionLog['MailFromResponse'] = $oResponse;

            // send the email to..
            fputs($oConnection, "RCPT TO: <$sToEmail>\r\n");
            $oResponse = fgets($oConnection);
            $aConnectionLog['MailToResponse'] = $oResponse;

            // get the response code..
            $sResponseCode = substr($aConnectionLog['MailToResponse'], 0, 3);
            $sBaseResponseCode = substr($sResponseCode, 0, 1);

            // say goodbye..
            fputs($oConnection,"QUIT\r\n");
            $oResponse = fgets($oConnection);

            // get the quit code and response..
            $aConnectionLog['QuitResponse'] = $oResponse;
            $aConnectionLog['QuitCode'] = substr($oResponse, 0, 3);

            if ($sBaseResponseCode == "5") {
                $bIsValid = false; // the address is not valid..
            }

            // close the connection..
            @fclose($oConnection);

        }

    }

    if ($bIsDebug) {
        print_r($aConnectionLog); // output debug info..
    }

    return $bIsValid;

}
$email = $_POST['email'];

$bIsEmailValid = jValidateEmailUsingSMTP("$email", "gmail.com", "email@gmail.com");
echo $bIsEmailValid ? "<b>Valid!</b>" : "Invalid! :(";
?>

解决方案

If you need to make super sure that an E-Mail address exists, send an E-Mail to it. It should contain a link with a random ID. Only when that link is clicked, and contains the correct random ID, the user's account is activated (or ad published, or order sent, or whatever it is that you are doing).

This is the only reliable way to verify the existence of an E-Mail address, and to make sure that the person filling in the form has access to it.

这篇关于检查电子邮件是否存在php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆