smtp.admin.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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['server']['smtp_ssl_context'] = array(
  94. '#type' => 'fieldset',
  95. '#title' => t('SSL Context Options'),
  96. '#description' => t('Be careful when adjusting following options. See more details !url.', array('!url' => l(t('here'), 'https://secure.php.net/manual/en/context.ssl.php'))),
  97. '#states' => array(
  98. 'invisible' => array(
  99. ':input[name="smtp_protocol"]' => array('value' => t('standard')),
  100. ),
  101. ),
  102. );
  103. $form['server']['smtp_ssl_context']['smtp_verify_peer'] = array(
  104. '#type' => 'checkbox',
  105. '#title' => t('Verify SMTP server SSL certificate'),
  106. '#default_value' => variable_get('smtp_verify_peer', version_compare(phpversion(), '5.6.0', '>=')),
  107. '#description' => t('This option is enabled by default from PHP 5.6.0. It checks whether SMTP server certificate is valid.'),
  108. '#states' => array(
  109. 'invisible' => array(
  110. ':input[name="smtp_protocol"]' => array('value' => t('standard')),
  111. ),
  112. ),
  113. );
  114. if (version_compare(phpversion(), '5.6.0', '>=')) {
  115. $form['server']['smtp_ssl_context']['smtp_verify_peer_name'] = array(
  116. '#type' => 'checkbox',
  117. '#title' => t('Verify SMTP server SSL certificate CN'),
  118. '#default_value' => variable_get('smtp_verify_peer_name', TRUE),
  119. '#description' => t('This option is introduced in PHP 5.6.0 and is enabled by default. It checks whether SMTP server certificate CN matches the SMTP server you specify above.'),
  120. '#states' => array(
  121. 'invisible' => array(
  122. ':input[name="smtp_protocol"]' => array('value' => t('standard')),
  123. ),
  124. ),
  125. );
  126. }
  127. $form['server']['smtp_ssl_context']['smtp_allow_self_signed'] = array(
  128. '#type' => 'checkbox',
  129. '#title' => t('Allow SMTP server to use self-signed certificate'),
  130. '#default_value' => variable_get('smtp_allow_self_signed', FALSE),
  131. '#description' => t('This option is disabled by default. It only takes effect when option <b>Verify SMTP server SSL certificate</b> is enabled.'),
  132. '#states' => array(
  133. 'invisible' => array(
  134. ':input[name="smtp_protocol"]' => array('value' => t('standard')),
  135. ),
  136. ),
  137. );
  138. $form['auth'] = array(
  139. '#type' => 'fieldset',
  140. '#title' => t('SMTP Authentication'),
  141. '#description' => t('Leave blank if your SMTP server does not require authentication.'),
  142. );
  143. $form['auth']['smtp_username'] = array(
  144. '#type' => 'textfield',
  145. '#title' => t('Username'),
  146. '#default_value' => variable_get('smtp_username', ''),
  147. '#description' => t('SMTP Username.'),
  148. );
  149. $form['auth']['smtp_password'] = array(
  150. '#type' => 'password',
  151. '#title' => t('Password'),
  152. '#default_value' => variable_get('smtp_password', ''),
  153. '#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.'),
  154. '#attributes' => array(
  155. 'autocomplete' => 'off',
  156. ),
  157. );
  158. $form['email_options'] = array(
  159. '#type' => 'fieldset',
  160. '#title' => t('E-mail options'),
  161. );
  162. $form['email_options']['smtp_from'] = array(
  163. '#type' => 'textfield',
  164. '#title' => t('E-mail from address'),
  165. '#default_value' => variable_get('smtp_from', ''),
  166. '#description' => t('The e-mail address that all e-mails will be from.'),
  167. );
  168. $form['email_options']['smtp_fromname'] = array(
  169. '#type' => 'textfield',
  170. '#title' => t('E-mail from name'),
  171. '#default_value' => variable_get('smtp_fromname', ''),
  172. '#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'),
  173. );
  174. $form['email_options']['smtp_allowhtml'] = array(
  175. '#type' => 'checkbox',
  176. '#title' => t('Allow to send e-mails formatted as Html'),
  177. '#default_value' => variable_get('smtp_allowhtml', 0),
  178. '#description' => t('Checking this box will allow Html formatted e-mails to be sent with the SMTP protocol.'),
  179. );
  180. $form['client'] = array(
  181. '#type' => 'fieldset',
  182. '#title' => t('SMTP client settings'),
  183. );
  184. $form['client']['smtp_client_hostname'] = array(
  185. '#type' => 'textfield',
  186. '#title' => t('Hostname'),
  187. '#default_value' => variable_get('smtp_client_hostname', ''),
  188. '#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')),
  189. );
  190. $form['client']['smtp_client_helo'] = array(
  191. '#type' => 'textfield',
  192. '#title' => t('HELO'),
  193. '#default_value' => variable_get('smtp_client_helo', ''),
  194. '#description' => t('The SMTP HELO/EHLO of the message. Defaults to hostname (see above).'),
  195. );
  196. $form['email_test'] = array(
  197. '#type' => 'fieldset',
  198. '#title' => t('Send test e-mail'),
  199. );
  200. $form['email_test']['smtp_test_address'] = array(
  201. '#type' => 'textfield',
  202. '#title' => t('E-mail address to send a test e-mail to'),
  203. '#default_value' => '',
  204. '#description' => t('Type in an address to have a test e-mail sent there.'),
  205. );
  206. $form['debugging'] = array(
  207. '#type' => 'fieldset',
  208. '#title' => t('Debugging and logging'),
  209. );
  210. $logging_options = array(
  211. SMTP_LOGGING_ALL => t('Log everything'),
  212. SMTP_LOGGING_ERRORS => t('Errors only'),
  213. SMTP_LOGGING_NONE => t('No logging'),
  214. );
  215. $form['debugging']['smtp_debugging'] = array(
  216. '#type' => 'select',
  217. '#title' => t('Logging'),
  218. '#options' => $logging_options,
  219. '#default_value' => $logging,
  220. '#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.'),
  221. );
  222. $form['email_test']['smtp_reroute_address'] = array(
  223. '#type' => 'textfield',
  224. '#title' => t('E-mail address to reroute all emails to'),
  225. '#default_value' => variable_get('smtp_reroute_address', ''),
  226. '#description' => t('All emails sent by the site will be rerouted to this email address; use with caution.'),
  227. );
  228. $form['debugging']['maillog'] = array(
  229. '#type' => 'fieldset',
  230. '#title' => t('Maillog integration'),
  231. );
  232. if (!module_exists('maillog')) {
  233. $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'));
  234. }
  235. else {
  236. $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')));
  237. $form['debugging']['maillog']['maillog_log'] = array(
  238. '#type' => 'checkbox',
  239. '#title' => t("Create table entries in maillog table for each e-mail."),
  240. '#default_value' => variable_get('maillog_log', TRUE),
  241. );
  242. $form['debugging']['maillog']['maillog_devel'] = array(
  243. '#type' => 'checkbox',
  244. '#title' => t("Display the e-mails on page using devel module (if enabled)."),
  245. '#default_value' => variable_get('maillog_devel', TRUE),
  246. '#disabled' => !module_exists('devel'),
  247. );
  248. }
  249. $form['#submit'][] = 'smtp_admin_settings_form_submit';
  250. $form = system_settings_form($form);
  251. $form['#submit'][] = 'smtp_admin_settings_submit_post_system_settings';
  252. return $form;
  253. }
  254. /**
  255. * Validation for the administrative settings form.
  256. */
  257. function smtp_admin_settings_validate($form, &$form_state) {
  258. if ($form_state['values']['smtp_on'] == 1 && $form_state['values']['smtp_host'] == '') {
  259. form_set_error('smtp_host', t('You must enter a SMTP server address.'));
  260. }
  261. if ($form_state['values']['smtp_on'] == 1 && $form_state['values']['smtp_port'] == '') {
  262. form_set_error('smtp_port', t('You must enter a SMTP port number.'));
  263. }
  264. if ($form_state['values']['smtp_from'] && !valid_email_address($form_state['values']['smtp_from'])) {
  265. form_set_error('smtp_from', t('The provided from e-mail address is not valid.'));
  266. }
  267. } // End of smtp_admin_settings_validate().
  268. /**
  269. * Submit handler().
  270. */
  271. function smtp_admin_settings_form_submit($form, &$form_state) {
  272. // Check if SMTP status has been changed.
  273. if (
  274. (!variable_get('smtp_on', FALSE) && $form_state['values']['smtp_on']) ||
  275. (variable_get('smtp_on', FALSE) && !$form_state['values']['smtp_on'])
  276. ) {
  277. $mail_modes = variable_get('mail_system', array('default-system' => 'DefaultMailSystem'));
  278. // Turning on.
  279. if ($form_state['values']['smtp_on']) {
  280. variable_set('smtp_previous_mail_system', $mail_modes['default-system']);
  281. $mail_modes['default-system'] = 'SmtpMailSystem';
  282. }
  283. // Turning off.
  284. else {
  285. $mail_modes['default-system'] = variable_get('smtp_previous_mail_system', 'DefaultMailSystem');
  286. }
  287. variable_set('mail_system', $mail_modes);
  288. }
  289. // If username is set empty, we must set both username/password empty as well.
  290. if (empty($form_state['values']['smtp_username'])) {
  291. $form_state['values']['smtp_password'] = '';
  292. }
  293. // A little hack. When form is presentend, the password is not shown (Drupal
  294. // way of doing). So, if user submits the form without changing the password,
  295. // we must prevent it from being reset.
  296. elseif (empty($form_state['values']['smtp_password'])) {
  297. unset($form_state['values']['smtp_password']);
  298. }
  299. // Save the test address to send an email after all the settings have been
  300. // updated.
  301. $form_state['storage']['smtp']['smtp_test_address'] = $form_state['values']['smtp_test_address'];
  302. unset($form_state['values']['smtp_test_address']);
  303. }
  304. /**
  305. * Submit handler for the administrative settings form containing all
  306. * functionality to be run after system_settings_form_submit.
  307. */
  308. function smtp_admin_settings_submit_post_system_settings($form, &$form_state) {
  309. // If an address was given, send a test e-mail message.
  310. $test_address = $form_state['storage']['smtp']['smtp_test_address'];
  311. if ($test_address != '') {
  312. $language = language_default();
  313. $params['subject'] = t('Drupal SMTP test e-mail');
  314. $params['body'] = array(t('If you receive this message it means your site is capable of using SMTP to send e-mail.'));
  315. drupal_mail('smtp', 'smtp-test', $test_address, $language, $params);
  316. drupal_set_message(t('A test e-mail has been sent to @email. You may want to <a href="!check">check the logs</a> for any error messages.', array('@email' => $test_address, '!check' => url('admin/reports/dblog')))); }
  317. }