WP_MAIL在TRANSITION_COMMENT_STATUS中不起作用

时间:2018-06-27 作者:Sagive

我需要发送一个访问者的通知,该访问者的评论已经发布
我创建此函数就是为了:

/*************************************************
** COMMENT APPROVED HOOK
*************************************************/
add_action(\'transition_comment_status\', \'siteit_comment_approved\', 10, 3);
function siteit_comment_approved($new_status, $old_status, $comment) {

    if($old_status != $new_status) {
        if($new_status == \'approved\') {

            if(is_email($comment->comment_author_email)) {

                $data[\'pid\']    = $comment->comment_post_ID;
                $data[\'ptitle\'] = get_the_title( $data[\'pid\'] );
                $data[\'plink\']  = get_permalink( $data[\'pid\'] );
                $data[\'name\']   = $comment->comment_author;
                $data[\'email\']  = $comment->comment_author_email;
                $data[\'date\']   = $comment->comment_date;

                $subject    = of_get_option(THEME_PREF.\'ntf_cmnt_approved_esubject\');
                $message    = of_get_option(THEME_PREF.\'ntf_cmnt_approved_ebody\');

                if( $subject && $message ) {

                    $subject = str_replace(\'[author_name]\', $data[\'name\'], $subject);
                    $subject = str_replace(\'[post_title]\', $data[\'ptitle\'], $subject);

                    $message = str_replace(\'[author_name]\', $data[\'name\'], $message);
                    $message = str_replace(\'[author_email]\', $data[\'email\'], $message);
                    $message = str_replace(\'[comment_date]\', $data[\'date\'], $message);
                    $message = str_replace(\'[post_title]\', $data[\'ptitle\'], $message);
                    $message = str_replace(\'[post_link]\', $data[\'plink\'], $message);

                    $headers[] = \'Content-Type: text/html; charset=UTF-8\';
                    $headers[] = \'From: \'.of_get_option(THEME_PREF.\'define_sitename_meta\').\' <\'.of_get_option(THEME_PREF.\'define_admin_email\').\'>\';



                    $mailResponse = wp_mail( $data[\'email\'], $subject, $message, $headers );

                    // if( is_user_logged_in() ) {
                    //     echo \'<pre style="z-index: 999; position: relative;">Mail DATA:\'."<br>";
                    //     print_r($data);
                    //     echo \'</pre>\'."<br>";

                    //     echo \'<pre style="z-index: 999; position: relative;">subject:\'."<br>";
                    //     print_r($subject);
                    //     echo \'</pre>\'."<br>";

                    //     echo \'<pre style="z-index: 999; position: relative;">message:\'."<br>";
                    //     print_r($message);
                    //     echo \'</pre>\'."<br>";

                    //     echo \'<pre style="z-index: 999; position: relative;">message:\'."<br>";
                    //     print_r($headers);
                    //     echo \'</pre>\'."<br>";

                    //     echo \'<pre style="z-index: 999; position: relative;">MailResponse:\'."<br>";
                    //     print_r($mailResponse);
                    //     echo \'</pre>\'."<br>";
                    // }
                }

            }


        }
    }

}
在代码下面,我添加了一些打印以检查数据是否正确
所有print_r\'s返回正确的数据。“只是”$mailResponse
不返回任何内容。

有什么想法吗?

1 个回复
最合适的回答,由SO网友:Sagive 整理而成

* UPDATE *
我已将此函数置于上一个代码之前

// define the wp_mail_failed callback 
function action_wp_mail_failed($wp_error) {
    return error_log(print_r($wp_error, true));
}

// add the action 
add_action(\'wp_mail_failed\', \'action_wp_mail_failed\', 10, 1);
这有助于我跟踪问题(使用错误的标题)
我与大家分享这一点,希望这对调试时的任何人都有帮助wp_mail()

结束

相关推荐

Simple comments spam solution

我想实施一个解决方案,打击评论垃圾邮件和方法很简单。我想,每一个评论,有“网站”字段填写被自动标记为垃圾邮件。此外,在提交之前或之后,应显示一条小警告,告知用户如果使用“网站”字段,其邮件将被标记为垃圾邮件<;这不是强制性的实现这一目标的最佳方法是什么?