dongxian3574 2013-11-12 14:27
浏览 403
已采纳

使用PHP EHLO进行SMTP验证

I'm trying to do email verification and I can't seem to get it going. At present it would make the connection to the domain but after that it seems to time out. Does anyone know why this is? It seems to be hanging here:

$res=fgets($connection, "EHLO $mydomain
");

Would anyone be able to inform me as to why this is? My code is below:

if(isset($_POST['email']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))

{

 // Could get this from the php ini? 
  $from="xxxx@xxxx-int.net";
 $to=$_POST['email'];
 $subject="Test";
 $message="Testing";
 list($me,$mydomain) = split("@",$from); 

 // Now look up the mail exchangers for the recipient 
 list($user,$domain) = split("@",$to,2); 
 if(getmxrr($domain,$mx,$weight) == 0)  return FALSE; 

 // Try them in order of lowest weight first 
 array_multisort($mx,$weight); 
 $success=0; 

 foreach($mx as $host) { 
  // Open an SMTP connection 
  $connection = fsockopen ($host, 25, $errno, $errstr, 1); 
  if (!$connection) 
    continue; 
  $res=fgets($connection); 
echo $res;
  if(substr($res,0,3) != "220") echo $res;

  // Introduce ourselves 
  fputs($connection, "EHLO $mydomain
"); 
  $res=fgets($connection); 
echo $res;
  if(substr($res,0,3) != "250") echo $res; 

  // Envelope from 
  fputs($connection, "MAIL FROM: $from
"); 
  $res=fgets($connection); 
echo $res; 
  if(substr($res,0,3) != "250") echo $res; 

  // Envelope to 
  fputs($connection, "RCPT TO: $to
"); 
  $res=fgets($connection); 
echo $res; 
  if(substr($res,0,3) != "250") echo $res;

  // The message 
  fputs($connection, "DATA
"); 
  $res=fgets($connection); 
echo $res; 
  if(substr($res,0,3) != "354") echo $res;

  // Send To:, From:, Subject:, other headers, blank line, message, and finish 
  // with a period on its own line. 
  fputs($connection, "To: $to
From: $from
Subject: $subject
$message
.
"); 
  $res=fgets($connection); 
echo $res; 
  if(substr($res,0,3) != "250") echo $res;

  // Say bye bye 
  fputs($connection,"QUIT
"); 
  $res=fgets($connection); 
echo $res; 
  if(substr($res,0,3) != "221") echo $res;

  // It worked! So break out of the loop which tries all the mail exchangers. 
  $success=1; 
  break; 
 } 
 // Debug for if we fall over - uncomment as desired 
 // print $success?"Mail sent":"Failure: $res
"; 
 if($connection) { 
  if($success==0) fputs($connection, "QUIT
"); 
  fclose ($connection); 
 } 
 return $success?TRUE:FALSE; 
}
  • 写回答

1条回答 默认 最新

  • dongxian6653 2013-11-12 15:05
    关注
      fputs($connection, "EHLO $mydomain
    "); 
    

    you only send line feeds at the end, but smtp rfc requires CRLF. This causes your script to "hang" because the email server will not respond to your invalid line ending. you'd have to change this to

      fputs($connection, "EHLO $mydomain
    ");
    

    etc.

    Please note that email validation will NOT WORK in many cases, even if you get your script to talk RFC compliant SMTP. Many servers simply accept (and later bounce) all messages to invalid recipients. Others do recipient verification at the data stage instead of rcpt to (exchange 2013..sigh) etc etc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计