View source
<?php
define('REROUTE_EMAIL_ADDRESS', 'reroute_email_address');
define('REROUTE_EMAIL_ENABLE_MESSAGE', 'reroute_email_enable_message');
define('REROUTE_EMAIL_ENABLE', 'reroute_email_enable');
define('REROUTE_EMAIL_EMAIL_SPLIT_RE', '/[\\s,;]+/');
function reroute_email_perm() {
return array(
'administer reroute email',
);
}
function reroute_email_menu() {
$items = array();
$items['admin/settings/reroute_email'] = array(
'title' => 'Reroute Email',
'description' => 'Reroute emails to a test address.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'reroute_email_settings',
),
'access arguments' => array(
'administer reroute email',
),
'file' => 'reroute_email.admin.inc',
);
$items['admin/settings/reroute_email/settings'] = array(
'title' => 'Settings',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/settings/reroute_email/test'] = array(
'title' => 'Test email form',
'type' => MENU_LOCAL_TASK,
'description' => 'Form for sending test email.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'reroute_email_test_email_form',
),
'access arguments' => array(
'administer reroute email',
),
'file' => 'reroute_email.admin.inc',
);
return $items;
}
function reroute_email_mail_alter(&$message) {
if (variable_get(REROUTE_EMAIL_ENABLE, 0)) {
global $base_url;
if (!variable_get(REROUTE_EMAIL_ADDRESS, '')) {
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;
if (isset($message['headers']) && is_array($message['headers'])) {
$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]);
}
}
}
$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)) {
$message['headers']['X-Rerouted-Original-To'] = $to;
$message['to'] = $addresslist[0];
if (variable_get(REROUTE_EMAIL_ENABLE_MESSAGE, 1)) {
$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";
if (is_string($message['body'])) {
$message['body'] = $msg . $message['body'];
}
else {
array_unshift($message['body'], $msg);
}
}
}
}
}
function reroute_email_mail($key, &$message, $params) {
if ($key != 'test_email_form') {
return;
}
$message['headers']['Cc'] = $params['cc'];
$message['headers']['Bcc'] = $params['bcc'];
$message['subject'] = $params['subject'];
$message['body'][] = $params['body'];
}