You are here

mail_safety.module in Mail Safety 8

Same filename and directory in other branches
  1. 7.2 mail_safety.module
  2. 7 mail_safety.module

The core Mail Safety module file.

File

mail_safety.module
View source
<?php

/**
 * @file
 * The core Mail Safety module file.
 */
use Drupal\mail_safety\Controller\MailSafetyController;

/**
 * Implements hook_theme().
 */
function mail_safety_theme() {
  return [
    'mail_safety_mail' => [
      'variables' => [
        'mail' => NULL,
      ],
    ],
    'mail_safety_details' => [
      'variables' => [
        'mail' => NULL,
        'details' => NULL,
      ],
    ],
  ];
}

/**
 * Implements hook_mail_alter().
 *
 * Filters any e-mail going through the drupal mail system.
 *
 * If Mail Safety is enabled it stops all mails from being sent depending on
 * the settings it will either send the mails to a default mail address and/or
 * send it to the dashboard.
 */
function mail_safety_mail_alter(&$message) {
  $config = \Drupal::config('mail_safety.settings');
  if ($config
    ->get('enabled')) {
    $message['send'] = FALSE;
  }

  // If mail to dashboard is enabled it'll send the mail to the dashboard.
  if ($config
    ->get('send_mail_to_dashboard')) {
    MailSafetyController::insert($message);
  }

  // If mail to default mail is enabled it will send the mail to the default
  // mail address.
  if ($config
    ->get('send_mail_to_default_mail')) {
    $message['to'] = $config
      ->get('default_mail_address');
    unset($message['headers']['Cc'], $message['headers']['Bcc']);
    $message['send'] = TRUE;
  }
}

/**
 * Implements hook_module_implements_alter().
 *
 * Make sure our hook is called last.
 */
function mail_safety_module_implements_alter(&$implementations, $hook) {
  if ($hook == 'mail_alter' && isset($implementations['mail_safety'])) {

    // Move mail_safety_mail_alter() to the end of the list.
    $group = $implementations['mail_safety'];
    unset($implementations['mail_safety']);
    $implementations['mail_safety'] = $group;
  }
}