smtp.admin.inc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * @file
  4. * Administrative page code for the smtp module.
  5. *
  6. */
  7. /**
  8. * Administrative settings.
  9. *
  10. * @return
  11. * An array containing form items to place on the module settings page.
  12. */
  13. function smtp_admin_settings() {
  14. // Override the smtp_library variable.
  15. if (module_exists('mimemail') &&
  16. strpos(variable_get('smtp_library', ''), 'mimemail')) {
  17. // don't touch smtp_library
  18. }
  19. else {
  20. if (variable_get('smtp_on', 0)) {
  21. $smtp_path = drupal_get_filename('module', 'smtp');
  22. if ($smtp_path) {
  23. variable_set('smtp_library', $smtp_path);
  24. drupal_set_message(t('SMTP.module is active.'));
  25. }
  26. // If drupal can't find the path to the module, display an error.
  27. else {
  28. drupal_set_message(t("SMTP.module error: Can't find file."), 'error');
  29. }
  30. }
  31. // If this module is turned off, delete the variable.
  32. else {
  33. variable_del('smtp_library');
  34. drupal_set_message(t('SMTP.module is INACTIVE.'));
  35. }
  36. }
  37. $form['onoff'] = array(
  38. '#type' => 'fieldset',
  39. '#title' => t('Install options'),
  40. );
  41. $form['onoff']['smtp_on'] = array(
  42. '#type' => 'radios',
  43. '#title' => t('Turn this module on or off'),
  44. '#default_value' => variable_get('smtp_on', 0),
  45. '#options' => array(1 => t('On'), 0 => t('Off')),
  46. '#description' => t('To uninstall this module you must turn it off here first.'),
  47. );
  48. $form['onoff']['smtp_queue'] = array(
  49. '#type' => 'checkbox',
  50. '#title' => t('Send mail by queue'),
  51. '#default_value' => variable_get('smtp_queue', FALSE),
  52. '#description' => t('Mails will be sent by drupal queue api.'),
  53. );
  54. $form['server'] = array(
  55. '#type' => 'fieldset',
  56. '#title' => t('SMTP server settings'),
  57. );
  58. $form['server']['smtp_host'] = array(
  59. '#type' => 'textfield',
  60. '#title' => t('SMTP server'),
  61. '#default_value' => variable_get('smtp_host', ''),
  62. '#description' => t('The address of your outgoing SMTP server.'),
  63. );
  64. $form['server']['smtp_hostbackup'] = array(
  65. '#type' => 'textfield',
  66. '#title' => t('SMTP backup server'),
  67. '#default_value' => variable_get('smtp_hostbackup', ''),
  68. '#description' => t('The address of your outgoing SMTP backup server. If the primary server can\'t be found this one will be tried. This is optional.'),
  69. );
  70. $form['server']['smtp_port'] = array(
  71. '#type' => 'textfield',
  72. '#title' => t('SMTP port'),
  73. '#size' => 6,
  74. '#maxlength' => 6,
  75. '#default_value' => variable_get('smtp_port', '25'),
  76. '#description' => t('The default SMTP port is 25, if that is being blocked try 80. Gmail uses 465. See !url for more information on configuring for use with Gmail.', array('!url' => l(t('this page'), 'http://gmail.google.com/support/bin/answer.py?answer=13287'))),
  77. );
  78. // Only display the option if openssl is installed.
  79. if (function_exists('openssl_open')) {
  80. $encryption_options = array(
  81. 'standard' => t('No'),
  82. 'ssl' => t('Use SSL'),
  83. 'tls' => t('Use TLS'),
  84. );
  85. $encryption_description = t('This allows connection to an SMTP server that requires SSL encryption such as Gmail.');
  86. }
  87. // If openssl is not installed, use normal protocol.
  88. else {
  89. variable_set('smtp_protocol', 'standard');
  90. $encryption_options = array('standard' => t('No'));
  91. $encryption_description = t('Your PHP installation does not have SSL enabled. See the !url page on php.net for more information. Gmail requires SSL.', array('!url' => l(t('OpenSSL Functions'), 'http://php.net/openssl')));
  92. }
  93. $form['server']['smtp_protocol'] = array(
  94. '#type' => 'select',
  95. '#title' => t('Use encrypted protocol'),
  96. '#default_value' => variable_get('smtp_protocol', 'standard'),
  97. '#options' => $encryption_options,
  98. '#description' => $encryption_description,
  99. );
  100. $form['auth'] = array(
  101. '#type' => 'fieldset',
  102. '#title' => t('SMTP Authentication'),
  103. '#description' => t('Leave blank if your SMTP server does not require authentication.'),
  104. );
  105. $form['auth']['smtp_username'] = array(
  106. '#type' => 'textfield',
  107. '#title' => t('Username'),
  108. '#default_value' => variable_get('smtp_username', ''),
  109. '#description' => t('SMTP Username.'),
  110. );
  111. $form['auth']['smtp_password'] = array(
  112. '#type' => 'password',
  113. '#title' => t('Password'),
  114. '#default_value' => variable_get('smtp_password', ''),
  115. '#description' => t('SMTP password. If you have already entered your password before, you should leave this field blank, unless you want to change the stored password.'),
  116. );
  117. $form['email_options'] = array(
  118. '#type' => 'fieldset',
  119. '#title' => t('E-mail options'),
  120. );
  121. $form['email_options']['smtp_from'] = array(
  122. '#type' => 'textfield',
  123. '#title' => t('E-mail from address'),
  124. '#default_value' => variable_get('smtp_from', ''),
  125. '#description' => t('The e-mail address that all e-mails will be from.'),
  126. );
  127. $form['email_options']['smtp_fromname'] = array(
  128. '#type' => 'textfield',
  129. '#title' => t('E-mail from name'),
  130. '#default_value' => variable_get('smtp_fromname', ''),
  131. '#description' => t('The name that all e-mails will be from. If left blank will use the site name of:') . ' ' . variable_get('site_name', 'Drupal powered site'),
  132. );
  133. $form['email_options']['smtp_allowhtml'] = array(
  134. '#type' => 'checkbox',
  135. '#title' => t('Allow to send e-mails formated as Html'),
  136. '#default_value' => variable_get('smtp_allowhtml', 0),
  137. '#description' => t('Checking this box will allow Html formated e-mails to be sent with the SMTP protocol.'),
  138. );
  139. // If an address was given, send a test e-mail message.
  140. $test_address = variable_get('smtp_test_address', '');
  141. if ($test_address != '') {
  142. // Clear the variable so only one message is sent.
  143. variable_del('smtp_test_address');
  144. global $language;
  145. $params['subject'] = t('Drupal SMTP test e-mail');
  146. $params['body'] = array(t('If you receive this message it means your site is capable of using SMTP to send e-mail.'));
  147. drupal_mail('smtp', 'smtp-test', $test_address, $language, $params);
  148. drupal_set_message(t('A test e-mail has been sent to @email. You may want to !check for any error messages.', array('@email' => $test_address, '!check' => l(t('check the logs'), 'admin/reports/dblog'))));
  149. }
  150. $form['email_test'] = array(
  151. '#type' => 'fieldset',
  152. '#title' => t('Send test e-mail'),
  153. );
  154. $form['email_test']['smtp_test_address'] = array(
  155. '#type' => 'textfield',
  156. '#title' => t('E-mail address to send a test e-mail to'),
  157. '#default_value' => '',
  158. '#description' => t('Type in an address to have a test e-mail sent there.'),
  159. );
  160. $form['smtp_debugging'] = array(
  161. '#type' => 'checkbox',
  162. '#title' => t('Enable debugging'),
  163. '#default_value' => variable_get('smtp_debugging', 0),
  164. '#description' => t('Checking this box will print SMTP messages from the server for every e-mail that is sent.'),
  165. );
  166. $form['#submit'][] = 'smtp_admin_settings_form_submit';
  167. return system_settings_form($form);
  168. } // End of smtp_admin_settings().
  169. /**
  170. * Validation for the administrative settings form.
  171. *
  172. * @param form
  173. * An associative array containing the structure of the form.
  174. * @param form_state
  175. * A keyed array containing the current state of the form.
  176. */
  177. function smtp_admin_settings_validate($form, &$form_state) {
  178. if ($form_state['values']['smtp_on'] == 1 && $form_state['values']['smtp_host'] == '') {
  179. form_set_error('smtp_host', t('You must enter an SMTP server address.'));
  180. }
  181. if ($form_state['values']['smtp_on'] == 1 && $form_state['values']['smtp_port'] == '') {
  182. form_set_error('smtp_port', t('You must enter an SMTP port number.'));
  183. }
  184. if ($form_state['values']['smtp_from'] && !valid_email_address($form_state['values']['smtp_from'])) {
  185. form_set_error('smtp_from', t('The provided from e-mail address is not valid.'));
  186. }
  187. // If username is set empty, we must set both username/password empty as well.
  188. if (empty($form_state['values']['smtp_username'])) {
  189. $form_state['values']['smtp_password'] = '';
  190. }
  191. // A little hack. When form is presentend, the password is not shown (Drupal way of doing).
  192. // So, if user submits the form without changing the password, we must prevent it from being reset.
  193. elseif (empty($form_state['values']['smtp_password'])) {
  194. unset($form_state['values']['smtp_password']);
  195. }
  196. } // End of smtp_admin_settings_validate().
  197. /**
  198. * Submit handler().
  199. */
  200. function smtp_admin_settings_form_submit($form, &$form_state) {
  201. // Check if SMTP status has been changed.
  202. if (
  203. (!variable_get('smtp_on', FALSE) && $form_state['values']['smtp_on']) ||
  204. (variable_get('smtp_on', FALSE) && !$form_state['values']['smtp_on'])
  205. ) {
  206. $mail_modes = variable_get('mail_system', array('default-system' => 'DefaultMailSystem'));
  207. // Turning on.
  208. if ($form_state['values']['smtp_on']) {
  209. variable_set('smtp_previous_mail_system', $mail_modes['default-system']);
  210. $mail_modes['default-system'] = 'SmtpMailSystem';
  211. }
  212. // Turning off.
  213. else {
  214. $mail_modes['default-system'] = variable_get('smtp_previous_mail_system', 'DefaultMailSystem');
  215. }
  216. variable_set('mail_system', $mail_modes);
  217. }
  218. }