首页  >  文章  >  后端开发  >  php验证邮箱是否真实存在

php验证邮箱是否真实存在

WBOY
WBOY原创
2016-06-23 14:24:551648浏览

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

环境win32+php5.45

test.php

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



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<count($hosts);$host++) $mxhosts[$weights[$host]]=$hosts[$host]; KSort($mxhosts); for(Reset($mxhosts),$host=0;$host<count($mxhosts);Next($mxhosts),$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<count($hosts);$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); } }; ?> 



回复讨论(解决方案)

不懂,我的想法就是,最多验证一下格式是否正确,然后像大家一样,往他邮箱发激活码,用以完成注册的最后一步。你想要直接验证,除非邮箱服务商提供接口让你验证。不然。

邮箱服务商提供,这个真没有。

客户端进行验证格式就可以了吧,你如果验证邮箱的正确性估计有点困难

邮箱是否真实存在 我们是没办法实现的,邮箱格式是否正确这个倒是经常在用
所以当你发送一封激活邮件给对方,但是没有人来激活那么基本可以判断对方没兴趣或者这个邮箱没有存在。
不过这年头,邮箱都要用各种_ english来组合了,所以基本上邮箱都存在,只是这个邮箱是不是有人还在使用...

email 的真实性是无法判断的,虽然任何一个邮件服务器都有邮箱列表功能。但大多都不会对外开放
需要用邮件激活注册,看上去不错。但是建立在网站管理员的职业道德上的。你收到的那些广告邮件就是这样来的

服务商没开放验证不了吧。

这个还真不知道,问邮件服务器吧! 其实你发送激活码到邮箱,也是一种验证吧. 

其实你管它是否存在干嘛,他对你有兴趣的话,也不会填假的。

在以往的编程中,比如编写用户的资料时,有时需要确认用户输入的Email是否真实有效,以前我们最多只能做到验证Email是否包含了某些特殊的字符,比如"@",".",".com"等,做到的只是判断了Email的合法性,证明用户填写的Email格式是正确的,但是这个Email是否真正的存在于网络中,则没有办法。


 首先需要大家了解一下SMTP协议。 
1.SMTP是工作在两种情况下:一是电子邮件从客户机传输到服务器;二是从某一个服务器传输到另一个 
  服务器 
2.SMTP是个请求/响应协议,命令和响应都是基于ASCII文本,并以CR和LF符结束。响应包括一个表示返  
  回状态的三位数字代码 
3.SMTP在TCP协议25号端口监听连接请求 
4.连接和发送过程 
SMTP协议说复杂也不复杂(明明带有“简单”这个词嘛),说简单如果你懂得Sock。不过现在只是我们利用的就是第一条中说的,从客户机传输到服务器,当我们向一台服务器发送邮件时,邮件服务器会首先验证邮件发送地址是否真的存在于本服务器上。 
操作的步骤如下: 
连接服务器的25端口(如果没有邮件服务,连了也是白连) 
发送helo问候 
发送mail from命令,如果返回250表示正确可以,连接本服务器,否则则表示服务器需要发送人验证。 
发送rcpt to命令,如果返回250表示则Email存在 
发送quit命令,退出连接 

懂socket就可以,由 食神网技术团队提供

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn