教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php邮箱验证类及正则匹配域名验证的实例代码

php邮箱验证类及正则匹配域名验证的实例代码

发布时间:2016-03-21   编辑:jiaochengji.com
本文介绍下,用php实现邮箱验证的一个类,以及用php正则匹配域名的方法,有需要的朋友,参考下吧。

完整代码如下:
 

复制代码 代码示例:
<?php
  /**
   * function getmxrr
   * 获取指定域名的MX记录信息
   * edit www.jbxue.com
  */
  function win_getmxrr($hostname, &$mxhosts, &$mxweight=false)
  {
     if (strtoupper(substr(PHP_OS, 0, 3)) != 'WIN') return;
     if (!is_array ($mxhosts) ) $mxhosts = array();
     if (empty($hostname)) return;
     $exec='nslookup -type=MX '.escapeshellarg($hostname);
     @exec($exec, $output);
     if (empty($output)) return;
     $i=-1;
     foreach ($output as $line) {
         $i++;
         if (preg_match("/^$hostname\tMX preference = ([0-9]+), mail exchanger = (.+)$/i", $line, $parts)) {
           $mxweight[$i] = trim($parts[1]);
           $mxhosts[$i] = trim($parts[2]);
         }
         if (preg_match('/responsible mail addr = (.+)$/i', $line, $parts)) {
           $mxweight[$i] = $i;
           $mxhosts[$i] = trim($parts[1]);
         }
     }
     return ($i!=-1);
  }
  if (!function_exists('getmxrr')) {
     function getmxrr($hostname, &$mxhosts, &$mxweight=false) {
         return win_getmxrr($hostname, $mxhosts, $mxweight);
     }
  }
 
  /**
   * class EmailLinker
   * 邮箱验证类,正则匹配邮箱地址并对域名是否有MX解析进行验证
  */
  class EmailLinker
  {
      CONST CONN_TIMEOUT = 10;
      CONST READ_TIMEOUT = 5;
      CONST SMTP_PORT = 25;
      private $email;
      function __construct($email)
      {
          $this->email = $email;
          $this->redirectIfNeeded();
      }
      //重定向邮箱地址
      function redirectIfNeeded()
      {
          if(array_key_exists('mail',$_GET) && $this->email == base64_decode($_GET['mail']) ){
              header('Location: mailto:'.$this->email);
              exit;
          }   
      }
 
      //回调返回邮箱地址
      public function link()
      {
          if($this->isValid()){
              $encoded = base64_encode($this->email);
              return '<a href="?mail='.urlencode($encoded).'" target="_blank">Email:'.$this->email.'</a>';
          }
      }
      //获取邮箱地址并验证
      public function isValid($mail = '')
      {
              static $valid = null;
              if($mail){
                  return ( $this->getParts() != null );
              }
              if($valid !== null){
                  return $valid;
              }
              if($parts = $this->getParts()){
                  $valid = $this->validateUser($parts['host'],$parts['user']);
              }
              return $valid;
      }
      private function validateUser($hostname,$user)
      {
          if($sock = $this->openSMTPSocket($hostname)){
              $this->smtpSend($sock,"HELO $hostname");
              $resp  = $this->smtpSend($sock,"MALL FROM: <$user@$hostname>");
              //$resp  = $this->smtpSend($sock,"RCPT TO:<$user@$hostname>"); //这一步可能被ISP过滤了,导致邮箱验证不成功
              $vaild = (preg_match('/250|451|452\s/',$resp) == 1);
              fclose($sock);
              return $vaild;
          }else{
              return false;
          }
      }
      private function openSMTPSocket($hostname)
      {
          $hosts = $this->getMX($hostname);
          foreach($hosts as $host => $weight)
          {
              if($sock = @fsockopen($host,self::SMTP_PORT,$errno,$errstr,self::CONN_TIMEOUT))
              {
                  stream_set_timeout($sock,self::READ_TIMEOUT);
                  return $sock;
              }
          }
      }
      private function getMX($hostname)
      {
          $host = array();
          $weights = array();
          getmxrr($hostname,$hosts,$weights);
 
          $results = array();
          foreach($hosts as $i => $host)
          {
              $results[$host] = $weights[$i];
          }
          arsort($results,SORT_NUMERIC);
          $results[$hostname] = 0;
          return $results;
      }
      private function smtpSend($sock,$data)
      {
          fwrite($sock,"$data\r\n");
          return fgets($sock,1024);
      }
     
      //正则匹配
      private function getParts()
      {
          //$emailRegex = '/\w+([-+.]\w+)*@\w+([-.]\w)*\.\w+([-.]\w+)*/x';
          $emailRegex = <<<__REGEX__
          /(?P<user>
              \w+([-+.]\w+)*
           )@(?P<host>
               \w+([-.]\w)*\.\w+([-.]\w+)*
           )/x
  __REGEX__;
          return (preg_match($emailRegex,$this->email,$matches))? $matches : null ;
      }
 
  }
  $emailLinker_class = new EmailLinker('790896688@qq.com');
  echo $emailLinker_class->link(); 
?>

您可能感兴趣的文章:
php邮箱检测的正则表达式一例
php正则验证email邮箱及抽取内容中email的例子
php常用正则表达式(日期 电话 中文 邮箱等)
php写的用来检测手机邮箱用户名的类
验证用户输入的邮箱有效性与正确性的php代码

您可能感兴趣的文章:
php邮箱检测的正则表达式一例
php正则验证email邮箱及抽取内容中email的例子
php常用正则表达式(日期 电话 中文 邮箱等)
php邮箱验证的正则表达式代码
PHP邮箱地址正确性验证示例
PHP、Mysql、jQuery找回密码的实现代码
php正则验证邮箱的函数
常用js验证代码大全(Email、手机号码、身份证号码、文件类型等)
PHP验证邮箱的正确与有效性(示例)
php邮箱验证类及正则匹配域名验证的实例代码

关键词: PHP邮箱验证  php邮箱正则   
[关闭]
~ ~