一般我們常希望拜訪你的網站的朋友能留下Email
但是很多人都會隨便打,造成治理員的困擾,
以下這個class可以線上檢查Email是否是有效的Email(存不存在)
%26lt;?
class CEmail {
var $email_regular_expression=%26quot;^([a-z0-9_]|\-|\.)+@(([a-z0-9_]|\-)+\.)+[a-z]{2,4}$%26quot;;
var $timeout=0;
var $localhost=%26quot;%26quot;;
var $localuser=%26quot;%26quot;;
Function GetLine($connection)
{
for($line=%26quot;%26quot;;;)
{
if(feof($connection))
return(0);
$line.=fgets($connection,100);
$length=strlen($line);
if($length%26gt;=2
%26amp;%26amp; substr($line,$length-2,2)==%26quot;rn%26quot;)
return(substr($line,0,$length-2));
}
}
Function PutLine($connection,$line)
{
return(fputs($connection,%26quot;$linern%26quot;));
}
Function VerifyRule($email)
{
return(eregi($this-%26gt;email_regular_expression,$email)!=0);
}
Function ValidateEmailHost($email,$hosts=0)
{
if(!$this-%26gt;VerifyRule($email))
return(0);
$user=strtok($email,%26quot;@%26quot;);
$domain=strtok(%26quot;%26quot;);
if(GetMXRR($domain,%26amp;$hosts,%26amp;$weights))
{
$mxhosts=array();
for($host=0;$host%26lt;count($hosts);$host++)
$mxhosts[$weights[$host]]=$hosts[$host];
KSort($mxhosts);
for(Reset($mxhosts),$host=0;$host%26lt;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-%26gt;GetLine($connection)))
{
if(!strcmp(strtok($line,%26quot; %26quot;),$code))
return(1);
if(strcmp(strtok($line,%26quot;-%26quot;),$code))
return(0);
}
return(-1);
}
Function VerifyOnline($email)
{
if(!$this-%26gt;ValidateEmailHost($email,%26amp;$hosts))
return(0);
if(!strcmp($localhost=$this-%26gt;localhost,%26quot;%26quot;)
%26amp;%26amp; !strcmp($localhost=getenv(%26quot;SERVER_NAME%26quot;),%26quot;%26quot;)
%26amp;%26amp; !strcmp($localhost=getenv(%26quot;HOST%26quot;),%26quot;%26quot;))
$localhost=%26quot;localhost%26quot;;
if(!strcmp($localuser=$this-%26gt;localuser,%26quot;%26quot;)
%26amp;%26amp; !strcmp($localuser=getenv(%26quot;USERNAME%26quot;),%26quot;%26quot;)
%26amp;%26amp; !strcmp($localuser=getenv(%26quot;USER%26quot;),%26quot;%26quot;))
$localuser=%26quot;root%26quot;;
for($host=0;$host%26lt;count($hosts);$host++)
{
if(($connection=($this-%26gt;timeout ? fsockopen($hosts[$host],25,%26amp;$errno,%26amp;$error,$this-%26gt;timeout) : fsockopen($hosts[$host],25))))
{
if($this-%26gt;VerifyResultLines($connection,%26quot;220%26quot;)%26gt;0
%26amp;%26amp; $this-%26gt;PutLine($connection,%26quot;HELO $localhost%26quot;)
%26amp;%26amp; $this-%26gt;VerifyResultLines($connection,%26quot;250%26quot;)%26gt;0
%26amp;%26amp; $this-%26gt;PutLine($connection,%26quot;MAIL FROM: %26lt;$localuser@$localhost%26gt;%26quot;)
%26amp;%26amp; $this-%26gt;VerifyResultLines($connection,%26quot;250%26quot;)%26gt;0
%26amp;%26amp; $this-%26gt;PutLine($connection,%26quot;RCPT TO: %26lt;$email%26gt;%26quot;)
%26amp;%26amp; ($result=$this-%26gt;VerifyResultLines($connection,%26quot;250%26quot;))%26gt;=0)
{
fclose($connection);
return($result);
}
fclose($connection);
}
}
return(-1);
}
function Verify($email,$type=0) {
if($type==0) return $this-%26gt;VerifyRule($email) ;
elsereturn $this-%26gt;VerifyOnline($email) ;
}
};
?%26gt;
用法:
$m=new CEmail;
//僅檢查語法
if($m-%26gt;Verify(%26quot;jerry@mail.jerry.com.tw%26quot;,0)) echo %26quot;有效%26quot;;
else echo %26quot;無效%26quot;;
//線上檢查是否真的有該Email
if($m-%26gt;Verify(%26quot;jerry@mail.jerry.com.tw%26quot;,1)) echo %26quot;有效%26quot;;
else echo %26quot;無效%26quot;;
全面測試email的有效性
2000-12-18 10:35:42 作者
相关文章
- · 用JavaMail发送带附件的Email
- · Java中使用XML创建EMAIL模板
- · 引用一个能发email的JavaMail的例子
- · 从outlook导入email地址
- · 怎样在APPLET中发EMAIL
- · 用java实现简单的email正则表达式判断
- · email address 生成图片程序
- · 很实用的一个完整email发送程序
- · 在jsp中发送email
- · 抓取动网论坛Email地址的一段代码
- · 常用Email组件发送函数
- · 表单递交合法性检测-Email
- · 验证email地址是否合法完整实例
- · 怎样写你自己的EMAIL组件(原理)
- · 用vbscript判断email地址的合法性
- · 转换文本为超联和Email格式的代码
- · 简单的检查输入email是否合法程序
- · 检查Email地址的比较完善的正则表达式函数
- · 怎 样 在 APPLET 中 发 EMAIL
- · 用java开发Email工具之发送邮件
