3

如果 strlen $mail->SMTPDebug 大于 10,我想回显“失败”。但我不知道如何使用 $mail->SMTPDebug 作为字符串。函数中的以下行启用调试。

$mail->SMTPDebug  = 1;     

但不能在我的函数中使用我的 if 语句。我怎样才能做到这一点 ?

function mailsender($val,$yollayan,$sifresi,$name,$subject,$message) {

$mail = new PHPMailer();  
$mail->IsSMTP();               
$mail->SMTPDebug  = 1;          
$mail->SMTPAuth = true;         
$mail->SMTPSecure = "tls";      

$mail->Username   = $yollayan;
$mail->Password   = $sifresi;

$mail->Host = "smtp.live.com";  
$mail->Port = "587";  


$mail->From = $yollayan;
$mail->Fromname = $name;
$mail->name = $name;


$mail->Subject = $subject;  
$mail->Body = $message;  
$mail->AddAddress($val);   
$mail->send();

}
4

2 回答 2

3

我猜你想对调试日志做点什么?class.smtp.php(第 114-126 行)内容如下:

/**
 * How to handle debug output.
 * Options:
 * * `echo` Output plain-text as-is, appropriate for CLI
 * * `html` Output escaped, line breaks converted to `<br>`, appropriate for browser output
 * * `error_log` Output to error log as configured in php.ini
 *
 * Alternatively, you can provide a callable expecting two params: a message string and the debug level:
 * <code>
 * $smtp->Debugoutput = function($str, $level) {echo "debug level $level; message: $str";};
 * </code>
 * @type string|callable
 */

所以在你的代码中,简单地

$mail->Debugoutput = function($str, $level) { do_something_with($str); };

于 2014-10-11T07:38:48.533 回答
3

您必须创建子类PHPMailer并重新定义edebug方法以将输出存储在变量中:

class MyPHPMailer extends PHPMailer {

  public $DbgOut = '';

  private function edebug($str) {
    $this->DbgOut .= $str;
  }

}

你这样称呼它:

function mailsender($val, $yollayan, $sifresi, $name, $subject, $message) {

  $mail = new MyPHPMailer();  
  $mail->IsSMTP();               
  $mail->SMTPDebug  = 1;          
  $mail->SMTPAuth = true;         
  $mail->SMTPSecure = "tls";      

  $mail->Username   = $yollayan;
  $mail->Password   = $sifresi;

  $mail->Host = "smtp.live.com";  
  $mail->Port = "587";  


  $mail->From = $yollayan;
  $mail->Fromname = $name;
  $mail->name = $name;


  $mail->Subject = $subject;  
  $mail->Body = $message;  
  $mail->AddAddress($val); 
  $mail->send();

  if(strlen($mail->DbgOut) > 10)
    echo 'Failed'.PHP_EOL;

}
于 2012-07-23T01:22:05.990 回答