/** * This function sends all the emails that are stored in the queue. * It will be called * - immediately after queueing messages in case of synchronous emails * - from a cronjob in case of asynchronous emails * If a failure occurs, then the function exits. * @todo In case of synchronous email sending, we may get a race condition where two requests send the same email. * @param bool $p_delete_on_failure indicates whether to remove email from queue on failure (default false) * @return null */ function email_send_all($p_delete_on_failure = false) { $t_ids = email_queue_get_ids('ASC'); $t_date_format = config_get('complete_date_format'); log_event(LOG_EMAIL, "Processing e-mail queue (" . count($t_ids) . " messages)"); foreach ($t_ids as $t_id) { $t_email_data = email_queue_get($t_id); $t_start = microtime(true); log_event(LOG_EMAIL, "Sending message #{$t_id} queued on " . date($t_date_format, $t_email_data->submitted)); # check if email was not found. This can happen if another request # picks up the email first and sends it. if ($t_email_data === false) { $t_email_sent = true; log_event(LOG_EMAIL, 'message has already been sent'); } else { $t_email_sent = email_send($t_email_data); } if (!$t_email_sent) { if ($p_delete_on_failure) { email_queue_delete($t_email_data->email_id); } # If unable to place the email in the email server queue and more # than 5 seconds have elapsed, then we assume that the server # connection is down, hence no point to continue trying with the # rest of the emails. if (microtime(true) - $t_start > 5) { log_event(LOG_EMAIL, 'Server not responding for 5 seconds, aborting'); break; } } } }
} else { $t_email_data = email_queue_get((int) $f_to); // check if email was found. This can fail if another request picks up the email first and sends it. echo 'Sending email...<br />'; if ($t_email_data !== false) { if (!email_send($t_email_data)) { echo 'Email Not Sent - Deleting from queue<br />'; email_queue_delete($t_email_data->email_id); } else { echo 'Email Sent<br />'; } } else { echo 'Email not found in queue<br />'; } } } } $t_ids = email_queue_get_ids(); if (count($t_ids) > 0) { echo '<table><tr><th>' . lang_get('id') . '</th><th>' . lang_get('email') . '</th><th>' . lang_get('timestamp') . '</th><th>Send Or Delete</th></tr>'; foreach ($t_ids as $t_id) { $row = email_queue_get($t_id); echo '<tr><td>' . $row->email_id . '</td><td>' . $row->email . '</td><td>' . date(config_get('complete_date_format'), $row->submitted) . '</td><td>', html_button('email_queue.php', 'Send Or Delete', array('send' => $row->email_id)), '</td></tr>'; } echo '</table>'; html_button('email_queue.php', 'Send All', array('send' => 'all')); html_button('email_queue.php', 'Send Or Delete All', array('send' => 'sendordelall')); } else { echo 'Email Queue Empty'; } html_page_bottom();
function email_send_all() { $t_ids = email_queue_get_ids(); $t_emails_recipients_failed = array(); $t_start = microtime_float(); foreach ($t_ids as $t_id) { $t_email_data = email_queue_get($t_id); # check if email was not found. This can happen if another request picks up the email first and sends it. if ($t_email_data === false) { continue; } # if unable to place the email in the email server queue, then the connection to the server is down, # and hence no point to continue trying with the rest of the emails. if (!email_send($t_email_data)) { if (microtime_float() - $t_start > 5) { break; } else { continue; } } } }