Ali_k
Ali_k

Reputation: 1661

How to to send emails only once to users in a function repeated by cron job

I'm using WP CRON say every week to check for users that will expire in 30 days using this

global $wpdb;

$daysPriorToRebill = 30;
$priorToRebillDate = date('Y-m-d', strtotime("+{$daysPriorToRebill} days"));
$todaydate = date('Y-m-d');   


    $sql3 = "SELECT DISTINCT wp_user_id
    FROM mm_user_data
    WHERE expiration_date <= '".$priorToRebillDate."' AND expiration_date >= '".$todaydate."'";
$userid = $wpdb->get_results($sql3);
$user_id =  $userids->wp_user_id;

after I find these users I send them an email with a link to pay, once they pay, I have another function to extend their membership by year interval

    $from_email = get_option('admin_email');

       $to = $user_email;

        $subject = "Hey update your membership;;

        $headers = "";

        $headers .= "From:" . $from_email . "\r\n";

        $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

        $message = "Message with link";



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

if they pay within a week, they will not recieve the next email, but if they take longer they will have to recieve this email again, is there a way to exclude users who already recieved the email in the next cron job. I'm thinking of inserting a specific value in a database field like 'sent' and checking against it, but not sure how to go about it or if there is a better way.

Upvotes: 0

Views: 610

Answers (1)

jdow
jdow

Reputation: 316

Your idea of adding a new field would work.

Another option would be to send emails only to those who would expire in 23-30 days, assuming the job runs weekly.

Alternatively run it daily and check only for expiring in exactly 30 days.

Upvotes: 1

Related Questions