分享

SMTP判断邮箱是否存在,检查email地址是否真实存在 - PHP技术文章 - PHP中...

 todaytomo 2007-11-20

SMTP判断邮箱是否存在,检查email地址是否真实存在

SMTP判断邮箱是否存在,检查email地址是否真实存在

判断一个Email是否存在的类
作者:mlemos
来源:www.
复制内容到剪贴板
PHP 代码:
  1. <?
  2. /*
  3. *  email_validation.php
  4. *
  5. *
  6. */
  7.  
  8. class  email_validation_class
  9. {
  10. //var  $email_regular_expression="^([a-z0-9_]  |\-  |\.)+@(([a-z0-9_]  |\-)+\.)+[a-z]{2,4}$";
  11.    var  $timeout=0;
  12.    var  $localhost="";
  13.    var  $localuser="";
  14.  
  15.    Function  GetLine($connection)
  16.    {
  17.      for($line="";;)
  18.      {
  19.        if(feof($connection))
  20.        return(0);
  21.        $line.=fgets($connection,100);
  22.        $length=strlen($line);
  23.        if($length>=2  &&  substr($line,$length-2,2)=="rn")
  24.        return(substr($line,0,$length-2));
  25.      }
  26.    }
  27.  
  28.    Function  PutLine($connection,$line)
  29.    {
  30.      return(fputs($connection,"$linern"));
  31.    }
  32.  
  33.    Function  ValidateEmailAddress($email)
  34.    {
  35.      //return(eregi($this->email_regular_expression,$email)!=0);
  36.      return(eregi("^([a-z0-9_]  |\-  |\.)+@(([a-z0-9_]  |\-)+\.)+[a-z]{2,4}$",$email)!=0);
  37.    }
  38.  
  39.    Function  ValidateEmailHost($email,$hosts=0)
  40.    {
  41.      if(!$this->ValidateEmailAddress($email))
  42.        return(0);
  43.      $user=strtok($email,"@");
  44.      $domain=strtok("");
  45.      if(GetMXRR($domain,&$hosts,&$weights))
  46.      {
  47.        $mxhosts=array();
  48.        for($host=0;$host<count($hosts);$host++)
  49.        $mxhosts[$weights[$host]]=$hosts[$host];
  50.        KSort($mxhosts);
  51.        for(Reset($mxhosts),$host=0;$host<count($mxhosts);Next($mxhosts),$host++)
  52.        $hosts[$host]=$mxhosts[Key($mxhosts)];
  53.      }
  54.      else
  55.      {
  56.        $hosts=array();
  57.        if(strcmp(@gethostbyname($domain),$domain)!=0)
  58.        $hosts[]=$domain;
  59.      }
  60.      return(count($hosts)!=0);
  61.    }
  62.  
  63.    Function  VerifyResultLines($connection,$code)
  64.    {
  65.      while(($line=$this->GetLine($connection)))
  66.      {
  67.        if(!strcmp(strtok($line,"  "),$code))
  68.        return(1);
  69.        if(strcmp(strtok($line,"-"),$code))
  70.        return(0);
  71.      }
  72.      return(-1);
  73.    }
  74.  
  75.    Function  ValidateEmailBox($email)
  76.    {
  77.      if(!$this->ValidateEmailHost($email,&$hosts))
  78.        return(0);
  79.      if(!strcmp($localhost=$this->localhost,"")  &&  !strcmp($localhost=getenv("SERVER_NAME"),"")  &&  !strcmp($localhost=getenv("HOST"),""))
  80.        $localhost="localhost";
  81.      if(!strcmp($localuser=$this->localuser,"")  &&  !strcmp($localuser=getenv("USERNAME"),"")  &&  !strcmp($localuser=getenv("USER"),""))
  82.        $localuser="root";
  83.      for($host=0;$host<count($hosts);$host++)
  84.      {
  85.        if(($connection=($this->timeout    fsockopen($hosts[$host],25,&$errno,&$error,$this->timeout)  :  fsockopen($hosts[$host],25))))
  86.        {
  87.        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)
  88.        {
  89.          fclose($connection);
  90.          return($result);
  91.        }
  92.        fclose($connection);
  93.        }
  94.      }
  95.      return(-1);
  96.    }
  97. };
  98.  
  99. ?>
     

TOP

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多