You are here

public function Mailer::attemptImmediateSend in Simplenews 3.x

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

Send mail spool immediately 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 181

Class

Mailer
Default Mailer.

Namespace

Drupal\simplenews\Mail

Code

public function attemptImmediateSend(array $conditions = [], $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 = [];
    for ($i = 0; $i < $num_operations; $i++) {
      $operations[] = [
        '_simplenews_batch_dispatcher',
        [
          'simplenews.mailer:sendSpool',
          $throttle,
          $conditions,
        ],
      ];
    }

    // Add separate operations to clear the spool and update the send status.
    $operations[] = [
      '_simplenews_batch_dispatcher',
      [
        'simplenews.spool_storage:clear',
      ],
    ];
    $operations[] = [
      '_simplenews_batch_dispatcher',
      [
        'simplenews.mailer:updateSendStatus',
      ],
    ];
    $batch = [
      'operations' => $operations,
      'title' => $this
        ->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;
}