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