smtp.admin.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. /**
  3. * @file
  4. * Administrative page code for the smtp module.
  5. */
  6. /**
  7. * Administrative settings.
  8. */
  9. function smtp_admin_settings() {
  10. if (variable_get('smtp_on', 0)) {
  11. drupal_set_message(t('SMTP.module is active.'));
  12. }
  13. else {
  14. drupal_set_message(t('SMTP.module is INACTIVE.'));
  15. }
  16. $logging = variable_get('smtp_debugging', SMTP_LOGGING_ERRORS);
  17. $form['onoff'] = array(
  18. '#type' => 'fieldset',
  19. '#title' => t('Install options'),
  20. );
  21. $form['onoff']['smtp_on'] = array(
  22. '#type' => 'radios',
  23. '#title' => t('Turn this module on or off'),
  24. '#default_value' => variable_get('smtp_on', FALSE),
  25. '#options' => array(1 => t('On'), 0 => t('Off')),
  26. '#description' => t('To uninstall this module you must turn it off here first.'),
  27. );
  28. $form['onoff']['smtp_deliver'] = array(
  29. '#type' => 'radios',
  30. '#title' => t('Turn on delivery of emails'),
  31. '#default_value' => variable_get('smtp_deliver', TRUE),
  32. '#options' => array(1 => t('On'), 0 => t('Off')),
  33. '#description' => t('With this option turned off, email messages will be queued up and processed as normal, but not actually delivered. This option should only be used for testing purposes.'),
  34. );
  35. $form['onoff']['smtp_queue'] = array(
  36. '#type' => 'checkbox',
  37. '#title' => t('Send mail by queue'),
  38. '#default_value' => variable_get('smtp_queue', FALSE),
  39. '#description' => t('Mails will be sent by drupal queue api.'),
  40. );
  41. $form['onoff']['smtp_queue_fail'] = array(
  42. '#type' => 'checkbox',
  43. '#title' => t('Retry sending mail on error.'),
  44. '#default_value' => variable_get('smtp_queue_fail', FALSE),
  45. '#description' => t('Mails will be added to the queue and sent by drupal queue api.'),
  46. );
  47. $form['server'] = array(
  48. '#type' => 'fieldset',
  49. '#title' => t('SMTP server settings'),
  50. );
  51. $form['server']['smtp_host'] = array(
  52. '#type' => 'textfield',
  53. '#title' => t('SMTP server'),
  54. '#default_value' => variable_get('smtp_host', ''),
  55. '#description' => t('The address of your outgoing SMTP server.'),
  56. );
  57. $form['server']['smtp_hostbackup'] = array(
  58. '#type' => 'textfield',
  59. '#title' => t('SMTP backup server'),
  60. '#default_value' => variable_get('smtp_hostbackup', ''),
  61. '#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.'),
  62. );
  63. $form['server']['smtp_port'] = array(
  64. '#type' => 'textfield',
  65. '#title' => t('SMTP port'),
  66. '#size' => 6,
  67. '#maxlength' => 6,
  68. '#default_value' => variable_get('smtp_port', '25'),
  69. '#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'))),
  70. );
  71. // Only display the option if openssl is installed.
  72. if (function_exists('openssl_open')) {
  73. $encryption_options = array(
  74. 'standard' => t('No'),
  75. 'ssl' => t('Use SSL'),
  76. 'tls' => t('Use TLS'),
  77. );
  78. $encryption_description = t('This allows connection to a SMTP server that requires SSL encryption such as Gmail.');
  79. }
  80. // If openssl is not installed, use normal protocol.
  81. else {
  82. variable_set('smtp_protocol', 'standard');
  83. $encryption_options = array('standard' => t('No'));
  84. $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')));
  85. }
  86. $form['server']['smtp_protocol'] = array(
  87. '#type' => 'select',
  88. '#title' => t('Use encrypted protocol'),
  89. '#default_value' => variable_get('smtp_protocol', 'standard'),
  90. '#options' => $encryption_options,
  91. '#description' => $encryption_description,
  92. );
  93. $form['auth'] = array(
  94. '#type' => 'fieldset',
  95. '#title' => t('SMTP Authentication'),
  96. '#description' => t('Leave blank if your SMTP server does not require authentication.'),
  97. );
  98. $form['auth']['smtp_username'] = array(
  99. '#type' => 'textfield',
  100. '#title' => t('Username'),
  101. '#default_value' => variable_get('smtp_username', ''),
  102. '#description' => t('SMTP Username.'),
  103. );
  104. $form['auth']['smtp_password'] = array(
  105. '#type' => 'password',
  106. '#title' => t('Password'),
  107. '#default_value' => variable_get('smtp_password', ''),
  108. '#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.'),
  109. '#attributes' => array(
  110. 'autocomplete' => 'off',
  111. ),
  112. );
  113. $form['email_options'] = array(
  114. '#type' => 'fieldset',
  115. '#title' => t('E-mail options'),
  116. );
  117. $form['email_options']['smtp_from'] = array(
  118. '#type' => 'textfield',
  119. '#title' => t('E-mail from address'),
  120. '#default_value' => variable_get('smtp_from', ''),
  121. '#description' => t('The e-mail address that all e-mails will be from.'),
  122. );
  123. $form['email_options']['smtp_fromname'] = array(
  124. '#type' => 'textfield',
  125. '#title' => t('E-mail from name'),
  126. '#default_value' => variable_get('smtp_fromname', ''),
  127. '#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'),
  128. );
  129. $form['email_options']['smtp_allowhtml'] = array(
  130. '#type' => 'checkbox',
  131. '#title' => t('Allow to send e-mails formatted as Html'),
  132. '#default_value' => variable_get('smtp_allowhtml', 0),
  133. '#description' => t('Checking this box will allow Html formatted e-mails to be sent with the SMTP protocol.'),
  134. );
  135. $form['client'] = array(
  136. '#type' => 'fieldset',
  137. '#title' => t('SMTP client settings'),
  138. );
  139. $form['client']['smtp_client_hostname'] = array(
  140. '#type' => 'textfield',
  141. '#title' => t('Hostname'),
  142. '#default_value' => variable_get('smtp_client_hostname', ''),
  143. '#description' => t('The hostname to use in the Message-Id and Received headers, and as the default HELO string. Leave blank for using %server_name.', array('%server_name' => isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain')),
  144. );
  145. $form['client']['smtp_client_helo'] = array(
  146. '#type' => 'textfield',
  147. '#title' => t('HELO'),
  148. '#default_value' => variable_get('smtp_client_helo', ''),
  149. '#description' => t('The SMTP HELO/EHLO of the message. Defaults to hostname (see above).'),
  150. );
  151. $form['email_test'] = array(
  152. '#type' => 'fieldset',
  153. '#title' => t('Send test e-mail'),
  154. );
  155. $form['email_test']['smtp_test_address'] = array(
  156. '#type' => 'textfield',
  157. '#title' => t('E-mail address to send a test e-mail to'),
  158. '#default_value' => '',
  159. '#description' => t('Type in an address to have a test e-mail sent there.'),
  160. );
  161. $form['debugging'] = array(
  162. '#type' => 'fieldset',
  163. '#title' => t('Debugging and logging'),
  164. );
  165. $logging_options = array(
  166. SMTP_LOGGING_ALL => t('Log everything'),
  167. SMTP_LOGGING_ERRORS => t('Errors only'),
  168. SMTP_LOGGING_NONE => t('No logging'),
  169. );
  170. $form['debugging']['smtp_debugging'] = array(
  171. '#type' => 'select',
  172. '#title' => t('Logging'),
  173. '#options' => $logging_options,
  174. '#default_value' => $logging,
  175. '#description' => t('Choose the appropriate log level. "Log everything" will log errors and informational messages when an email is sent. "Errors only" will only create a log entry when sending failed. "No logging" will disable all logging for this module.'),
  176. );
  177. $form['email_test']['smtp_reroute_address'] = array(
  178. '#type' => 'textfield',
  179. '#title' => t('E-mail address to reroute all emails to'),
  180. '#default_value' => variable_get('smtp_reroute_address', ''),
  181. '#description' => t('All emails sent by the site will be rerouted to this email address; use with caution.'),
  182. );
  183. $form['debugging']['maillog'] = array(
  184. '#type' => 'fieldset',
  185. '#title' => t('Maillog integration'),
  186. );
  187. if (!module_exists('maillog')) {
  188. $form['debugging']['maillog']['#description'] = t('Installing the <a href="@url">Maillog module</a> also allows keeping copies of all emails sent through the site.', array('@url' => 'https://www.drupal.org/project/maillog'));
  189. }
  190. else {
  191. $form['debugging']['maillog']['#description'] = t('The <a href="@url">Maillog module</a> is installed, it can also be used to keep copies of all emails sent through the site.', array('@url' => url('admin/config/development/maillog')));
  192. $form['debugging']['maillog']['maillog_log'] = array(
  193. '#type' => 'checkbox',
  194. '#title' => t("Create table entries in maillog table for each e-mail."),
  195. '#default_value' => variable_get('maillog_log', TRUE),
  196. );
  197. $form['debugging']['maillog']['maillog_devel'] = array(
  198. '#type' => 'checkbox',
  199. '#title' => t("Display the e-mails on page using devel module (if enabled)."),
  200. '#default_value' => variable_get('maillog_devel', TRUE),
  201. '#disabled' => !module_exists('devel'),
  202. );
  203. }
  204. $form['#submit'][] = 'smtp_admin_settings_form_submit';
  205. $form = system_settings_form($form);
  206. $form['#submit'][] = 'smtp_admin_settings_submit_post_system_settings';
  207. return $form;
  208. }
  209. /**
  210. * Validation for the administrative settings form.
  211. */
  212. function smtp_admin_settings_validate($form, &$form_state) {
  213. if ($form_state['values']['smtp_on'] == 1 && $form_state['values']['smtp_host'] == '') {
  214. form_set_error('smtp_host', t('You must enter a SMTP server address.'));
  215. }
  216. if ($form_state['values']['smtp_on'] == 1 && $form_state['values']['smtp_port'] == '') {
  217. form_set_error('smtp_port', t('You must enter a SMTP port number.'));
  218. }
  219. if ($form_state['values']['smtp_from'] && !valid_email_address($form_state['values']['smtp_from'])) {
  220. form_set_error('smtp_from', t('The provided from e-mail address is not valid.'));
  221. }
  222. } // End of smtp_admin_settings_validate().
  223. /**
  224. * Submit handler().
  225. */
  226. function smtp_admin_settings_form_submit($form, &$form_state) {
  227. // Check if SMTP status has been changed.
  228. if (
  229. (!variable_get('smtp_on', FALSE) && $form_state['values']['smtp_on']) ||
  230. (variable_get('smtp_on', FALSE) && !$form_state['values']['smtp_on'])
  231. ) {
  232. $mail_modes = variable_get('mail_system', array('default-system' => 'DefaultMailSystem'));
  233. // Turning on.
  234. if ($form_state['values']['smtp_on']) {
  235. variable_set('smtp_previous_mail_system', $mail_modes['default-system']);
  236. $mail_modes['default-system'] = 'SmtpMailSystem';
  237. }
  238. // Turning off.
  239. else {
  240. $mail_modes['default-system'] = variable_get('smtp_previous_mail_system', 'DefaultMailSystem');
  241. }
  242. variable_set('mail_system', $mail_modes);
  243. }
  244. // If username is set empty, we must set both username/password empty as well.
  245. if (empty($form_state['values']['smtp_username'])) {
  246. $form_state['values']['smtp_password'] = '';
  247. }
  248. // A little hack. When form is presentend, the password is not shown (Drupal
  249. // way of doing). So, if user submits the form without changing the password,
  250. // we must prevent it from being reset.
  251. elseif (empty($form_state['values']['smtp_password'])) {
  252. unset($form_state['values']['smtp_password']);
  253. }
  254. // Save the test address to send an email after all the settings have been
  255. // updated.
  256. $form_state['storage']['smtp']['smtp_test_address'] = $form_state['values']['smtp_test_address'];
  257. unset($form_state['values']['smtp_test_address']);
  258. }
  259. /**
  260. * Submit handler for the administrative settings form containing all
  261. * functionality to be run after system_settings_form_submit.
  262. */
  263. function smtp_admin_settings_submit_post_system_settings($form, &$form_state) {
  264. // If an address was given, send a test e-mail message.
  265. $test_address = $form_state['storage']['smtp']['smtp_test_address'];
  266. if ($test_address != '') {
  267. $language = language_default();
  268. $params['subject'] = t('Drupal SMTP test e-mail');
  269. $params['body'] = array(t('If you receive this message it means your site is capable of using SMTP to send e-mail.'));
  270. drupal_mail('smtp', 'smtp-test', $test_address, $language, $params);
  271. 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'))));
  272. }
  273. }