public static function process_queue()
 {
     //check that this isn't doing cron already - if this is MultiSite Global, then we place a lock at Network level
     $doing_emails = EM_MS_GLOBAL ? get_site_option('em_cron_doing_emails') : get_option('em_cron_doing_emails');
     if ($doing_emails) {
         //if process has been running for over 15 minutes or 900 seconds (e.g. likely due to a php error or timeout), let it proceed
         if ($doing_emails > time() - 900) {
             return false;
         }
     }
     EM_MS_GLOBAL ? update_site_option('em_cron_doing_emails', time()) : update_option('em_cron_doing_emails', time());
     //init phpmailer
     global $EM_Mailer, $wpdb;
     if (!is_object($EM_Mailer)) {
         $EM_Mailer = new EM_Mailer();
     }
     //get queue
     $limit = get_option('dbem_cron_emails_limit', 100);
     $count = 0;
     $sql = "SELECT * FROM " . EM_EMAIL_QUEUE_TABLE . " ORDER BY queue_id  ASC LIMIT 100";
     $results = $wpdb->get_results($sql);
     //loop through results of query whilst results exist
     while ($wpdb->num_rows > 0) {
         //go through current results set
         foreach ($results as $email) {
             //if we reach a limit (provided limit is > 0, remove lock and exit this function
             if ($count >= $limit && $limit > 0) {
                 EM_MS_GLOBAL ? update_site_option('em_cron_doing_emails', 0) : update_option('em_cron_doing_emails', 0);
                 return true;
             }
             //send email, immediately delete after from queue
             if ($EM_Mailer->send($email->subject, $email->body, $email->email, unserialize($email->attachment))) {
                 $wpdb->query("DELETE FROM " . EM_EMAIL_QUEUE_TABLE . ' WHERE queue_id =' . $email->queue_id);
             }
             //add to the count and move onto next email
             $count++;
         }
         //if we haven't reached a limit, load up new results
         $results = $wpdb->get_results($sql);
     }
     //remove the lock on this cron
     EM_MS_GLOBAL ? update_site_option('em_cron_doing_emails', 0) : update_option('em_cron_doing_emails', 0);
 }
 /**
  * Send an email and log errors in this object
  * @param string $subject
  * @param string $body
  * @param string $email
  * @return string
  */
 function email_send($subject, $body, $email)
 {
     global $EM_Mailer;
     if (!empty($subject)) {
         if (!is_object($EM_Mailer)) {
             $EM_Mailer = new EM_Mailer();
         }
         if (!$EM_Mailer->send($subject, $body, $email)) {
             if (is_array($EM_Mailer->errors)) {
                 foreach ($EM_Mailer->errors as $error) {
                     $this->errors[] = $error;
                 }
             } else {
                 $this->errors[] = $EM_Mailer->errors;
             }
             return false;
         }
     }
     return true;
 }
 function process_queue()
 {
     //init phpmailer
     global $EM_Mailer, $wpdb;
     if (!is_object($EM_Mailer)) {
         $EM_Mailer = new EM_Mailer();
     }
     add_action('em_mailer', array('EM_Emails', 'em_mailer_mod'), 10, 1);
     add_action('em_mailer_sent', array('EM_Emails', 'em_mailer_mod_cleanup'), 10, 1);
     //get queue
     $limit = get_option('emp_cron_emails_limit', 100);
     $results = $wpdb->get_results("SELECT * FROM " . EM_EMAIL_QUEUE_TABLE . " ORDER BY queue_id  ASC LIMIT {$limit}");
     $ids_to_delete = array();
     foreach ($results as $email) {
         $ids_to_delete[] = $email->queue_id;
         $EM_Mailer->send($email->subject, $email->body, $email->email, unserialize($email->attachment));
     }
     //cleanup
     if (count($ids_to_delete) > 0) {
         $wpdb->query("DELETE FROM " . EM_EMAIL_QUEUE_TABLE . ' WHERE queue_id IN (' . implode(',', $ids_to_delete) . ')');
     }
     remove_action('em_mailer', array('EM_Emails', 'em_mailer_mod'), 10, 1);
     remove_action('em_mailer_sent', array('EM_Emails', 'em_mailer_mod_cleanup'), 10, 1);
 }