【问题标题】:How to display WP_Mail errors? [closed]如何显示 WP_Mail 错误? [关闭]
【发布时间】:2018-06-06 07:58:58
【问题描述】:

如何让 PHP 在 else 语句之后显示确切的错误,而不是回显文本?

这是我使用的代码,我想知道发送邮件后到底出了什么问题:

function deliver_mail() {

// if the submit button is clicked, send the email
if ( isset( $_POST['cf-submitted'] ) ) {

    // sanitize form values
    $name    = sanitize_text_field( $_POST["cf-name"] );
    $email   = sanitize_email( $_POST["cf-email"] );
    $subject = sanitize_text_field( $_POST["cf-subject"] );
    $message = esc_textarea( $_POST["cf-message"] );

    // get the blog administrator's email address
    $to = get_option( 'admin_email' );

    $headers = "From: $name <$email>" . "\r\n";

    // If email has been process for sending, display a success message
    if ( wp_mail( $to, $subject, $message, $headers ) ) {
        echo '<div>';
        echo '<p>You have sent the mail!</p>';
        echo '</div>';
    } else {
        echo 'error occurred';
    }
}

【问题讨论】:

  • 错误信息在哪里?

标签: php wordpress plugins


【解决方案1】:

看看这个:

https://developer.wordpress.org/reference/hooks/wp_mail_failed/

使用这种简单的方法调试 wp_mail() 会容易得多。 默认情况下,它将显示比 Wordpress 更有用的错误消息(原始 phpmailer 错误)。 只需添加此函数即可显示真正的 wp_mail() 错误。 但仅用于调试。

`

   // show wp_mail() errors
    add_action( 'wp_mail_failed', 'onMailError', 10, 1 );
    function onMailError( $wp_error ) {
        echo "<pre>";
        print_r($wp_error);
        echo "</pre>";
    }       

`

【讨论】:

  • 为什么不能在生产中使用?我想知道某事是否出错了
  • 作者可能的意思是使用print_r($wp_error)可以显示敏感数据。使用此挂钩显示“出现问题。再试一次”之类的用户友好通知没有任何问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多