0

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.

UPDATE:

Code updated based on first answer, but still no results.

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.

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 <videkivendeghazak@gmail.com>'
            );
            $to = $user->user_email;
            $subject = "Fiókod hamarosan törlésre kerül";
            $body = "<p style='font-size: 16px; margin-bottom: 20px;'>Kedves Szállásadó!</p><p style='font-size:14px; margin-bottom: 20px;'>A <a href='https://videkivendeghazak.hu'>Vidéki Vendégházak oldalon</a> felhasználói fiókod 30 nap múlva <b>automatikus törlésre kerül minden általad létrehozottt tartalommal együtt.</b></p><p style='font-size:14px; margin-bottom: 20px;'>Amennyiben nem szeretnéd, hogy fiókod törlésre kerüljön, úgy a következő 30 napon belül lépj be felhasználói fiókodba <a href='https://videkivendeghazak.hu/fiokom'>ezen a linken keresztül.</a></p><p style='font-size:14px;'>Üdvözlettel: a Vidéki Vendégházak csapata</p>";

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

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

  • Please [debug your code](https://stackoverflow.com/questions/5710665/how-to-debug-php-code), or your question will end like [wp_mail() is not working for me in wordpress](https://stackoverflow.com/questions/44694320/wp-mail-is-not-working-for-me-in-wordpress) – Luuk Nov 26 '22 at 11:26

1 Answers1

0

The problem here is your return; in else. You're telling the code to return if the condition does not match then return something within foreach. which means, if the first iteration will not match the if statement it will return and the foreach will break and your function will return nothing.

In general, we shouldn't return anything within the foreach, we can store the value in a variable then use a break then outside the foreach we'll return the stored variable value.

If you've to test this problem then try this similar code on an online php compiler

<?php
$oldusers = [100,101,102,104, 4210];
foreach ($oldusers as $olduser) {    
  if($olduser == 4210 ) {  
     echo 'found it';
  }else { 
    return;
  }
}
?>

If you'll run that code, you'll not see found it as output but if you'll just remove return; you'll see it.

So in your code you just need to remove that else condition and return and your issue should be solved if all the above query and other things are correct.

Vijay Hardaha
  • 2,411
  • 1
  • 7
  • 16
  • Sadly removing else condition with return didn't solve the problem, mail still not going out. I have tested the query on frontend og the site and I go my email in the $to variable there, so I assume the query is good, no idea what is the problem. I have jsut updated the code based on your answer. – Attila Sotus Nov 26 '22 at 11:49