【问题标题】:How to print SMTP debug in other location?如何在其他位置打印 SMTP 调试?
【发布时间】:2020-01-07 05:27:25
【问题描述】:

我正在尝试显示 PHPMailer SMTP 调试信息。 这没关系如果电子邮件失败但如果成功则发送两封电子邮件。

global $phpmailer;
$send = wp_mail( $to, 'Test Email', $message );

$phpmailer->SMTPDebug = 3;

ob_start();
$send = wp_mail( $to, 'Test Email', $message );
$smtp_debug = ob_get_clean();


echo "<pre>"; print_r( $smtp_debug ); echo "</pre>";

如果我删除第一个 wp_mail()

global $phpmailer;

$phpmailer->SMTPDebug = 3;

ob_start();
$send = wp_mail( $to, 'Test Email', $message );
$smtp_debug = ob_get_clean();


echo "<pre>"; print_r( $smtp_debug ); echo "</pre>";

它给出警告:

PHP 警告:从行中的空值创建默认对象:$phpmailer-&gt;SMTPDebug = 3;

如果我删除第二个 wp_mail()$smtp_debug 将为空。

那么,如何在成功时只发送一封电子邮件,在失败时也抓取错误?

【问题讨论】:

  • 你初始化phpmailer类了吗?这是起点$phpmailer= new PHPMailer(true);,然后是$phpmailer-&gt;SMTPDebug = 3;

标签: php wordpress phpmailer


【解决方案1】:

要控制 PHPMailer 的调试输出,您可以将一个可调用对象注入到 Debugoutput 属性中,如 the source code says

$phpmailer->Debugoutput = function($str, $level) {
    echo "debug level $level; message: $str";
};

这只是一个示例 - 您可以在该闭包中做任何您喜欢的事情 - 写入日志文件、将数据发送到外部 API 等。与使用输出缓冲相比,这是一种更好且更简单的方法。

正如@Akam 所说,您收到的default object 警告意味着$phpmailer 变量为空(例如NULL 或未定义),因此您设置了一个不存在的属性,从而导致警告.

检查$phpmailer 是否包含您认为它应该包含的内容(即追踪它最初创建的位置)并确保在您尝试使用实例之前运行代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-30
    • 2016-02-10
    • 1970-01-01
    相关资源
    最近更新 更多