WP-Cron not sending email, Help

Hi There,

How are we all?

I have a website, that’s powered by WordPress and have put the following code in its functions.php file. The code needs to send out emails on a certain date. I have created the following code:

// Send email using wp-cron
add_action( 'wp', 'send_reminder_email_daily' );
function send_reminder_email_daily() {
  if ( ! wp_next_scheduled( 'send_reminder_email') ) {
    wp_schedule_event( time(), 'daily', 'send_reminder_email');
  } 
}
add_action( 'send_reminder_email', 'send_reminder_email_check' );

function send_reminder_email_check() {

  $today = date('Y-m-d');

  $reminders = $wpdb->get_results( 'SELECT * FROM table_name WHERE (plan_end_date >= "'. $today .'") ');

  if ($reminders) {

    foreach($reminders as $reminder);

   // echo $reminder->email;

    $date1 = time();

    $date2 = strtotime($reminder->plan_end_date);

    $datediff = $date2 - $date1;

    $daysremaining = floor($datediff / (60 * 60 * 24));

    //echo $daysremaining;

                if ($daysremaining <= 14 || $daysremaining <= 7 || $daysremaining <= 3 || $daysremaining <= 1) {

                    $email = $reminder->email;
                    $to = $email;
                    $subject = 'Subscription renewal';
                    $body = '';
                    $headers = array('Content-Type: text/html; charset=UTF-8','Reply-To: ' . $email);
                    
                    wp_mail($to, $subject, $body, $headers);

                      $emailSent = true;
            }

        } else {
                 echo "Error";
     } 
}

*tablename is not the real name for the table, changed for security reasons.

I’ve tested the email part of this program and it works, however there seems to be a problem as when I run the wp_cron job no email is sent, no error is produced either.

Is there something I’m missing?

Any help would be great.

Thanks

It might help if you show us what your cron job code looks like.

Was the email body left blank just for the post? I wondered if it wouldn’t send a blank email.

This line is strange, though probably not related to the issue at hand

if ($daysremaining <= 14 || $daysremaining <= 7 || $daysremaining <= 3 || $daysremaining <= 1) {

Surely you only need the first check?

Hi,

I was able to sort this out and manage to get it firing.

The code above is what I wanted executed on a daily basis. Managed to get this bit working. :smiley:

Thank you.

Hi droopsnoot,

You’re quite right.

Will remove the other 3 checks.

Improving the code is next on my list of things to do as I think parts of the code are quite bloaty. :).

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.