PHP 8, AJAX mail form to function.php doesn’t work

The question:

I have code that send message to email, it works at PHP 7.2, but after we changed version to PHP 8 it didn’t works. Maybe someone have ideas whats wrong?

Function.php

function true_add_ajaxform(){

$multiple_to_recipients = array(
    '[email protected]',
);

add_filter( 'wp_mail_content_type', 'set_html_content_type' );

$message = 'text: '.$_POST['page']."n";

$message .= 'text: '.$_POST['name']."n";

$message .= 'text: '.$_POST['phone']."n";

wp_mail( $multiple_to_recipients, $_POST['nameForm'], $message);

remove_filter( 'wp_mail_content_type', 'set_html_content_type' );

function set_html_content_type() {
    return 'text/html';
}

}
 
add_action('wp_ajax_ajaxform', 'true_add_ajaxform'); 
add_action('wp_ajax_nopriv_ajaxform', 'true_add_ajaxform');

JS

var formType = '';
var page = document.location.href;
if (thParent.find('#actHidd .textAr').length > 0) {
    formType = 'author=' + thParent.find('#author').val() + '&comment=' + thParent.find('#actHidd .textAr').val() + '&idComment=' + jQuery('#commentCount').val() + '&rating=' + jQuery(".starRating input[type='radio']:checked").val() + '&action=ajaxcomments';
} else {
    formType = 'name=' + thParent.find('.callName').val() + '&phone=' + thParent.find('.callPhone').val() + '&nameForm=' + thParent.find('.callTitle').text() + '&page=' + page + '&action=ajaxform';
}

jQuery.ajax({
    type: 'POST',
    url: '/wp-admin/admin-ajax.php',
    data: formType,

    success: function(newComment) {
        jQuery('#submit').addClass('clack');
        // console.log('clack');
        console.log(newComment);
        //                    alert(newComment);


        if (jQuery(this).is('#ericSend')) {
            jQuery('#innerEric').css({
                "display": "none"
            });
            thParent.find('.final').css({
                "display": "block"
            });
        } else if (jQuery(this).is('#sendForm')) {
            jQuery('#actHidd').css({
                "display": "none"
            });
            thParent.find('.final').css({
                "display": "block"
            });
        }
        thParent.find('.hiddForm').css({
            "display": "none"
        });
        thParent.find('.final').css({
            "display": "block"
        });

    }
});

When we use debug see this

PHP Fatal error:  Uncaught TypeError: call_user_func_array(): Argument #1 ($function) must be a valid callback, function "set_html_content_type" not found or invalid function name in /public_html/wp-includes/class-wp-hook.php:292
Stack trace:
#0 /public_html/wp-includes/plugin.php(212): WP_Hook->apply_filters('text/plain', Array)
#1 /public_html/wp-includes/pluggable.php(469): apply_filters('wp_mail_content...', 'text/plain')
#2 /public_html/wp-content/themes/honestRepair/functions.php(1234): wp_mail(Array, 'xD0x9ExD1x81xD1x82xD0xB0xD0xB2xD0xB8xD1x82xD1...', 'xD0xA1xD1x82xD1x80xD0xB0xD0xBDxD0xB8xD1x86xD0...')
#3 /public_html/wp-includes/class-wp-hook.php(292): true_add_ajaxform('')
#4 /public_html/wp-includes/class-wp-hook.php(316): WP_Hook->apply_filters('', Array)
#5 /public_html/wp-includes/plugin.php(484): WP_Hook->do_action(Array)
#6 /public_html/wp-admin/admin-ajax.php(187): do_action('wp_ajax_ajaxfor...')
#7 {main}
  thrown in /home/p/progress55/public_html/wp-includes/class-wp-hook.php on line 292

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

This is the cause of your problem:

function true_add_ajaxform(){
    ...
    function set_html_content_type() {
        return 'text/html';
    }

}

You shouldn’t nest function definitions! Move it out of the function like this:

function true_add_ajaxform(){
    ...
}

function set_html_content_type() {
    return 'text/html';
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment