You are here

function simplenews_mail_spool in Simplenews 6

Same name and namespace in other branches
  1. 6.2 includes/simplenews.mail.inc \simplenews_mail_spool()
  2. 7.2 includes/simplenews.mail.inc \simplenews_mail_spool()
  3. 7 includes/simplenews.mail.inc \simplenews_mail_spool()

Send simplenews newsletters from the spool.

Individual newsletter emails are stored in database spool. Sending is triggered by cron or immediately when the node is saved. Mail data is retreived from the spool, rendered and send one by one If sending is succesful the message is marked as send in the spool.

2 calls to simplenews_mail_spool()
simplenews_cron in ./simplenews.module
Implementation of hook_cron().
simplenews_send_node in ./simplenews.module
Send newsletter node to subcribers.
3 string references to 'simplenews_mail_spool'
simplenews_update_6004 in ./simplenews.install
Add spool table and remove cache table.
simplenews_update_6009 in ./simplenews.install
Addition of error to simplenews_mail_cache to mark an email error.
simplenews_update_6201 in ./simplenews.install
Correction of field size mismatch caused by simplenews_update_6009().

File

./simplenews.module, line 1783
Simplnews node handling, sent email, newsletter block and general hooks

Code

function simplenews_mail_spool($nid = NULL, $vid = NULL, $limit = NULL) {

  // Send pending messages from database cache
  // A limited number of mails is retrieved from the spool
  $limit = isset($limit) ? $limit : variable_get('simplenews_throttle', 20);
  if ($messages = simplenews_get_spool(SIMPLENEWS_SPOOL_PENDING, $nid, $vid, $limit)) {
    $count_fail = $count_success = 0;

    // Get PHP maximum execution time. 30 seconds default.
    $max_execution_time = ini_get('max_execution_time') ? ini_get('max_execution_time') : SIMPLENEWS_MAX_EXECUTION_TIME;
    _simplenews_measure_usec(TRUE);
    foreach ($messages as $key => $message) {
      $result = simplenews_mail_mail($message['nid'], $message['vid'], $message['mail']);

      // Update spool status.
      // This is not optimal for performance but prevents duplicate emails
      // in case of PHP execution time overrun.
      simplenews_update_spool(array(
        $key,
      ), $result);
      if ($result['status'] == SIMPLENEWS_SPOOL_DONE) {
        $count_success++;
      }
      if ($result['error']) {
        $count_fail++;
      }

      // Check every n emails if we exceed the limit.
      // When PHP maximum execution time is almost elapsed we interrupt
      // sending. The remainder will be sent during the next cron run.
      if (++$check_counter >= SIMPLENEWS_SEND_CHECK_INTERVAL) {
        $check_counter = 0;

        // Break the sending if a percentage of max execution time was exceeded.
        $elapsed = _simplenews_measure_usec();
        if ($elapsed > SIMPLENEWS_SEND_TIME_LIMIT * $max_execution_time) {
          watchdog('simplenews', 'Sending interrupted: PHP maximum execution time almost exceeded. Remaining newsletters will be sent during the next cron run. If this warning occurs regularly you should reduce the !cron_throttle_setting.', array(
            '!cron_throttle_setting' => l(t('Cron throttle setting'), 'admin/settings/simplenews/mail'),
          ), WATCHDOG_WARNING);
          break;
        }
      }
    }

    // Report sent result and elapsed time. On Windows systems retrusage() is
    // not implemented and hence no elapsed time is available.
    if (function_exists('getrusage')) {
      watchdog('simplenews', '%success emails sent in %sec seconds, %fail failed sending.', array(
        '%success' => $count_success,
        '%sec' => round(_simplenews_measure_usec(), 1),
        '%fail' => $count_fail,
      ));
    }
    else {
      watchdog('simplenews', '%success emails sent, %fail failed.', array(
        '%success' => $count_success,
        '%fail' => $count_fail,
      ));
    }
  }
}