smtp.admin.inc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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['server'] = array(
  49. '#type' => 'fieldset',
  50. '#title' => t('SMTP server settings'),
  51. );
  52. $form['server']['smtp_host'] = array(
  53. '#type' => 'textfield',
  54. '#title' => t('SMTP server'),
  55. '#default_value' => variable_get('smtp_host', ''),
  56. '#description' => t('The address of your outgoing SMTP server.'),
  57. );
  58. $form['server']['smtp_hostbackup'] = array(
  59. '#type' => 'textfield',
  60. '#title' => t('SMTP backup server'),
  61. '#default_value' => variable_get('smtp_hostbackup', ''),
  62. '#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.'),
  63. );
  64. $form['server']['smtp_port'] = array(
  65. '#type' => 'textfield',
  66. '#title' => t('SMTP port'),
  67. '#size' => 6,
  68. '#maxlength' => 6,
  69. '#default_value' => variable_get('smtp_port', '25'),
  70. '#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'))),
  71. );
  72. // Only display the option if openssl is installed.
  73. if (function_exists('openssl_open')) {
  74. $encryption_options = array(
  75. 'standard' => t('No'),
  76. 'ssl' => t('Use SSL'),
  77. 'tls' => t('Use TLS'),
  78. );
  79. $encryption_description = t('This allows connection to an SMTP server that requires SSL encryption such as Gmail.');
  80. }
  81. // If openssl is not installed, use normal protocol.
  82. else {
  83. variable_set('smtp_protocol', 'standard');
  84. $encryption_options = array('standard' => t('No'));
  85. $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')));
  86. }
  87. $form['server']['smtp_protocol'] = array(
  88. '#type' => 'select',
  89. '#title' => t('Use encrypted protocol'),
  90. '#default_value' => variable_get('smtp_protocol', 'standard'),
  91. '#options' => $encryption_options,
  92. '#description' => $encryption_description,
  93. );
  94. $form['auth'] = array(
  95. '#type' => 'fieldset',
  96. '#title' => t('SMTP Authentication'),
  97. '#description' => t('Leave blank if your SMTP server does not require authentication.'),
  98. );
  99. $form['auth']['smtp_username'] = array(
  100. '#type' => 'textfield',
  101. '#title' => t('Username'),
  102. '#default_value' => variable_get('smtp_username', ''),
  103. '#description' => t('SMTP Username.'),
  104. );
  105. $form['auth']['smtp_password'] = array(
  106. '#type' => 'password',
  107. '#title' => t('Password'),
  108. '#default_value' => variable_get('smtp_password', ''),
  109. '#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.'),
  110. );
  111. $form['email_options'] = array(
  112. '#type' => 'fieldset',
  113. '#title' => t('E-mail options'),
  114. );
  115. $form['email_options']['smtp_from'] = array(
  116. '#type' => 'textfield',
  117. '#title' => t('E-mail from address'),
  118. '#default_value' => variable_get('smtp_from', ''),
  119. '#description' => t('The e-mail address that all e-mails will be from.'),
  120. );
  121. $form['email_options']['smtp_fromname'] = array(
  122. '#type' => 'textfield',
  123. '#title' => t('E-mail from name'),
  124. '#default_value' => variable_get('smtp_fromname', ''),
  125. '#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'),
  126. );
  127. $form['email_options']['smtp_allowhtml'] = array(
  128. '#type' => 'checkbox',
  129. '#title' => t('Allow to send e-mails formated as Html'),
  130. '#default_value' => variable_get('smtp_allowhtml', 0),
  131. '#description' => t('Checking this box will allow Html formated e-mails to be sent with the SMTP protocol.'),
  132. );
  133. // If an address was given, send a test e-mail message.
  134. $test_address = variable_get('smtp_test_address', '');
  135. if ($test_address != '') {
  136. // Clear the variable so only one message is sent.
  137. variable_del('smtp_test_address');
  138. global $language;
  139. $params['subject'] = t('Drupal SMTP test e-mail');
  140. $params['body'] = array(t('If you receive this message it means your site is capable of using SMTP to send e-mail.'));
  141. drupal_mail('smtp', 'smtp-test', $test_address, $language, $params);
  142. 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'))));
  143. }
  144. $form['email_test'] = array(
  145. '#type' => 'fieldset',
  146. '#title' => t('Send test e-mail'),
  147. );
  148. $form['email_test']['smtp_test_address'] = array(
  149. '#type' => 'textfield',
  150. '#title' => t('E-mail address to send a test e-mail to'),
  151. '#default_value' => '',
  152. '#description' => t('Type in an address to have a test e-mail sent there.'),
  153. );
  154. $form['smtp_debugging'] = array(
  155. '#type' => 'checkbox',
  156. '#title' => t('Enable debugging'),
  157. '#default_value' => variable_get('smtp_debugging', 0),
  158. '#description' => t('Checking this box will print SMTP messages from the server for every e-mail that is sent.'),
  159. );
  160. return system_settings_form($form);
  161. } // End of smtp_admin_settings().
  162. /**
  163. * Validation for the administrative settings form.
  164. *
  165. * @param form
  166. * An associative array containing the structure of the form.
  167. * @param form_state
  168. * A keyed array containing the current state of the form.
  169. */
  170. function smtp_admin_settings_validate($form, &$form_state) {
  171. if ($form_state['values']['smtp_on'] == 1 && $form_state['values']['smtp_host'] == '') {
  172. form_set_error('smtp_host', t('You must enter an SMTP server address.'));
  173. }
  174. if ($form_state['values']['smtp_on'] == 1 && $form_state['values']['smtp_port'] == '') {
  175. form_set_error('smtp_port', t('You must enter an SMTP port number.'));
  176. }
  177. if ($form_state['values']['smtp_from'] && !valid_email_address($form_state['values']['smtp_from'])) {
  178. form_set_error('smtp_from', t('The provided from e-mail address is not valid.'));
  179. }
  180. // If username is set empty, we must set both username/password empty as well.
  181. if (empty($form_state['values']['smtp_username'])) {
  182. $form_state['values']['smtp_password'] = '';
  183. }
  184. // A little hack. When form is presentend, the password is not shown (Drupal way of doing).
  185. // So, if user submits the form without changing the password, we must prevent it from being reset.
  186. elseif (empty($form_state['values']['smtp_password'])) {
  187. unset($form_state['values']['smtp_password']);
  188. }
  189. } // End of smtp_admin_settings_validate().