View source
<?php
define('REROUTE_EMAIL_ENABLE', 'reroute_email_enable');
define('REROUTE_EMAIL_ADDRESS', 'reroute_email_address');
define('REROUTE_EMAIL_WHITELIST', 'reroute_email_whitelist');
define('REROUTE_EMAIL_ENABLE_MESSAGE', 'reroute_email_enable_message');
define('REROUTE_EMAIL_ENABLE_DSM', 'reroute_email_enable_dsm');
define('REROUTE_EMAIL_MAIL_KEYS', 'reroute_email_mail_keys');
define('REROUTE_EMAIL_ADDRESS_EMPTY_PLACEHOLDER', '[No reroute email address configured]');
function reroute_email_module_implements_alter(&$implementations, $hook) {
if ($hook == 'mail_alter') {
$group = $implementations['reroute_email'];
unset($implementations['reroute_email']);
$implementations['reroute_email'] = $group;
if (isset($implementations['queue_mail'])) {
$group = $implementations['queue_mail'];
unset($implementations['queue_mail']);
$implementations['queue_mail'] = $group;
}
}
}
function reroute_email_permission() {
return array(
'administer reroute email' => array(
'title' => t('Administer Reroute Email'),
'description' => t('Administer the Reroute Email module.'),
'restrict access' => TRUE,
),
);
}
function reroute_email_menu() {
$items['admin/config/development/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/config/development/reroute_email/settings'] = array(
'title' => 'Settings',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/config/development/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) {
global $base_url;
if (empty($message) || !is_array($message)) {
return;
}
if (!empty($message['headers']) && isset($message['headers']['X-Rerouted-Force'])) {
if (FALSE === (bool) $message['headers']['X-Rerouted-Force']) {
return;
}
}
elseif (reroute_email_check($message) === FALSE) {
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]);
}
}
}
$rerouting_addresses = variable_get(REROUTE_EMAIL_ADDRESS, variable_get('site_mail', ini_get('sendmail_from')));
$message['headers']['X-Rerouted-Original-To'] = $to;
$message['to'] = empty($rerouting_addresses) ? REROUTE_EMAIL_ADDRESS_EMPTY_PLACEHOLDER : $rerouting_addresses;
if (variable_get(REROUTE_EMAIL_ENABLE_MESSAGE, 1)) {
$message_lines = array(
t('This email was rerouted.'),
t('Web site: @site', array(
'@site' => $base_url,
)),
t('Mail key: @key', array(
'@key' => $mailkey,
)),
t('Originally to: @to', array(
'@to' => $to,
)),
);
if (!empty($message['headers']['X-Rerouted-Original-Cc'])) {
$message_lines[] = t('Originally cc: @cc', array(
'@cc' => $message['headers']['X-Rerouted-Original-Cc'],
));
}
if (!empty($message['headers']['X-Rerouted-Original-Bcc'])) {
$message_lines[] = t('Originally bcc: @bcc', array(
'@bcc' => $message['headers']['X-Rerouted-Original-Bcc'],
));
}
$message_lines[] = '-----------------------';
$message_lines[] = '';
$msg = implode(PHP_EOL, $message_lines);
if (is_string($message['body'])) {
$message['body'] = $msg . $message['body'];
}
else {
array_unshift($message['body'], $msg);
}
}
if ($rerouting_addresses === '') {
$message['send'] = FALSE;
unset($message['params']);
$message_string = var_export($message, TRUE);
watchdog('reroute_email', 'Aborted email sending for <em>@message_id</em>.<br />Detailed email data: Array $message <pre>@message</pre>', array(
'@message_id' => $message['id'],
'@message' => $message_string,
));
if (variable_get(REROUTE_EMAIL_ENABLE_DSM, 1)) {
drupal_set_message(t('<em>@message_id</em> was aborted by reroute email; site administrators can check the recent log entries for complete details on the rerouted email.', array(
'@message_id' => $message['id'],
)));
}
}
elseif (variable_get(REROUTE_EMAIL_ENABLE_DSM, 1)) {
drupal_set_message(t('Submitted email, with ID: <em>@message_id</em>, was rerouted to configured address: <em>@reroute_target</em>. For more details please refer to Reroute Email settings.', array(
'@message_id' => $message['id'],
'@reroute_target' => $message['to'],
)));
}
}
function reroute_email_mail($key, &$message, $params) {
if ('reroute_email_test_email_form' !== $message['id']) {
return;
}
$message['headers']['Cc'] = $params['cc'];
$message['headers']['Bcc'] = $params['bcc'];
$message['subject'] = $params['subject'];
$message['body'][] = $params['body'];
}
function reroute_email_check(array $message) {
if (!variable_get(REROUTE_EMAIL_ENABLE, FALSE)) {
return FALSE;
}
$keys = reroute_email_split_string(variable_get(REROUTE_EMAIL_MAIL_KEYS, ''));
if (!empty($keys) && !(in_array($message['id'], $keys, TRUE) || in_array($message['module'], $keys, TRUE))) {
$message['header']['X-Reroute-Status'] = 'MAILKEY-IGNORED';
return FALSE;
}
$original_addresses = reroute_email_split_string($message['to']);
$whitelisted_addresses = reroute_email_split_string(variable_get(REROUTE_EMAIL_WHITELIST, ''));
$whitelisted_domains = array();
foreach ($whitelisted_addresses as $key => $email) {
if (preg_match('/^\\*@(.*)$/', $email, $matches)) {
$domain = drupal_strtolower($matches[1]);
$whitelisted_domains[$domain] = $domain;
unset($whitelisted_addresses[$key]);
}
}
$invalid = 0;
foreach ($original_addresses as $email) {
$domain = drupal_strtolower(drupal_substr(strrchr($email, '@'), 1));
if (in_array($email, $whitelisted_addresses, TRUE) || in_array($domain, $whitelisted_domains, TRUE)) {
continue;
}
if (valid_email_address($email) === FALSE) {
$invalid++;
continue;
}
$message['header']['X-Reroute-Status'] = 'REROUTED';
return TRUE;
}
if (count($original_addresses) === $invalid) {
$message['header']['X-Reroute-Status'] = 'INVALID-ADDRESSES';
return TRUE;
}
$message['header']['X-Reroute-Status'] = 'WHITELISTED';
return FALSE;
}
function reroute_email_split_string($string) {
$array = preg_split('/[\\s,;\\n]+/', $string, -1, PREG_SPLIT_NO_EMPTY);
$array = array_unique($array);
return $array;
}
function reroute_email_element_validate_addresses($element, &$form_state, $form) {
$field_name = $element['#name'];
$addresses = reroute_email_split_string($form_state['values'][$field_name]);
foreach ($addresses as $address) {
if (!valid_email_address($address)) {
form_set_error($field_name, t('@address is not a valid email address.', array(
'@address' => $address,
)));
}
}
form_set_value($form[$field_name], implode(',', $addresses), $form_state);
}