PHPMailer调试消息

时间:2018-04-25 10:33:32

标签: phpmailer

我正在使用PHPMailer,我想在我的数据库中保存一些调试信息。

以下代码显示了在使用如下SMTP服务器时如何保存调试信息:

mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->SMTPDebug = 2; //Alternative to above constant
$mail->isSMTP();  // tell the class to use SMTP
$mail->SMTPAuth   = true;                // enable SMTP authentication
$mail->Port       = 25;                  // set the SMTP port
$mail->Host       = "mail.yourhost.com"; // SMTP server
$mail->Username   = "name@yourhost.com"; // SMTP account username
$mail->Password   = "your password";     // SMTP account password

根据PHPMailer文档,SMTPDebug有4个级别。

SMTP::DEBUG_OFF (0): Disable debugging (you can also leave this out completely, 0 is the default).
SMTP::DEBUG_CLIENT (1): Output messages sent by the client.
SMTP::DEBUG_SERVER (2): as 1, plus responses received from the server (this is the most useful setting).
SMTP::DEBUG_CONNECTION (3): as 2, plus more information about the initial connection - this level can help diagnose STARTTLS failures.
SMTP::DEBUG_LOWLEVEL (4): as 3, plus even lower-level information, very verbose, don't use for debugging SMTP, only low-level problems.

当我使用级别2时,我将客户端和服务器响应记录到屏幕上。

有没有办法只记录服务器响应?我也不想保存客户端请求,因为它可能有一些敏感数据(电子邮件内容自己发送)。

1 个答案:

答案 0 :(得分:1)

是。您可以注入一个捕获调试输出的闭包并将其写入您的数据库。有an example in the docs

可能是这样的(包括过滤掉客户端消息):

$mail->Debugoutput = function($str, $level) use ($db) {
    if (strpos($str, 'CLIENT -> SERVER') === false) {
        mysqli_query($db, "INSERT INTO maildebug SET level = '".mysqli_real_escape_string($db, $level)."', message = '".mysqli_real_escape_string($db, $str)."'");
    }
};

一个稍微整洁的选项是将所有调试输出累积到临时变量中,并且只有在出现问题时才将其写入数据库。