/** * Sends out newsletters. * * I recipients is an array of subscribers, other parameters are ignored and a test * batch is started. This parameter has priority over all. * * If continue is true, the system try to continue a previous batch keeping its * configuration (eg. if it was a simulation or not). * * If continue is false, simulate indicates if the batch is a simulation and forces * the subscriber's email to a test one, as specified in the configuration. * * Return true if the batch is completed. */ function newsletter_send_batch() { global $wpdb; newsletter_info(__FUNCTION__, 'Start'); $options = get_option('newsletter'); $options_email = get_option('newsletter_email'); $batch = get_option('newsletter_batch'); if ($batch == null || !is_array($batch)) { newsletter_error(__FUNCTION__, 'No batch found'); return; } newsletter_debug(__FUNCTION__, "Batch:\n" . print_r($last, true)); // Batch have to contain 'id' which is the starting id, 'simulate' boolean // to indicate if is a simulation or not, 'scheduled' if it's a scheduled // sending process. 'list' is the list number, required. // If 'id' = 0 it's a new seding process. if (!isset($batch['id'])) { newsletter_error(__FUNCTION__, 'Batch "id" parameter not present'); return false; } if (!isset($batch['list'])) { newsletter_error(__FUNCTION__, 'Batch "list" parameter not present'); return false; } if (!isset($batch['simulate'])) { newsletter_error(__FUNCTION__, 'Batch "simulate" parameter not present'); return false; } if (!isset($batch['scheduled'])) { newsletter_error(__FUNCTION__, 'Batch "scheduled" parameter not present'); return false; } $id = (int) $batch['id']; $list = (int) $batch['list']; $simulate = (bool) $batch['simulate']; $scheduled = (bool) $batch['scheduled']; // Used to avoid echo if ($scheduled) { $max = $options_email['scheduler_max']; if (!is_numeric($max)) { $max = 10; } } else { $max = $options_email['max']; if (!is_numeric($max)) { $max = 0; } } $query = "select * from " . $wpdb->prefix . "newsletter where status='C' and list=" . $list . " and id>" . $id . " order by id"; if ($max > 0) { $query .= " limit " . $max; } $recipients = $wpdb->get_results($query); // For a new batch save some info if ($id == 0) { newsletter_delete_batch_file(); wp_clear_scheduled_hook('newsletter_cron_hook'); $batch['total'] = $wpdb->get_var("select count(*) from " . $wpdb->prefix . "newsletter where status='C' and list=" . $list); $batch['sent'] = 0; $batch['completed'] = false; $batch['message'] = ''; } // Not all hosting provider allow this... @set_time_limit(100000); $start_time = time(); $max_time = (int) (ini_get('max_execution_time') * 0.8); $db_time = time(); if (!$scheduled) { echo 'Sending to: <br />'; } if (isset($options_email['novisual'])) { $message = $options_email['message']; } else { $message = '<html><head><style type="text/css">' . newsletter_get_theme_css($options_email['theme']) . '</style></head><body>' . $options_email['message'] . '</body></html>'; } $idx = 0; add_action('phpmailer_init', 'newsletter_phpmailer_init'); if (newsletter_has_extras('1.0.4')) { newsletter_init_mail(); } foreach ($recipients as $r) { $url = newsletter_add_qs($options['url'], 'na=u&ni=' . $r->id . '&nt=' . $r->token); $m = newsletter_replace_url($message, 'UNSUBSCRIPTION_URL', $url); $m = newsletter_replace($m, $r); if (defined('NEWSLETTER_EXTRAS') && isset($options_email['track'])) { $m = newsletter_relink($m, $r->id, $options_email['name']); } $s = $options_email['subject']; $s = newsletter_replace($s, $r); if ($simulate) { $x = newsletter_mail($options_email['simulate_email'], $s, $m, true); } else { $x = newsletter_mail($r->email, $s, $m, true); } if (!$scheduled) { echo htmlspecialchars($r->name) . ' (' . $r->email . ') '; if ($x) { echo '[OK] - '; newsletter_debug(__FUNCTION__, 'Sent to ' . $r->id . ' success'); } else { echo '[KO] - '; newsletter_debug(__FUNCTION__, 'Sent to ' . $r->id . ' failed'); } flush(); } $idx++; $batch['sent']++; $batch['id'] = $r->id; // Try to avoid database timeout if (time() - $db_time > 15) { newsletter_debug(__FUNCTION__, 'Batch saving to avoid database timeout'); $db_time = time(); $batch['message'] = 'Temporary saved batch to avoid database timeout'; if (!update_option('newsletter_batch', $batch)) { newsletter_error(__FUNCTION__, 'Unable to save to database, saving on file system'); newsletter_error(__FUNCTION__, "Batch:\n" . print_r($batch, true)); newsletter_save_batch_file($batch); remove_action('phpmailer_init', 'newsletter_phpmailer_init'); if (newsletter_has_extras('1.0.4')) { newsletter_close_mail(); } return false; } } // Check for the max emails per batch if ($max != 0 && $idx >= $max) { newsletter_info(__FUNCTION__, 'Batch saving due to max emails limit reached'); $batch['message'] = 'Batch max emails limit reached (it is ok)'; if (!update_option('newsletter_batch', $batch)) { newsletter_error(__FUNCTION__, 'Unable to save to database, saving on file system'); newsletter_error(__FUNCTION__, "Batch:\n" . print_r($batch, true)); newsletter_save_batch_file($batch); remove_action('phpmailer_init', 'newsletter_phpmailer_init'); if (newsletter_has_extras('1.0.4')) { newsletter_close_mail(); } return false; } remove_action('phpmailer_init', 'newsletter_phpmailer_init'); if (newsletter_has_extras('1.0.4')) { newsletter_close_mail(); } return true; } // Timeout check, max time is zero if set_time_limit works if ($max_time != 0 && time() - $start_time > $max_time) { newsletter_info(__FUNCTION__, 'Batch saving due to max time limit reached'); $batch['message'] = 'Batch max time limit reached (it is ok)'; if (!update_option('newsletter_batch', $batch)) { newsletter_error(__FUNCTION__, 'Unable to save to database, saving on file system'); newsletter_error(__FUNCTION__, "Batch:\n" . print_r($last, true)); newsletter_save_batch_file($batch); remove_action('phpmailer_init', 'newsletter_phpmailer_init'); if (newsletter_has_extras('1.0.4')) { newsletter_close_mail(); } return false; } remove_action('phpmailer_init', 'newsletter_phpmailer_init'); if (newsletter_has_extras('1.0.4')) { newsletter_close_mail(); } return true; } } // All right (incredible!) newsletter_info(__FUNCTION__, 'Sending completed!'); $batch['completed'] = true; $batch['message'] = ''; if (!update_option('newsletter_batch', $batch)) { newsletter_error(__FUNCTION__, 'Unable to save to database, saving on file system'); newsletter_error(__FUNCTION__, "Batch:\n" . print_r($last, true)); newsletter_save_batch_file($batch); remove_action('phpmailer_init', 'newsletter_phpmailer_init'); if (newsletter_has_extras('1.0.4')) { newsletter_close_mail(); } return false; } remove_action('phpmailer_init', 'newsletter_phpmailer_init'); if (newsletter_has_extras('1.0.4')) { newsletter_close_mail(); } return true; }
delete_option('newsletter_batch', array()); } if (isset($_POST['scheduled_simulate']) && check_admin_referer()) { $options = stripslashes_deep($_POST['options']); update_option('newsletter_email', $options); newsletter_send_scheduled(0, true); } if (isset($_POST['scheduled_send']) && check_admin_referer()) { $options = stripslashes_deep($_POST['options']); update_option('newsletter_email', $options); newsletter_send_scheduled(0, false); } if (isset($_POST['restore']) && check_admin_referer()) { $batch = newsletter_load_batch_file(); update_option('newsletter_batch', $batch); newsletter_delete_batch_file(); } // Theme style $css_url = null; $theme_dir = newsletter_get_theme_dir($options['theme']); if (file_exists($theme_dir . '/style.css')) { $css_url = newsletter_get_theme_url($options['theme']) . '/style.css'; } $nc = new NewsletterControls($options, 'composer'); if (!isset($options['novisual'])) { ?> <script type="text/javascript" src="<?php echo get_option('siteurl'); ?> /wp-content/plugins/newsletter/tiny_mce/tiny_mce.js"></script> <script type="text/javascript">