0

I need to send a notification for a visitor whose comment has been published.
I created this function to do it:

/*************************************************
** 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>";
                    // }
                }

            }


        }
    }

}

Down the code I added some prints to check if the data is correct.
All the print_r's returns the correct data. Just the "$mailResponse"
returns nothing.

Ideas anyone?

0

* UPDATE *
I have placed this function before the prev code

// 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);

Which helped me track the problem (using headers wrong).
I share this in the hopes this helpes anyone while debugging wp_mail()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.