0

I have used two plugins for cron

1) Advance Cron Manager 2) WP Control

I have added cron in both plugins, but it removes from queue after some time when there is no activity on website (frontenf/Backend).

It's odd but cron interval time is 1 minute, but last till I am logged in dashboard for testing same thing.

Cron Type: Recurring after 1 minute. Here is screenshot of schedule event. https://prnt.sc/n9e5x2

This is code from my plugin file

add_action('new_email_event', 'identify_new_email_received');

function identify_new_email_received() {    
    ini_set('display_errors', '1'); 
    $settings = odz_get_whatsappsmtp_settings();    
    include(dirname(__FILE__).'/src/PhpImap/__autoload.php');   

    $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
    $username = $settings['configured_email'];
    $password = $settings['email_password'];

    $mailbox = new PhpImap\Mailbox('{imap.gmail.com:993/imap/ssl}INBOX', $username, $password);
    //$mailbox = new PhpImap\Mailbox('{imap.gmail.com:993/imap/ssl}INBOX', $settings['configured_email'], $settings['email_password'], __DIR__);                

    $since_date = date('Y-m-d',strtotime("-1 days"));       
    $mailsIds = $mailbox->searchMailbox('SINCE "'.$since_date.'"'); 

    if(!$mailsIds) {
        my_custom_log('No emails');     
    }


    $email_data = array();
    if(!get_site_option('wp_last_email_time')){
        my_custom_log('No last email');
        //whatsapp the last email
        $latest_mail = count($mailsIds) -1;
        $mail = $mailbox->getMail($mailsIds[$latest_mail]);
        $email_data['date'] = $mail->date;
        $email_data['text'] = $mail->textPlain;
        $email_data['id'] = $mail->id;
        $email_data['subject'] = $mail->subject;
        $email_data['fromAddress'] = $mail->fromAddress;
        $email_data['fromName'] = $mail->fromName;
        $sender = get_sender_number($mail->textHtml);
        $msg = get_sender_message($mail->textHtml);
        if(!empty($msg)){
            $email_data['text'] = $msg;
        }

        if(!empty($sender)){
            $mailbox->deleteMail($mail->id);
            sendWhatsappMessage($email_data,$sender);
            update_site_option('wp_last_email_time',strtotime($mail->date));
        }
    }
    else{
        my_custom_log('Looping through mail ids');
        foreach($mailsIds as $mail_id){
            $mail = $mailbox->getMail($mail_id);
            $last_email = get_site_option('wp_last_email_time');
            my_custom_log('last email time');
            my_custom_log($last_email);
            my_custom_log('************************');
            if(strtotime($mail->date) > $last_email){
                $email_data = array();
                $email_data['date'] = $mail->date;
                $email_data['text'] = $mail->textPlain;
                $email_data['id'] = $mail->id;
                $email_data['subject'] = $mail->subject;
                $email_data['fromAddress'] = $mail->fromAddress;
                $email_data['fromName'] = $mail->fromName;
                $sender = get_sender_number($mail->textHtml);
                $msg = get_sender_message($mail->textHtml);
                if(!empty($msg)){
                    $email_data['text'] = $msg;
                }
                if(!empty($sender)){
                    sendWhatsappMessage($email_data,$sender);
                    update_site_option('wp_last_email_time',strtotime($mail->date));
                    $mailbox->deleteMail($mail->id);
                }
            }
            else{
                //echo 'this email not sent. '.$mail->subject.' '.$mail->date.' '.strtotime($mail->date).'</br>';
            }

        }

    }

}
  • Can you share your cron_schedules filter code and the scheduled function? – Karun Apr 9 at 5:21
  • I have updated the question, with the function that will be called when cron executes. – Hemant Kumar Apr 9 at 6:00
  • WP Cron will not run when the site is inactive. You can try to replace WP Cron by cpanel cron. Instructions here – Karun Apr 9 at 6:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.