You are here

reroute_email.module in Reroute Email 5

Reroute Email module

File

reroute_email.module
View source
<?php

/**
 * @file
 * Reroute Email module
 */
define('REROUTE_EMAIL_ADDRESS', 'reroute_email_address');

/**
 * Implements hook_menu().
 */
function reroute_email_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/reroute_email',
      'title' => 'Reroute Email',
      'description' => 'Reroute emails to a test address.',
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'reroute_email_settings',
      ),
      'type' => MENU_NORMAL_ITEM,
    );
  }
  return $items;
}

/**
 * Menu Callback: Reroute Email admin configuration form.
 */
function reroute_email_settings() {
  $form[REROUTE_EMAIL_ADDRESS] = array(
    '#type' => 'textfield',
    '#title' => t('Email address'),
    '#required' => TRUE,
    '#default_value' => variable_get(REROUTE_EMAIL_ADDRESS, ini_get('sendmail_from')),
    '#size' => 35,
    '#description' => t('The email address to reroute all emails from the site to.'),
  );
  return system_settings_form($form);
}

/**
 * Implements hook_mail_alter().
 *
 * This hook is required to change the destination of outgoing emails.
 */
function reroute_email_mail_alter(&$mailkey, &$to, &$subject, &$body, &$from, &$headers) {
  global $base_url;

  // Unset Bcc and Cc fields to prevent emails from going to those addresses.
  unset($headers['Bcc']);
  unset($headers['Cc']);

  // Format a message to show at the top.
  $msg = "This email was rerouted.\n";
  $msg .= "Web site: @site\n";
  $msg .= "Mail key: @key\n";
  $msg .= "Originally to: <@to>\n";
  $msg .= "-----------------------\n";

  // Prepend to the body of the email.
  $body = t($msg, array(
    '@site' => $base_url,
    '@to' => $to,
    '@key' => $mailkey,
  )) . $body;

  // Change the $to address to be the one we defined.
  $to = variable_get(REROUTE_EMAIL_ADDRESS, ini_get('sendmail_from'));
}

Functions

Namesort descending Description
reroute_email_mail_alter Implements hook_mail_alter().
reroute_email_menu Implements hook_menu().
reroute_email_settings Menu Callback: Reroute Email admin configuration form.

Constants

Namesort descending Description
REROUTE_EMAIL_ADDRESS @file Reroute Email module