You are here

public function Mailer::attemptImmediateSend in Simplenews 8

Same name and namespace in other branches
  1. 8.2 src/Mail/Mailer.php \Drupal\simplenews\Mail\Mailer::attemptImmediateSend()
  2. 3.x src/Mail/Mailer.php \Drupal\simplenews\Mail\Mailer::attemptImmediateSend()

Send mail spool immediatly if cron should not be used.

Parameters

array $conditions: (Optional) Array of spool conditions which are applied to the query.

bool $use_batch: (optional) Whether the batch API should be used or not.

Return value

bool TRUE if the mails were sent or a batch was prepared, FALSE if not.

Overrides MailerInterface::attemptImmediateSend

File

src/Mail/Mailer.php, line 150

Class

Mailer
Default Mailer.

Namespace

Drupal\simplenews\Mail

Code

public function attemptImmediateSend(array $conditions = array(), $use_batch = TRUE) {
  if ($this->config
    ->get('mail.use_cron')) {
    return FALSE;
  }
  if ($use_batch) {

    // Set up as many send operations as necessary to send all mails with the
    // defined throttle amount.
    $throttle = $this->config
      ->get('mail.throttle');
    $spool_count = $this->spoolStorage
      ->countMails($conditions);
    $num_operations = ceil($spool_count / $throttle);
    $operations = array();
    for ($i = 0; $i < $num_operations; $i++) {
      $operations[] = array(
        '_simplenews_batch_dispatcher',
        array(
          'simplenews.mailer:sendSpool',
          $throttle,
          $conditions,
        ),
      );
    }

    // Add separate operations to clear the spool and update the send status.
    $operations[] = array(
      '_simplenews_batch_dispatcher',
      array(
        'simplenews.spool_storage:clear',
      ),
    );
    $operations[] = array(
      '_simplenews_batch_dispatcher',
      array(
        'simplenews.mailer:updateSendStatus',
      ),
    );
    $batch = array(
      'operations' => $operations,
      'title' => t('Sending mails'),
    );
    batch_set($batch);
  }
  else {

    // Send everything that matches the conditions immediately.
    $this
      ->sendSpool(SpoolStorageInterface::UNLIMITED, $conditions);
    $this->spoolStorage
      ->clear();
    $this
      ->updateSendStatus();
  }
  return TRUE;
}