php驗證信箱是否真實存在

來源:互聯網
上載者:User
php驗證郵箱是否真實存在
大家好,我是php新手,現在有個問題想請教大家,就是註冊時驗證該email地址是否真的存在,謝謝

環境win32+php5.45

test.php

echo "
檢查電子郵件地址的正確性:
";
require("email_validation.php");
$newmail = "[email protected]";
$validator=new email_validation_class;
$validator->timeout=10;

//if(IsSet($newemail) && strcmp($newemail,"")){
if( ($result=$validator->ValidateEmailBox($newmail) )<0){
echo "不能確定您的信箱是否正確. 您的信箱離這裡太遠了吧?
";
return;
}else{
echo "22222222";
if(!$result){
echo "您輸入的信箱地址是不正確的! :)
";
return;
}else{
echo "郵箱合法!
";
}
}

?>



email_validation.php


class email_validation_class
{
//var $email_regular_expression="^([a-z0-9_] |//- |//.)+@(([a-z0-9_] |//-)+//.)+[a-z]{2,4}$";
var $timeout=0;
var $localhost="";
var $localuser="";
var $hosts=0;

Function GetLine($connection)
{
for($line="";;)
{
if(feof($connection))
return(0);
$line.=fgets($connection,100);
$length=strlen($line);
if($length>=2 && substr($line,$length-2,2)=="/r/n")
return(substr($line,0,$length-2));
}
}

Function PutLine($connection,$line)
{
return(fputs($connection,"$line/r/n"));
}

Function ValidateEmailAddress($email) {
//return(eregi($this->email_regular_expression,$email)!=0);
//origin:
//return(eregi("^([a-z0-9_] |//- |//.)+@(([a-z0-9_] |//-)+//.)+[a-z]{2,4}$",$email)!=0);

//return preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $_REQUEST[$email]);
return preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $_REQUEST[$email]);

}

Function ValidateEmailHost($email,$hosts=0)
{
if(!$this->ValidateEmailAddress($email))
return(0);
$user=strtok($email,"@");
$domain=strtok("");
if(GetMXRR($domain,$hosts,$weights))
{
$mxhosts=array();
for($host=0;$host$mxhosts[$weights[$host]]=$hosts[$host];
KSort($mxhosts);
for(Reset($mxhosts),$host=0;$host$hosts[$host]=$mxhosts[Key($mxhosts)];
}
else
{
$hosts=array();
if(strcmp(@gethostbyname($domain),$domain)!=0)
$hosts[]=$domain;
}
return(count($hosts)!=0);
}

Function VerifyResultLines($connection,$code)
{
while(($line=$this->GetLine($connection)))
{
if(!strcmp(strtok($line," "),$code))
return(1);
if(strcmp(strtok($line,"-"),$code))
return(0);
}
return(-1);
}

Function ValidateEmailBox($email)
{
if(!$this->ValidateEmailHost($email,$hosts))
return(0);
if(!strcmp($localhost=$this->localhost,"") && !strcmp($localhost=getenv("SERVER_NAME"),"") && !strcmp($localhost=getenv("HOST"),""))
$localhost="localhost";
if(!strcmp($localuser=$this->localuser,"") && !strcmp($localuser=getenv("USERNAME"),"") && !strcmp($localuser=getenv("USER"),""))
$localuser="root";
for($host=0;$host{
if(($connection=($this->timeout ? fsockopen($hosts[$host],25,$errno,$error,$this->timeout) : fsockopen($hosts[$host],25))))
{
if($this->VerifyResultLines($connection,"220")>0 && $this->PutLine($connection,"HELO $localhost") && $this->VerifyResultLines($connection,"250")>0 && $this->PutLine($connection,"MAIL FROM: <$localuser@$localhost>") && $this->VerifyResultLines($connection,"250")>0 && $this->PutLine($connection,"RCPT TO: <$email>") && ($result=$this->VerifyResultLines($connection,"250"))>=0)
{
fclose($connection);
return($result);
}
fclose($connection);
}
}
return(-1);
}
};

?>



分享到:


------解決方案--------------------
不懂,我的想法就是,最多驗證一下格式是否正確,然後像大家一樣,往他郵箱發啟用碼,用以完成註冊的最後一步。你想要直接驗證,除非郵箱服務商提供介面讓你驗證。不然。
------解決方案--------------------
email 的真實性是無法判斷的,雖然任何一個郵件伺服器都有郵箱列表功能。但大多都不會對外開放
需要用郵件啟用註冊,看上去不錯。但是建立在網站管理員的職業道德上的。你收到的那些廣告郵件就是這樣來的
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.