You are here

function mail_safety_mail_alter in Mail Safety 8

Same name and namespace in other branches
  1. 7.2 mail_safety.module \mail_safety_mail_alter()
  2. 7 mail_safety.module \mail_safety_mail_alter()

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.

File

./mail_safety.module, line 33
The core Mail Safety module file.

Code

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;
  }
}