system.mail.inc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * @file
  4. * Drupal core implementations of MailSystemInterface.
  5. */
  6. /**
  7. * The default Drupal mail backend using PHP's mail function.
  8. */
  9. class DefaultMailSystem implements MailSystemInterface {
  10. /**
  11. * Concatenate and wrap the e-mail body for plain-text mails.
  12. *
  13. * @param $message
  14. * A message array, as described in hook_mail_alter().
  15. *
  16. * @return
  17. * The formatted $message.
  18. */
  19. public function format(array $message) {
  20. // Join the body array into one string.
  21. $message['body'] = implode("\n\n", $message['body']);
  22. // Convert any HTML to plain-text.
  23. $message['body'] = drupal_html_to_text($message['body']);
  24. // Wrap the mail body for sending.
  25. $message['body'] = drupal_wrap_mail($message['body']);
  26. return $message;
  27. }
  28. /**
  29. * Send an e-mail message, using Drupal variables and default settings.
  30. *
  31. * @see http://php.net/manual/function.mail.php
  32. * @see drupal_mail()
  33. *
  34. * @param $message
  35. * A message array, as described in hook_mail_alter().
  36. * @return
  37. * TRUE if the mail was successfully accepted, otherwise FALSE.
  38. */
  39. public function mail(array $message) {
  40. // If 'Return-Path' isn't already set in php.ini, we pass it separately
  41. // as an additional parameter instead of in the header.
  42. // However, if PHP's 'safe_mode' is on, this is not allowed.
  43. if (isset($message['headers']['Return-Path']) && !ini_get('safe_mode')) {
  44. $return_path_set = strpos(ini_get('sendmail_path'), ' -f');
  45. if (!$return_path_set) {
  46. $message['Return-Path'] = $message['headers']['Return-Path'];
  47. unset($message['headers']['Return-Path']);
  48. }
  49. }
  50. $mimeheaders = array();
  51. foreach ($message['headers'] as $name => $value) {
  52. $mimeheaders[] = $name . ': ' . mime_header_encode($value);
  53. }
  54. $line_endings = variable_get('mail_line_endings', MAIL_LINE_ENDINGS);
  55. // Prepare mail commands.
  56. $mail_subject = mime_header_encode($message['subject']);
  57. // Note: e-mail uses CRLF for line-endings. PHP's API requires LF
  58. // on Unix and CRLF on Windows. Drupal automatically guesses the
  59. // line-ending format appropriate for your system. If you need to
  60. // override this, adjust $conf['mail_line_endings'] in settings.php.
  61. $mail_body = preg_replace('@\r?\n@', $line_endings, $message['body']);
  62. // For headers, PHP's API suggests that we use CRLF normally,
  63. // but some MTAs incorrectly replace LF with CRLF. See #234403.
  64. $mail_headers = join("\n", $mimeheaders);
  65. // We suppress warnings and notices from mail() because of issues on some
  66. // hosts. The return value of this method will still indicate whether mail
  67. // was sent successfully.
  68. if (!isset($_SERVER['WINDIR']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Win32') === FALSE) {
  69. // We validate the return path, unless it is equal to the site mail, which
  70. // we assume to be safe.
  71. if (isset($message['Return-Path']) && !ini_get('safe_mode') && (variable_get('site_mail', ini_get('sendmail_from')) === $message['Return-Path'] || self::_isShellSafe($message['Return-Path']))) {
  72. // On most non-Windows systems, the "-f" option to the sendmail command
  73. // is used to set the Return-Path. There is no space between -f and
  74. // the value of the return path.
  75. $mail_result = @mail(
  76. $message['to'],
  77. $mail_subject,
  78. $mail_body,
  79. $mail_headers,
  80. '-f' . $message['Return-Path']
  81. );
  82. }
  83. else {
  84. // The optional $additional_parameters argument to mail() is not
  85. // allowed if safe_mode is enabled. Passing any value throws a PHP
  86. // warning and makes mail() return FALSE.
  87. $mail_result = @mail(
  88. $message['to'],
  89. $mail_subject,
  90. $mail_body,
  91. $mail_headers
  92. );
  93. }
  94. }
  95. else {
  96. // On Windows, PHP will use the value of sendmail_from for the
  97. // Return-Path header.
  98. $old_from = ini_get('sendmail_from');
  99. ini_set('sendmail_from', $message['Return-Path']);
  100. $mail_result = @mail(
  101. $message['to'],
  102. $mail_subject,
  103. $mail_body,
  104. $mail_headers
  105. );
  106. ini_set('sendmail_from', $old_from);
  107. }
  108. return $mail_result;
  109. }
  110. /**
  111. * Disallows potentially unsafe shell characters.
  112. *
  113. * Functionally similar to PHPMailer::isShellSafe() which resulted from
  114. * CVE-2016-10045. Note that escapeshellarg and escapeshellcmd are inadequate
  115. * for this purpose.
  116. *
  117. * @param string $string
  118. * The string to be validated.
  119. *
  120. * @return bool
  121. * True if the string is shell-safe.
  122. *
  123. * @see https://github.com/PHPMailer/PHPMailer/issues/924
  124. * @see https://github.com/PHPMailer/PHPMailer/blob/v5.2.21/class.phpmailer.php#L1430
  125. *
  126. * @todo Rename to ::isShellSafe() and/or discuss whether this is the correct
  127. * location for this helper.
  128. */
  129. protected static function _isShellSafe($string) {
  130. if (escapeshellcmd($string) !== $string || !in_array(escapeshellarg($string), array("'$string'", "\"$string\""))) {
  131. return FALSE;
  132. }
  133. if (preg_match('/[^a-zA-Z0-9@_\-.]/', $string) !== 0) {
  134. return FALSE;
  135. }
  136. return TRUE;
  137. }
  138. }
  139. /**
  140. * A mail sending implementation that captures sent messages to a variable.
  141. *
  142. * This class is for running tests or for development.
  143. */
  144. class TestingMailSystem extends DefaultMailSystem implements MailSystemInterface {
  145. /**
  146. * Accept an e-mail message and store it in a variable.
  147. *
  148. * @param $message
  149. * An e-mail message.
  150. */
  151. public function mail(array $message) {
  152. $captured_emails = variable_get('drupal_test_email_collector', array());
  153. $captured_emails[] = $message;
  154. variable_set('drupal_test_email_collector', $captured_emails);
  155. return TRUE;
  156. }
  157. }