reroute_email.admin.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * @file
  4. * Reroute Email admin configuration functions.
  5. */
  6. /**
  7. * Settings form.
  8. */
  9. function reroute_email_settings() {
  10. $form[REROUTE_EMAIL_ENABLE] = array(
  11. '#type' => 'checkbox',
  12. '#title' => t('Enable rerouting'),
  13. '#default_value' => variable_get(REROUTE_EMAIL_ENABLE, 0),
  14. '#description' => t('Check this box if you want to enable email rerouting. Uncheck to disable rerouting.'),
  15. );
  16. $form[REROUTE_EMAIL_ADDRESS] = array(
  17. '#type' => 'textfield',
  18. '#title' => t('Email addresses'),
  19. '#default_value' => variable_get(REROUTE_EMAIL_ADDRESS, variable_get('site_mail', ini_get('sendmail_from'))),
  20. '#description' => t('Provide a space, comma, or semicolon-delimited list of email addresses to pass through. Every destination email address which is not on this list will be rerouted to the first address on the list.'),
  21. '#states' => array(
  22. 'visible' => array(':input[name=reroute_email_enable]' => array('checked' => TRUE)),
  23. ),
  24. );
  25. $form[REROUTE_EMAIL_ENABLE_MESSAGE] = array(
  26. '#type' => 'checkbox',
  27. '#title' => t('Show rerouting description in mail body'),
  28. '#default_value' => variable_get(REROUTE_EMAIL_ENABLE_MESSAGE, 1),
  29. '#description' => t('Check this box if you want a message to be inserted into the email body when the mail is being rerouted. Otherwise, SMTP headers will be used to describe the rerouting. If sending rich-text email, leave this unchecked so that the body of the email will not be disturbed.'),
  30. '#states' => array(
  31. 'visible' => array(':input[name=reroute_email_enable]' => array('checked' => TRUE)),
  32. ),
  33. );
  34. return system_settings_form($form);
  35. }
  36. /**
  37. * Validation callback for reroute_email_settings() form.
  38. */
  39. function reroute_email_settings_validate($form, $form_state) {
  40. if ($form_state['values']['reroute_email_enable'] == TRUE) {
  41. // Allow splitting emails by space, comma, semicolon.
  42. $addresslist = preg_split(REROUTE_EMAIL_EMAIL_SPLIT_RE, $form_state['values']['reroute_email_address'], -1, PREG_SPLIT_NO_EMPTY);
  43. foreach ($addresslist as $address) {
  44. if (!valid_email_address($address)) {
  45. form_set_error('reroute_email_address', t('@address is not a valid email address', array('@address' => $address)));
  46. }
  47. }
  48. }
  49. }
  50. /**
  51. * Form for sending test messages.
  52. */
  53. function reroute_email_test_email_form() {
  54. return array(
  55. 'addresses' => array(
  56. '#type' => 'fieldset',
  57. '#description' => t('Email addresses are not validated: any valid or invalid email address format could be submitted.'),
  58. 'to' => array(
  59. '#type' => 'textfield',
  60. '#title' => t('To'),
  61. '#required' => TRUE,
  62. ),
  63. 'cc' => array(
  64. '#type' => 'textfield',
  65. '#title' => t('Cc'),
  66. ),
  67. 'bcc' => array(
  68. '#type' => 'textfield',
  69. '#title' => t('Bcc'),
  70. ),
  71. ),
  72. 'subject' => array(
  73. '#type' => 'textfield',
  74. '#title' => t('Subject'),
  75. '#default_value' => t('Reroute Email Test'),
  76. ),
  77. 'body' => array(
  78. '#type' => 'textarea',
  79. '#title' => t('Body'),
  80. ),
  81. 'submit' => array(
  82. '#type' => 'submit',
  83. '#value' => t('Send email'),
  84. ),
  85. );
  86. }
  87. /**
  88. * Submit handler for email test.
  89. */
  90. function reroute_email_test_email_form_submit(&$form, &$form_state) {
  91. $to = $form_state['values']['to'];
  92. $param_keys = array('cc', 'bcc', 'subject', 'body');
  93. $params = array_intersect_key($form_state['values'], array_flip($param_keys));
  94. $message = drupal_mail('reroute_email', 'test_email_form', $to, language_default(), $params);
  95. if (!empty($message['result'])) {
  96. drupal_set_message(t("Test email submitted for delivery."));
  97. }
  98. }