php – wp_mail function not working in user query loop


I’m trying to send an e-mail to every user who is logged in more than a year ago (based on woocommerce meta value from database), but the e-mails do not go out. In my code I’m trying to filter myself for the testing phase, so that only me get the mail, after testing these lines would be deleted (marked in code). We are using SMPT with mailing queue and we are using wp_mail function in other similar codes, where wp_mail working correct.

Query seems fine on site fronted testing, cause I got my email address in $to variable with echo.

Tried to add “ini_set(“display_errors”,1); error_reporting(E_ALL);” to code but got no errors.

Also tried to delete the filter lines (if/else) and set $to to my email manually, neither worked, the mail is not going out.

function my_notifyOldUsers() {

global $wpdb;

$query = $wpdb->prepare("SELECT user_id FROM $wpdb->usermeta WHERE ( meta_key='wc_last_active' AND DATE_ADD(FROM_UNIXTIME(meta_value), INTERVAL 1 DAY) > NOW() )");  //will be 365 DAY and "<" after testing

$oldusers = $wpdb->get_results($query, ARRAY_N);
require_once( ABSPATH.'wp-admin/includes/user.php' );

    foreach ($oldusers as $olduser) {
        $user = get_userdata( $olduser[0] );
        $user_azonosito = $user->ID;        //this line will be deleted after testing
        if( $user_azonosito == 4210 ) {     //this line will be deleted after testing, ID 4210 is myself

            $headers = array(
                'Content-Type: text/html; charset=UTF-8',
                'From: Vidéki Vendégházak <sensitive data removed>'
            $to = $user->user_email;
            $subject = "TESZT";
            $body = "<p style="font-size: 16px; margin-bottom: 20px;">TESZT</p>";

            wp_mail( $to, $subject, $body, $headers );

Attila Sotus 1 year 2022-12-02T11:45:07-05:00 0 Answers 0 views 0

Leave an answer