You are here

function reroute_email_mail_alter in Reroute Email 6

Same name and namespace in other branches
  1. 8 reroute_email.module \reroute_email_mail_alter()
  2. 5 reroute_email.module \reroute_email_mail_alter()
  3. 7 reroute_email.module \reroute_email_mail_alter()
  4. 2.x reroute_email.module \reroute_email_mail_alter()

Implements hook_mail_alter().

This hook is required to change the destination of outgoing emails.

File

./reroute_email.module, line 64
Intercepts all outgoing emails to be rerouted to a configurable destination.

Code

function reroute_email_mail_alter(&$message) {
  if (variable_get(REROUTE_EMAIL_ENABLE, 0)) {
    global $base_url;
    if (!variable_get(REROUTE_EMAIL_ADDRESS, '')) {

      // If email address not in settings, then do nothing.
      return;
    }
    if (empty($message)) {
      return;
    }
    if (!is_array($message)) {
      return;
    }
    $mailkey = isset($message['id']) ? $message['id'] : t('[mail id] is missing');
    $to = isset($message['to']) ? $message['to'] : t('[to] is missing');
    $message['headers']['X-Rerouted-Mail-Key'] = $mailkey;
    $message['headers']['X-Rerouted-Website'] = $base_url;

    // Unset Bcc and Cc fields to prevent emails from going to those addresses.
    if (isset($message['headers']) && is_array($message['headers'])) {

      // Ensure we catch all Cc and Bcc headers, regardless of case,
      // and protecting against multiple instances of the "same" header.
      $header_keys = array();
      foreach (array_keys($message['headers']) as $key) {
        $header_keys[strtolower($key)][] = $key;
      }
      if (!empty($header_keys['cc'])) {
        foreach ($header_keys['cc'] as $header) {
          $message['headers']['X-Rerouted-Original-Cc'] = $message['headers'][$header];
          unset($message['headers'][$header]);
        }
      }
      if (!empty($header_keys['bcc'])) {
        foreach ($header_keys['bcc'] as $header) {
          $message['headers']['X-Rerouted-Original-Bcc'] = $message['headers'][$header];
          unset($message['headers'][$header]);
        }
      }
    }

    // Split the address string on whitespace, ignoring any empty results.
    $addresslist = preg_split(REROUTE_EMAIL_EMAIL_SPLIT_RE, variable_get(REROUTE_EMAIL_ADDRESS, variable_get('site_mail', ini_get('sendmail_from'))), -1, PREG_SPLIT_NO_EMPTY);
    if (!in_array($to, $addresslist)) {

      // Not on the list, so reroute to the first address in the list.
      $message['headers']['X-Rerouted-Original-To'] = $to;
      $message['to'] = $addresslist[0];
      if (variable_get(REROUTE_EMAIL_ENABLE_MESSAGE, 1)) {

        // Format a message to show at the top.
        $msg = t("This email was rerouted.") . "\n";
        $msg .= t("Web site: @site", array(
          '@site' => $base_url,
        )) . "\n";
        $msg .= t("Mail key: @key", array(
          '@key' => $mailkey,
        )) . "\n";
        $msg .= t("Originally to: @to", array(
          '@to' => $to,
        )) . "\n";
        $msg .= "-----------------------\n";

        // Prepend explanation message to the body of the email. This must be
        // handled differently depending on whether the body came in as a
        // string or an array. If it came in as a string (despite the fact it
        // should be an array) we'll respect that and leave it as a string.
        if (is_string($message['body'])) {
          $message['body'] = $msg . $message['body'];
        }
        else {
          array_unshift($message['body'], $msg);
        }
      }
    }
  }
}