首頁 >後端開發 >php教程 > php验证信箱是否真实存在

php验证信箱是否真实存在

WBOY
WBOY原創
2016-06-13 12:34:53952瀏覽

php验证邮箱是否真实存在
大家好,我是php新手,现在有个问题想请教大家,就是注册时验证该email地址是否真的存在,谢谢 

环境win32+php5.45

test.php

<br />
<?php<br />
echo "<center><br/>检查电子邮件地址的正确性:<br/>"; <br />
require("email_validation.php"); <br />
$newmail = "283285993@qq.com"; <br />
$validator=new email_validation_class; <br />
$validator->timeout=10; <br />
<br />
//if(IsSet($newemail) && strcmp($newemail,"")){<br />
	if( ($result=$validator->ValidateEmailBox($newmail) )<0){ <br />
		echo "不能确定您的信箱是否正确. 您的信箱离这里太远了吧?<br/>"; <br />
		return; <br />
	}else{ <br />
			echo "22222222";<br />
		if(!$result){ <br />
			echo "您输入的信箱地址是不正确的! :)<br/>"; <br />
			return; <br />
		}else{<br />
			echo "邮箱合法!<br/>"; <br />
		} <br />
	}<br />
<br />
?><br />



email_validation.php

<br />
<? <br />
class email_validation_class <br />
{ <br />
//var $email_regular_expression="^([a-z0-9_] |//- |//.)+@(([a-z0-9_] |//-)+//.)+[a-z]{2,4}$"; <br />
var $timeout=0; <br />
var $localhost=""; <br />
var $localuser=""; <br />
var $hosts=0;<br />
<br />
Function GetLine($connection) <br />
{ <br />
for($line="";;) <br />
{ <br />
if(feof($connection)) <br />
return(0); <br />
$line.=fgets($connection,100); <br />
$length=strlen($line); <br />
if($length>=2 && substr($line,$length-2,2)=="/r/n") <br />
return(substr($line,0,$length-2)); <br />
} <br />
} <br />
<br />
Function PutLine($connection,$line) <br />
{ <br />
return(fputs($connection,"$line/r/n")); <br />
} <br />
<br />
Function ValidateEmailAddress($email) { <br />
//return(eregi($this->email_regular_expression,$email)!=0); <br />
//origin:<br />
//return(eregi("^([a-z0-9_] |//- |//.)+@(([a-z0-9_] |//-)+//.)+[a-z]{2,4}$",$email)!=0); <br />
<br />
//return preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $_REQUEST[$email]);<br />
return preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $_REQUEST[$email]);<br />
<br />
} <br />
<br />
Function ValidateEmailHost($email,$hosts=0) <br />
{ <br />
if(!$this->ValidateEmailAddress($email)) <br />
return(0); <br />
$user=strtok($email,"@"); <br />
$domain=strtok(""); <br />
if(GetMXRR($domain,$hosts,$weights)) <br />
{ <br />
$mxhosts=array(); <br />
for($host=0;$host<count($hosts);$host++) <br />
$mxhosts[$weights[$host]]=$hosts[$host]; <br />
KSort($mxhosts); <br />
for(Reset($mxhosts),$host=0;$host<count($mxhosts);Next($mxhosts),$host++) <br />
$hosts[$host]=$mxhosts[Key($mxhosts)]; <br />
} <br />
else <br />
{ <br />
$hosts=array(); <br />
if(strcmp(@gethostbyname($domain),$domain)!=0) <br />
$hosts[]=$domain; <br />
} <br />
return(count($hosts)!=0); <br />
} <br />
<br />
Function VerifyResultLines($connection,$code) <br />
{ <br />
while(($line=$this->GetLine($connection))) <br />
{ <br />
if(!strcmp(strtok($line," "),$code)) <br />
return(1); <br />
if(strcmp(strtok($line,"-"),$code)) <br />
return(0); <br />
} <br />
return(-1); <br />
} <br />
<br />
Function ValidateEmailBox($email) <br />
{ <br />
if(!$this->ValidateEmailHost($email,$hosts)) <br />
return(0); <br />
if(!strcmp($localhost=$this->localhost,"") && !strcmp($localhost=getenv("SERVER_NAME"),"") && !strcmp($localhost=getenv("HOST"),"")) <br />
$localhost="localhost"; <br />
if(!strcmp($localuser=$this->localuser,"") && !strcmp($localuser=getenv("USERNAME"),"") && !strcmp($localuser=getenv("USER"),"")) <br />
$localuser="root"; <br />
for($host=0;$host<count($hosts);$host++) <br />
{ <br />
if(($connection=($this->timeout ? fsockopen($hosts[$host],25,$errno,$error,$this->timeout) : fsockopen($hosts[$host],25)))) <br />
{ <br />
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) <br />
{ <br />
fclose($connection); <br />
return($result); <br />
} <br />
fclose($connection); <br />
} <br />
} <br />
return(-1); <br />
} <br />
}; <br />
<br />
?> <br />
<br />


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn