如何显示WP_Mail错误?

时间:2018-06-06 07:58:58

标签: php wordpress plugins

如何让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';
    }
}

1 个答案:

答案 0 :(得分: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>";
    }       

`

相关问题