| 1 |
<?php |
<?php |
| 2 |
// $Id: mail_redirect.module Exp $ |
// $Id: mail_redirect.module Exp $ |
| 3 |
|
|
| 4 |
|
/* TODO New hook_mail implementation |
| 5 |
|
Because of changes to drupal_mail function, you need to move the variables |
| 6 |
|
setup and string replace commands into the hook_mail implementation and then |
| 7 |
|
call drupal_mail with the name of the module which contains this |
| 8 |
|
implementation, the mailkey, the recipient, the language of the user the mail |
| 9 |
|
goes to and some arbitrary parameters. */ |
| 10 |
|
|
| 11 |
|
|
| 12 |
/* redirect any system generated email to your configured email domain |
/* redirect any system generated email to your configured email domain |
| 13 |
* usage: |
* usage: |
| 14 |
* - in your test site's settings.php set: |
* - in your test site's settings.php set: |
| 26 |
/** |
/** |
| 27 |
* Implementation of hook_help(). |
* Implementation of hook_help(). |
| 28 |
*/ |
*/ |
| 29 |
function mail_redirect_help($section='') { |
function mail_redirect_help($path='', $arg) { |
| 30 |
switch ($section) { |
switch ($path) { |
| 31 |
case 'admin/modules#description': |
case 'admin/modules#description': |
| 32 |
return t("Redirects ALL system generated email to a test mail domain. Works with any mail generated |
return t('Redirects ALL system generated email to a test mail domain. Works with any mail generated |
| 33 |
by drupal_mail() or (with included patch applied) any module that uses mimemail. Used for testing |
by drupal_mail() or (with included patch applied) any module that uses mimemail. Used for testing |
| 34 |
mail functions on test sites with real email addresses in the db."); |
mail functions on test sites with real email addresses in the db. Be sure to add: |
| 35 |
|
$conf = array(\'mail_redirect_domain\' => "mydomain.com"); in your settings.php'); |
| 36 |
} |
} |
| 37 |
} |
} |
| 38 |
|
|
| 40 |
/** |
/** |
| 41 |
* Implementation of hook_menu() |
* Implementation of hook_menu() |
| 42 |
*/ |
*/ |
| 43 |
function mail_redirect_menu($may_cache) { |
function mail_redirect_menu() { |
| 44 |
$items = array(); |
$items = array(); |
| 45 |
if ($may_cache) { |
|
| 46 |
$items[] = array( |
$items['admin/settings/mail_redirect'] = array( |
| 47 |
'path' => 'admin/settings/mail_redirect', |
'title' => 'Mail Redirect', |
| 48 |
'title' => t('Mail Redirect'), |
'description' => 'Settings for Mail Redirect module.', |
| 49 |
'description' => t('Settings for Mail Redirect module.'), |
'page callback' => 'drupal_get_form', |
| 50 |
'callback' => 'drupal_get_form', |
'page arguments' => array('mail_redirect_admin_settings'), |
| 51 |
'callback arguments' => array('mail_redirect_admin_settings'), |
'type' => MENU_NORMAL_ITEM, |
| 52 |
'type' => MENU_NORMAL_ITEM, |
); |
|
); |
|
|
} |
|
|
else { |
|
|
|
|
|
} |
|
| 53 |
|
|
| 54 |
return $items; |
return $items; |
| 55 |
} |
} |
| 73 |
* |
* |
| 74 |
* shows a msg to indicate whenever an email has been redirected. |
* shows a msg to indicate whenever an email has been redirected. |
| 75 |
*/ |
*/ |
| 76 |
function mail_redirect_mail_alter(&$mailkey, &$to, &$subject, &$body, &$from, &$headers) { |
function mail_redirect_mail_alter(&$message) { |
| 77 |
|
|
| 78 |
$mydomain = variable_get('mail_redirect_domain', 'NA'); |
$mydomain = variable_get('mail_redirect_domain', 'NA'); |
| 79 |
|
|
| 80 |
if ($mydomain != 'NA') { |
if ($mydomain != 'NA') { |
| 81 |
drupal_set_message(t("The following TO address or list: %to has been redirected to the following TEST DOMAIN: %mydomain", |
drupal_set_message(t("The following TO address or list: %to has been redirected to the following TEST DOMAIN: %mydomain", |
| 82 |
array('%to' => htmlentities($to), '%mydomain' => $mydomain), 'status')); |
array('%to' => $message['to'], '%mydomain' => $mydomain), 'status')); |
| 83 |
|
|
| 84 |
/* need to handle RFC2822 formats for $to: |
/* need to handle RFC2822 formats for $message['to']: |
| 85 |
* user@example.com |
* user@example.com |
| 86 |
* user@example.com, anotheruser@example.com |
* user@example.com, anotheruser@example.com |
| 87 |
* User <user@example.com> |
* User <user@example.com> |
| 88 |
* User <user@example.com>, Another User <anotheruser@example.com> |
* User <user@example.com>, Another User <anotheruser@example.com> |
| 89 |
*/ |
*/ |
| 90 |
|
|
| 91 |
if (stristr($to, ",")) { // then we have a list |
if (stristr($message['to'], ",")) { // then we have a list |
| 92 |
$tos = split(",", $to); |
$tos = split(",", $message['to']); |
| 93 |
array_walk($tos, '_mail_redirect_trim_array_values'); |
array_walk($tos, '_mail_redirect_trim_array_values'); |
| 94 |
} |
} |
| 95 |
else $tos = (array)$to; |
else $tos = (array)$message['to']; |
| 96 |
|
|
| 97 |
foreach ($tos as $key => $to) { |
foreach ($tos as $key => $to) { |
| 98 |
if (stristr($to, "<")) $tos[$key] = preg_replace('^(.*)<((.+)@.+)>^', "$1<$3@$mydomain>", $to); |
if (stristr($to, "<")) $tos[$key] = preg_replace('^(.*)<((.+)@.+)>^', "$1<$3@$mydomain>", $to); |
| 99 |
else $tos[$key] = preg_replace('^((.+)@.+)^', "$2@$mydomain", $to); |
else $tos[$key] = preg_replace('^((.+)@.+)^', "$2@$mydomain", $to); |
| 100 |
} |
} |
| 101 |
|
|
| 102 |
$to = join(",", $tos); |
$message['to'] = join(",", $tos); |
|
|
|
| 103 |
} |
} |
| 104 |
} |
} |
| 105 |
|
|