php email smpt验证错误发送34个字节失败

时间:2018-06-02 19:11:45

标签: php codeigniter validation email

我想通过smtp验证电子邮件地址是否有效

我无法在codeigniter中验证电子邮件。

这是错误

  

fwrite():发送34个字节失败,错误为errno = 10054已存在   连接被远程主机强行关闭。

此方法检查电子邮件是否确实存在

我在这段代码的不同行中有两个相同的错误

function isValidEmail($email){
   $result=false;

   # BASIC CHECK FOR EMAIL PATTERN WITH REGULAR EXPRESSION
   if(!preg_match('/^[_A-z0-9-]+((\.|\+)[_A-z0-9-]+)*@[A-z0-9-]+(\.[A-z0-9-]+)*(\.[A-z]{2,4})$/',$email))
       return $result;

   # MX RECORD CHECK
     list($name, $domain)=explode('@',$email);

   if(!checkdnsrr($domain,'MX'))
      return $result;

   # SMTP QUERY CHECK
   $max_conn_time = 30;
   $sock='';
   $port = 25;
   $max_read_time = 5;
   $users=$name;

   # retrieve SMTP Server via MX query on domain
   $hosts = array();
   $mxweights = array();
   getmxrr($domain, $hosts, $mxweights);
   $mxs = array_combine($hosts, $mxweights);
   asort($mxs, SORT_NUMERIC);

   #last fallback is the original domain
   $mxs[$domain] = 100;
   $timeout = $max_conn_time / count($mxs);

   # try each host
   while(list($host) = each($mxs)) {
    #connect to SMTP server
    if($sock = fsockopen($host, $port, $errno, $errstr, (float) $timeout)){
      stream_set_timeout($sock, $max_read_time);
      break;
    }
   } 

   # did we get a TCP socket
   if($sock) {
      $reply = fread($sock, 2082);
      preg_match('/^([0-9]{3}) /ims', $reply, $matches);
      $code = isset($matches[1]) ? $matches[1] : '';

      if($code != '220') {
        # MTA gave an error...
        return $result;
      }

      # initiate smtp conversation
      $msg="HELO ".$domain;
      fwrite($sock, $msg."\r\n");
      $reply = fread($sock, 2082);

      # tell of sender
      $msg="MAIL FROM: <".$name.'@'.$domain.">";
      fwrite($sock, $msg."\r\n");
      $reply = fread($sock, 2082);


      #ask of recepient
      $msg="RCPT TO: <".$name.'@'.$domain.">";
      fwrite($sock, $msg."\r\n");
      $reply = fread($sock, 2082);

      #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
        $result=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
        $result=true;
      }else{
        $result=false;
      }

      #quit smtp connection
      $msg="quit";
      fwrite($sock, $msg."\r\n");

      # close socket
      fclose($sock);

   }

   return $result;


}

$email='test1221s@gmail.com';

if(isValidEmail($email))
  echo "**** EMAIL EXISTS ****";
else
  echo "**** NOT A VALID EMAIL ****";

1 个答案:

答案 0 :(得分:0)

  

此方法检查电子邮件是否确实存在

你不能这样做,而另一方也不想和你说话,因为你试过了。

过去,抓取工具会通过询问服务器是否存在user@domain.com来收集电子邮件地址。然后是user1@domain.com。然后是user2@domain.com等。

他们最终会得到一份有效的垃圾邮件用户列表。

从那以后,邮件服务器变得不那么开放了,并且#34; chatty&#34;并且不会回答这些问题,并且在发生一些失败后实际上会禁止你的地址。

您所能做的就是发送实际的电子邮件并处理退回(如果它无法送达)。如果你坚持&#34;检查&#34;电子邮件地址以确定它们是否良好,您会发现很快就会有一个列入黑名单的IP地址。