system.mail.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/en/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. if (isset($message['Return-Path']) && !ini_get('safe_mode')) {
  70. // On most non-Windows systems, the "-f" option to the sendmail command
  71. // is used to set the Return-Path. There is no space between -f and
  72. // the value of the return path.
  73. $mail_result = @mail(
  74. $message['to'],
  75. $mail_subject,
  76. $mail_body,
  77. $mail_headers,
  78. '-f' . $message['Return-Path']
  79. );
  80. }
  81. else {
  82. // The optional $additional_parameters argument to mail() is not
  83. // allowed if safe_mode is enabled. Passing any value throws a PHP
  84. // warning and makes mail() return FALSE.
  85. $mail_result = @mail(
  86. $message['to'],
  87. $mail_subject,
  88. $mail_body,
  89. $mail_headers
  90. );
  91. }
  92. }
  93. else {
  94. // On Windows, PHP will use the value of sendmail_from for the
  95. // Return-Path header.
  96. $old_from = ini_get('sendmail_from');
  97. ini_set('sendmail_from', $message['Return-Path']);
  98. $mail_result = @mail(
  99. $message['to'],
  100. $mail_subject,
  101. $mail_body,
  102. $mail_headers
  103. );
  104. ini_set('sendmail_from', $old_from);
  105. }
  106. return $mail_result;
  107. }
  108. }
  109. /**
  110. * A mail sending implementation that captures sent messages to a variable.
  111. *
  112. * This class is for running tests or for development.
  113. */
  114. class TestingMailSystem extends DefaultMailSystem implements MailSystemInterface {
  115. /**
  116. * Accept an e-mail message and store it in a variable.
  117. *
  118. * @param $message
  119. * An e-mail message.
  120. */
  121. public function mail(array $message) {
  122. $captured_emails = variable_get('drupal_test_email_collector', array());
  123. $captured_emails[] = $message;
  124. variable_set('drupal_test_email_collector', $captured_emails);
  125. return TRUE;
  126. }
  127. }