Basically what you would want is a daily task that sends those reminders, for that you would use WP Cron. I’m not gonna get into the logic of actually building the email (used your code). Here’s a basic setup:
if ( ! wp_next_scheduled( 'quote_reminder_hook' ) ) {
wp_schedule_event( time(), 'daily', 'quote_reminder_hook' );
}
add_action( 'quote_reminder_hook', 'send_quote_reminder' );
function send_quote_reminder() {
$args = array(
'post_type' => 'quote',
);
$loop = new WP_Query($args);
while($loop->have_posts()){
$loop->the_post();
$days = "30";
$eventName = types_render_field( "event-name", array( 'raw' => true));
$email = types_render_field( "email-user", array( 'raw' => true));
$eventdate = types_render_field( "event-date", array( 'raw' => true) );
$newDate = date("d-m-Y", strtotime($eventdate));
$date = strtotime ('-30 days', strtotime ($newDate));
$date = date ('d-m-Y', $date);
$today = date("d-m-Y");
$balance = types_render_field( "remaining", array( 'raw' => true) );
if (!empty($balance) && $today === $date ) {
$to = $email;
$subject = "Payment reminder";
$content="Please pay the final payment here";
/*$from = "[email protected]";
$headers = "From:" . $from;*/
wp_mail($to,$subject,$content,$headers);
}
}
}
If you’re gonna have lots of quotes, I suggest breaking them into batches, and perhaps setting the event to hourly, so that it doesn’t timeout or eat your server resources.
I recommend that you setup the cron to be run from your system task scheduler (here’s a guide https://developer.wordpress.org/plugins/cron/hooking-into-the-system-task-scheduler/)
Cron reference https://developer.wordpress.org/plugins/cron/
Regards,