You are here

public function SendMailQueueWorker::processItem in Queue Mail 8

Works on a single queue item.

Parameters

mixed $data: The data that was passed to \Drupal\Core\Queue\QueueInterface::createItem() when the item was queued.

Throws

\Drupal\Core\Queue\RequeueException Processing is not yet finished. This will allow another process to claim the item immediately.

\Exception A QueueWorker plugin may throw an exception to indicate there was a problem. The cron process will log the exception, and leave the item in the queue to be processed again later.

\Drupal\Core\Queue\SuspendQueueException More specifically, a SuspendQueueException should be thrown when a QueueWorker plugin is aware that the problem will affect all subsequent workers of its queue. For example, a callback that makes HTTP requests may find that the remote server is not responding. The cron process will behave as with a normal Exception, and in addition will not attempt to process further items from the current item's queue during the current cron run.

Overrides QueueWorkerInterface::processItem

See also

\Drupal\Core\Cron::processQueues()

File

src/Plugin/QueueWorker/SendMailQueueWorker.php, line 119

Class

SendMailQueueWorker
Sends emails form queue.

Namespace

Drupal\queue_mail\Plugin\QueueWorker

Code

public function processItem($message) {
  $original_message = $message;
  $interval = $this->config
    ->get('requeue_interval');

  // Prevent retrying until specified interval has elapsed.
  if (isset($message['last_attempt']) && $message['last_attempt'] + $interval > time()) {

    // Skip item.
    throw new \RuntimeException(sprintf('Sending of mail "%s" is skipped in the mail queue due to requeue interval.', $message['id']));
  }

  // Invoke hook_queue_mail_send_alter() to allow all modules to alter the
  // email before sending.
  $this->moduleHandler
    ->alter('queue_mail_send', $message);

  // The caller requested sending. Sending was canceled by one or more
  // hook_queue_mail_send_alter() implementations. We set 'result' to NULL,
  // because FALSE indicates an error in sending.
  if (empty($message['send'])) {
    $message['result'] = NULL;
    return $message;
  }

  // Retrieve the responsible implementation for this message.
  $system = $this->mailManager
    ->getInstance([
    'module' => $message['module'],
    'key' => $message['key'],
  ]);

  // Set theme that was used to generate mail body.
  $this
    ->setMailTheme($message);

  // Set mail's language as active.
  $current_langcode = $this
    ->setMailLanguage($message);
  try {

    // Format the message body.
    $message = $system
      ->format($message);
  } finally {

    // Revert the active theme, this is done inside a finally block so it is
    // executed even if an exception is thrown during sending a mail.
    $this
      ->setActiveTheme($message);

    // Revert the active language.
    $this
      ->setActiveLanguage($message, $current_langcode);
  }

  // Ensure that subject is plain text. By default translated and
  // formatted strings are prepared for the HTML context and email
  // subjects are plain strings.
  if ($message['subject']) {
    $message['subject'] = PlainTextOutput::renderFromHtml($message['subject']);
  }
  $message['result'] = $system
    ->mail($message);

  // Log errors.
  if (!$message['result']) {
    $this->logger
      ->error('Error sending email (from %from to %to with reply-to %reply).', [
      '%from' => $message['from'],
      '%to' => $message['to'],
      '%reply' => $message['reply-to'] ? $message['reply-to'] : $this
        ->t('not set'),
    ]);
    $this
      ->processRetryLimit($original_message);
  }
  $this
    ->waitBetweenSending();
  return $message;
}