设为首页 收藏本站
查看: 757|回复: 0

[经验分享] php验证邮件地址的有效性

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-1-29 08:56:33 | 显示全部楼层 |阅读模式
<?php
/**
* Validate Email Addresses Via SMTP
* This queries the SMTP server to see if the email address isaccepted.
* @author 臭三八
* @version 1.0
*/
class SMTP_validateEmail {
      /**
       * PHP Socket resource to remote MTA
       * @var resource $sock
       */
      var $sock;
      
      /**
       * Current User being validated
       */
      var $user;
      /**
       * Current domain where user is being validated
       */
      var $domain;
      /**
       * List of domains to validate users on
       */
      var $domains;
      /**
       * SMTP Port
       */
      var $port = 25;
      /**
       * Maximum Connection Time to wait for connectionestablishment per MTA
       */
      var $max_conn_time = 30;
      /**
       * Maximum time to read from socket before giving up
       */
      var $max_read_time = 5;
      
      /**
       * username of sender
       */
      var $from_user = 'user';
      /**
       * Host Name of sender
       */
      var $from_domain = 'localhost';
      
      /**
       * Nameservers to use when make DNS query for MX entries
       * @var Array $nameservers
       */
      var $nameservers = array(
          '192.168.0.1'
      );
      
      var $debug = false;
      
      /**
       * Initializes the Class
       * @return SMTP_validateEmail Instance
       * @param $email Array[optional] List of Emails toValidate
       * @param $sender String[optional] Email of validator
       */
      function SMTP_validateEmail($emails = false, $sender =false)
      {
          if ($emails) {
               $this->setEmails($emails);
          }
          if ($sender) {
               $this->setSenderEmail($sender);
          }
      }
      
      function _parseEmail($email)
      {
          $parts = explode('@', $email);
          $domain = array_pop($parts);
          $user= implode('@', $parts);
          return array($user, $domain);
      }
      
      /**
       * Set the Emails to validate
       * @param $emails Array List of Emails
       */
      function setEmails($emails)
      {
          foreach ($emails as $email) {
              list($user, $domain) = $this->_parseEmail($email);
              if (!isset($this->domains[$domain])) {
                    $this->domains[$domain] = array();
              }
              $this->domains[$domain][] = $user;
          }
      }
      
      /**
       * Set the Email of the sender/validator
       * @param $email String
       */
      function setSenderEmail($email)
      {
          $parts = $this->_parseEmail($email);
          $this->from_user = $parts[0];
          $this->from_domain = $parts[1];
      }
      
      /**
      * Validate Email Addresses
      * @param String $emails Emails to validate (recipientemails)
      * @param String $sender Sender's Email
      * @return Array Associative List of Emails and theirvalidation results
      */
      function validate($emails = false, $sender = false)
      {
           
          $results = array();
      
          if ($emails) {
               $this->setEmails($emails);
          }
          if ($sender) {
               $this->setSenderEmail($sender);
          }
      
          // query the MTAs on each Domain
          foreach($this->domains as $domain=>$users) {
      
          $mxs = array();
      
          // current domain being queried
          $this->domain = $domain;
      
          // retrieve SMTP Server via MX query on domain
          list($hosts, $mxweights) = $this->queryMX($domain);
      
          // retrieve MX priorities
          for($n = 0; $n < count($hosts); $n++){
              $mxs[$hosts[$n]] = $mxweights[$n];
          }
          asort($mxs);
      
          // last fallback is the original domain
          $mxs[$this->domain] = 0;
      
          $this->debug(print_r($mxs, 1));
      
          $timeout = $this->max_conn_time;
        
          // try each host
          while(list($host) = each($mxs)) {
              // connect to SMTP server
              $this->debug("try $host:$this->port\n");
              if ($this->sock = fsockopen($host, $this->port, $errno, $errstr, (float) $timeout)) {
                  stream_set_timeout($this->sock, $this->max_read_time);
                   break;
              }
          }
      
          //did we get a TCP socket
          if ($this->sock) {
              $reply = fread($this->sock, 2082);
              $this->debug("<<<\n$reply");
            
              preg_match('/^([0-9]{3}) /ims', $reply,$matches);
              $code = isset($matches[1]) ? $matches[1] : '';
           
              if($code != '220') {
                  // MTA gave an error...
                  foreach($users as $user) {
                   $results[$user.'@'.$domain] = false;
                   }
                   continue;
              }
               
              // say helo
              $this->send("HELO ".$this->from_domain);
              // tell of sender
              $this->send("MAIL FROM: <".$this->from_user.'@'.$this->from_domain.">");
            
              // ask for each recepient on this domain
              foreach($users as $user) {
            
                   // ask of recepient
                   $reply = $this->send("RCPT TO: <".$user.'@'.$domain.">");
                 
                  // get code and msg from response
                   preg_match('/^([0-9]{3}) /ims', $reply,$matches);
                   $code = isset($matches[1]) ? $matches[1] : '';
               
                   if ($code == '250') {
                   // you received 250 so the email address was accepted
                        $results[$user.'@'.$domain] =true;
                   }
                   elseif ($code == '451' || $code == '452') {
                        // you received 451 so the email address was greylisted (or some temporary error occured on the MTA) - so assume is ok
                        $results[$user.'@'.$domain] =true;
                   }
                   else {
                        $results[$user.'@'.$domain] =false;
                   }
            
             }
            
              // reset before quit
              $this->send("RSET");
            
              // quit
              $this->send("quit");
              // close socket
              fclose($this->sock);
              }
          }
          return $results;
      }
      
      
     function send($msg) {
          fwrite($this->sock, $msg."\r\n");
      
          $reply = fread($this->sock, 2082);
      
          $this->debug(">>>\n$msg\n");
          $this->debug("<<<\n$reply");
      
          return $reply;
     }
      
      /**
       * Query DNS server for MX entries
       * @return
       */
     function queryMX($domain) {
          $hosts = array();
          $mxweights = array();
          if (function_exists('getmxrr')) {
          getmxrr($domain, $hosts, $mxweights);
     }
     else {
          // windows, we need Net_DNS
          require_once 'Net/DNS.php';
      
          $resolver = new Net_DNS_Resolver();
          $resolver->debug = $this->debug;
          // nameservers to query
          $resolver->nameservers = $this->nameservers;
          $resp = $resolver->query($domain, 'MX');
              if ($resp) {
                   foreach($resp->answer as $answer) {
                        $hosts[] = $answer->exchange;
                        $mxweights[] = $answer->preference;
                   }
              }
               
          }
          return array($hosts, $mxweights);
      }
      
      /**
       * Simple function to replicate PHP 5 behaviour.http://php.net/microtime
       */
     function microtime_float()
     {
       list($usec, $sec) = explode(" ", microtime());
       return ((float)$usec + (float)$sec);
     }
      
     function debug($str)
     {
          if ($this->debug) {
              echo '<pre>'.htmlentities($str).'</pre>';
          }
     }
}
set_time_limit(0);
  
//清空并关闭输出缓存
ob_end_clean();
flush(); //将输出发送给客户端浏览器
$mail_list = "add.txt";
$fp = fopen($mail_list,'r');
while(!feof($fp)){
  $list[] = trim(fgets($fp));
}


//$email = '395582979@qq.com';
// an optional sender

$sender = 'gaocz@im.ac.cn';
// instantiate the class

$SMTP_Validator = new SMTP_validateEmail();
// turn on debugging if you want to view the SMTP transaction

$SMTP_Validator->debug = true;
// do the validation

foreach($list as $k=>$who){
  if($who != ''){
      $results = $SMTP_Validator->validate(array($who),$sender);
      // view results
      //echo 'ok';
      echo $who.' 是'.($results[$who] ? '有效的' :'无效的')."\n";
      if($results[$who]){
        file_put_contents('result.txt',$who. "\r\n", FILE_APPEND);
      }
      // send email?

      if ($results[$who]) {
        //mail($email, 'Confirm Email', 'Please reply to this email to confirm', 'From:'.$sender."\r\n"); // send email

      }
      else {
        //file_put_contents('result.txt',$who. "\r\n", FILE_APPEND);
        //echo 'The email addresses you entered is not valid';
      }
  }
}



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-41672-1-1.html 上篇帖子: PHP连接access数据库出现的问题及注意事项 下篇帖子: PHP添加SOAP模块
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表