【问题标题】:php email smpt validation error send of 34 bytes failedphp email smtp 验证错误 end of 34 bytes 失败
【发布时间】:2018-11-12 14:11:27
【问题描述】:

我想通过 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 ****";

【问题讨论】:

  • 为什么不使用内置于电子邮件库的codeigniter?

标签: php codeigniter validation email


【解决方案1】:

此方法检查邮件是否真实存在

你不能那样做,对方也不想再和你说话,因为你试过了。

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

他们最终会得到一个有效用户列表来发送垃圾邮件。

从那时起,邮件服务器变得不那么开放和“健谈”,并且不会响应这些查询,实际上会在多次失败后禁止您的地址。

您所能做的就是发送实际电子邮件并在无法送达时处理退回邮件。如果您坚持“检查”电子邮件地址以查看它们是否正确,您会发现您很快就会拥有一个列入黑名单的 IP 地址。

【讨论】:

  • 同意。大多数人只是验证电子邮件以查看其语法是否有效,而不是我们无法控制。
猜你喜欢
  • 2022-08-15
  • 1970-01-01
  • 2016-07-14
  • 2013-08-07
  • 2016-05-07
  • 2016-07-30
  • 2014-07-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多