You are here

queue_mail.module in Queue Mail 8

Same filename and directory in other branches
  1. 5 queue_mail.module
  2. 6 queue_mail.module
  3. 7 queue_mail.module

The Queue Mail module.

File

queue_mail.module
View source
<?php

/**
 * @file
 * The Queue Mail module.
 */

/**
 * Get an instance of the mail queue.
 */
function _queue_mail_get_queue() {
  return \Drupal::queue('queue_mail', TRUE);
}

/**
 * Implements hook_mail_alter().
 */
function queue_mail_mail_alter(&$message) {
  $message['queued'] = FALSE;

  // Avoid queueing a message if it is not going to be sent, as it would be
  // skipped by our queue worker anyway.
  if (empty($message['send'])) {
    return;
  }
  $config = \Drupal::config('queue_mail.settings');
  $mail_keys = $config
    ->get('queue_mail_keys');
  if (\Drupal::service('path.matcher')
    ->matchPath($message['id'], $mail_keys)) {

    // Save theme that is used to format mail.
    $message['theme'] = \Drupal::service('theme.manager')
      ->getActiveTheme()
      ->getName();

    // Add message to the queue.
    $id = _queue_mail_get_queue()
      ->createItem($message);

    // Was the message added to queue?
    $message['queued'] = $id ? TRUE : FALSE;

    // Prevent the message from being sent instantly.
    $message['send'] = FALSE;
  }
}

/**
 * Implements hook_module_implements_alter().
 */
function queue_mail_module_implements_alter(&$implementations, $hook) {
  if ($hook == 'mail_alter') {

    // Ensure that our hook_mail_alter implementation is always called last.
    if (isset($implementations['queue_mail'])) {
      $group = $implementations['queue_mail'];
      unset($implementations['queue_mail']);

      // Now add it back, which will ensure we are called last.
      $implementations['queue_mail'] = $group;
    }
  }
}

/**
 * Implements hook_queue_info_alter().
 */
function queue_mail_queue_info_alter(&$queues) {
  $queue_time = \Drupal::config('queue_mail.settings')
    ->get('queue_mail_queue_time');
  if (!empty($queue_time)) {
    $queues['queue_mail']['cron']['time'] = $queue_time;
  }
}