reroute_email.module 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * @file
  4. * Reroute Email module
  5. */
  6. define('REROUTE_EMAIL_ADDRESS', 'reroute_email_address');
  7. define('REROUTE_EMAIL_ENABLE_MESSAGE', 'reroute_email_enable_message');
  8. define('REROUTE_EMAIL_ENABLE', 'reroute_email_enable');
  9. // Regular expression used to split email addresses provided in form.
  10. // This allows the use of any number of spaces, commas, or semicolons
  11. // between email addresses.
  12. define('REROUTE_EMAIL_EMAIL_SPLIT_RE', '/[\s,;]+/');
  13. /**
  14. * Implements of hook_permission().
  15. */
  16. function reroute_email_permission() {
  17. return array(
  18. 'administer reroute email' => array(
  19. 'title' => t('Administer Reroute Email'),
  20. 'description' => t('Administer the Reroute Email module.'),
  21. ),
  22. );
  23. }
  24. /**
  25. * Implements hook_menu().
  26. */
  27. function reroute_email_menu() {
  28. $items = array();
  29. $items['admin/config/development/reroute_email'] = array(
  30. 'title' => 'Reroute Email',
  31. 'description' => 'Reroute emails to a test address.',
  32. 'page callback' => 'drupal_get_form',
  33. 'page arguments' => array('reroute_email_settings'),
  34. 'access arguments' => array('administer reroute email'),
  35. 'file' => 'reroute_email.admin.inc',
  36. );
  37. $items['admin/config/development/reroute_email/settings'] = array(
  38. 'title' => 'Settings',
  39. 'type' => MENU_DEFAULT_LOCAL_TASK,
  40. 'weight' => -10,
  41. );
  42. $items['admin/config/development/reroute_email/test'] = array(
  43. 'title' => 'Test email form',
  44. 'type' => MENU_LOCAL_TASK,
  45. 'description' => 'Form for sending test email.',
  46. 'page callback' => 'drupal_get_form',
  47. 'page arguments' => array('reroute_email_test_email_form'),
  48. 'access arguments' => array('administer reroute email'),
  49. 'file' => 'reroute_email.admin.inc',
  50. );
  51. return $items;
  52. }
  53. /**
  54. * Implements hook_mail_alter().
  55. *
  56. * This hook is required to change the destination of outgoing emails.
  57. */
  58. function reroute_email_mail_alter(&$message) {
  59. if (variable_get(REROUTE_EMAIL_ENABLE, 0)) {
  60. global $base_url;
  61. if (!variable_get(REROUTE_EMAIL_ADDRESS, '')) {
  62. // If email address not in settings, then do nothing.
  63. return;
  64. }
  65. if (empty($message)) {
  66. return;
  67. }
  68. if (!is_array($message)) {
  69. return;
  70. }
  71. $mailkey = isset($message['id']) ? $message['id'] : t('[mail id] is missing');
  72. $to = isset($message['to']) ? $message['to'] : t('[to] is missing');
  73. $message['headers']['X-Rerouted-Mail-Key'] = $mailkey;
  74. $message['headers']['X-Rerouted-Website'] = $base_url;
  75. // Unset Bcc and Cc fields to prevent emails from going to those addresses.
  76. if (isset($message['headers']) && is_array($message['headers'])) {
  77. // Ensure we catch all Cc and Bcc headers, regardless of case,
  78. // and protecting against multiple instances of the "same" header.
  79. $header_keys = array();
  80. foreach (array_keys($message['headers']) as $key) {
  81. $header_keys[strtolower($key)][] = $key;
  82. }
  83. if (!empty($header_keys['cc'])) {
  84. foreach ($header_keys['cc'] as $header) {
  85. $message['headers']['X-Rerouted-Original-Cc'] = $message['headers'][$header];
  86. unset($message['headers'][$header]);
  87. }
  88. }
  89. if (!empty($header_keys['bcc'])) {
  90. foreach ($header_keys['bcc'] as $header) {
  91. $message['headers']['X-Rerouted-Original-Bcc'] = $message['headers'][$header];
  92. unset($message['headers'][$header]);
  93. }
  94. }
  95. }
  96. // Split the address string on whitespace, ignoring any empty results.
  97. $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);
  98. if (!in_array($to, $addresslist)) {
  99. // Not on the list, so reroute to the first address in the list.
  100. $message['headers']['X-Rerouted-Original-To'] = $to;
  101. $message['to'] = $addresslist[0];
  102. if (variable_get(REROUTE_EMAIL_ENABLE_MESSAGE, 1)) {
  103. // Format a message to show at the top.
  104. $msg = t("This email was rerouted.") . "\n";
  105. $msg .= t("Web site: @site", array('@site' => $base_url)) . "\n";
  106. $msg .= t("Mail key: @key", array('@key' => $mailkey)) . "\n";
  107. $msg .= t("Originally to: @to", array('@to' => $to)) . "\n";
  108. $msg .= "-----------------------\n";
  109. // Prepend explanation message to the body of the email. This must be
  110. // handled differently depending on whether the body came in as a
  111. // string or an array. If it came in as a string (despite the fact it
  112. // should be an array) we'll respect that and leave it as a string.
  113. if (is_string($message['body'])) {
  114. $message['body'] = $msg . $message['body'];
  115. }
  116. else {
  117. array_unshift($message['body'], $msg);
  118. }
  119. }
  120. }
  121. }
  122. }
  123. /**
  124. * Implements hook_mail().
  125. */
  126. function reroute_email_mail($key, &$message, $params) {
  127. if ($key != 'test_email_form') {
  128. return;
  129. }
  130. $message['headers']['Cc'] = $params['cc'];
  131. $message['headers']['Bcc'] = $params['bcc'];
  132. $message['subject'] = $params['subject'];
  133. $message['body'][] = $params['body'];
  134. }