2016-10-21 6 views
1

メールを送信できなかった場合、レコードを挿入する簡単なテーブルlogを作成しました。完全なSMTPエラーログを取得できませんPHPメーラー

mysql> describe log; 
+--------+-----------+------+-----+---------------------+-----------------------------+ 
| Field | Type  | Null | Key | Default    | Extra      | 
+--------+-----------+------+-----+---------------------+-----------------------------+ 
| id_log | int(11) | NO | PRI | NULL    | auto_increment    | 
| error | text  | YES |  | NULL    |        | 
| time | timestamp | NO |  | 0000-00-00 00:00:00 | on update CURRENT_TIMESTAMP | 
+--------+-----------+------+-----+---------------------+-----------------------------+ 

そして、私のPHP側では、エラーログがこのようなものです:

foreach ($arrayWithMails as $key => $value) { 
     $mail->addAddress($value); 
     if (!$mail->send()) 
     { 
      put('message', $mail->ErrorInfo); //store in session 
//   echo $mail->ErrorInfo; 

     } 
     else { 
      /* Add email from array in session to be displayed at main page */ 
      put('emailAddresses', $arrayWithMails); //Store in session 

     } 
     $mail->ClearAllRecipients(); 
    } 
$log->logError($_SESSION['message']); 

それは、パラメータとして文字列を渡して、DBに挿入する私のクラスのメソッドを呼び出します。

しかし、すべての私のログは次のように探しています:

mysql> select * from log; 
+--------+------------------------------------------------------------------------------------+---------------------+ 
| id_log | error                    | time    | 
+--------+------------------------------------------------------------------------------------+---------------------+ 
|  1 | SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting | 2016-10-21 15:08:30 | 
|  2 | 1                     | 2016-10-21 15:12:04 | 
|  3 | 1                     | 2016-10-21 15:13:42 | 
|  4 | SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting | 2016-10-21 15:14:14 | 
|  5 | SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting | 2016-10-21 15:17:02 | 
|  6 | SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting | 2016-10-21 15:18:22 | 
|  7 | SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting | 2016-10-21 15:28:02 | 
|  8 | SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting | 2016-10-21 15:28:53 | 
+--------+------------------------------------------------------------------------------------+---------------------+ 

そして、私のSMTPのデバッグは4に設定されています。 2でも同様の結果が試されました。

f.e.私は$mail->ErrorInfoの基本的なエコーを行います。完全なログにはエラーがありますが、DBにその完全なログ情報を保存したいときは、私だけが得ます: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting $ _SESSION varにそのエラーを保存しようとすると同じことが起こります。

どのように完全なログを保存するか考えていますか? ありがとう!

答えて

1

ErrorInfoには、完全なデバッグログではなく、エラーメッセージのみが含まれています。デバッグ出力はちょうどそれです - デフォルトでは標準出力にダンプされ、保持されません。

これをキャプチャするには、Debugoutputプロパティを参照する必要があります。PHPMailerの最近のバージョンでは、デバッグ出力を収集してDBに挿入できる呼び出し可能コードに設定できます。例えば

//Before sending 
$debuglog = ''; 
$mail->Debugoutput = function($str, $level) use ($debuglog) { 
    $debuglog .= $str; 
}; 

//... after calling send() 
$log->logError($mail->Errorinfo . ' ' . $debuglog); 
+0

こんにちは、ありがとうございました。私は実際に私のコードであなたのソリューションを実装する方法は実際にはわかりません。私は私の質問を編集しました。 – fugitive

0

これが私の答えをカバーしています。私はPHPMailerを拡張しているので、例外をスローすることになっているので、このようにして例外メッセージを正しく取得できます。

foreach ($array as $key => $value) { 
    $mail->addAddress($value); 
    try 
    { 
     if (!$mail->send()) { 
      put('message', $mail->ErrorInfo); // Storing for frontend, that error exist, for detailed info - log is in DB 

     } 
     else { 
      /* Add email from array in session to be displayed at main page */ 
      put('emailAddresses', $array); 
     } 
    } catch (phpmailerException $exc) 
    { 
     $debugError = $exc->errorMessage(); 
    } 

    $mail->ClearAllRecipients(); 
} 
$log->logError($debugError); 
+0

このアプローチでは、例外のメッセージがErrorInfoのメッセージと同じになるため、それ以上は得られません。 – Synchro

+0

@シンクロノップメイト、私はこの出力を得ます: 'select * from log; + -------- + ------------------------------------- + - -------------------- + | id_log |エラー|時間| + -------- + ------------------------------------- + - -------------------- + | 77 | SMTPエラー:認証できませんでした。 | 2016-10-21 18:51:42 | + -------- + ------------------------------------- + - -------------------- + ' – fugitive

+0

しかし、それはあなたが求めていたものですか? – Synchro

関連する問題