PHPMailer调试消息 [英] PHPMailer Debug messages

查看:0
本文介绍了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时,会将客户端和服务器响应记录到屏幕上。

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

推荐答案

是。您可以注入一个闭包来捕获调试输出并将其写入数据库。有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)."'");
    }
};

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

这篇关于PHPMailer调试消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆