如何使用$ mail-> SMTPDebug作为字符串?

时间:2012-07-23 01:03:07

标签: php

如果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();

}

2 个答案:

答案 0 :(得分: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;

}

答案 1 :(得分: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-&gt; Debugoutput = function($ str,$ level){do_something_with($ str); };