/core/lib/Drupal/Core/Mail/Plugin/Mail/PhpMail.php

https://gitlab.com/reasonat/test8 · PHP · 115 lines · 54 code · 11 blank · 50 comment · 5 complexity · e919292376572a255513d97aa6f008e6 MD5 · raw file

  1. <?php
  2. namespace Drupal\Core\Mail\Plugin\Mail;
  3. use Drupal\Component\Utility\Unicode;
  4. use Drupal\Core\Mail\MailFormatHelper;
  5. use Drupal\Core\Mail\MailInterface;
  6. use Drupal\Core\Site\Settings;
  7. /**
  8. * Defines the default Drupal mail backend, using PHP's native mail() function.
  9. *
  10. * @Mail(
  11. * id = "php_mail",
  12. * label = @Translation("Default PHP mailer"),
  13. * description = @Translation("Sends the message as plain text, using PHP's native mail() function.")
  14. * )
  15. */
  16. class PhpMail implements MailInterface {
  17. /**
  18. * Concatenates and wraps the email body for plain-text mails.
  19. *
  20. * @param array $message
  21. * A message array, as described in hook_mail_alter().
  22. *
  23. * @return array
  24. * The formatted $message.
  25. */
  26. public function format(array $message) {
  27. // Join the body array into one string.
  28. $message['body'] = implode("\n\n", $message['body']);
  29. // Convert any HTML to plain-text.
  30. $message['body'] = MailFormatHelper::htmlToText($message['body']);
  31. // Wrap the mail body for sending.
  32. $message['body'] = MailFormatHelper::wrapMail($message['body']);
  33. return $message;
  34. }
  35. /**
  36. * Sends an email message.
  37. *
  38. * @param array $message
  39. * A message array, as described in hook_mail_alter().
  40. *
  41. * @return bool
  42. * TRUE if the mail was successfully accepted, otherwise FALSE.
  43. *
  44. * @see http://php.net/manual/function.mail.php
  45. * @see \Drupal\Core\Mail\MailManagerInterface::mail()
  46. */
  47. public function mail(array $message) {
  48. // If 'Return-Path' isn't already set in php.ini, we pass it separately
  49. // as an additional parameter instead of in the header.
  50. if (isset($message['headers']['Return-Path'])) {
  51. $return_path_set = strpos(ini_get('sendmail_path'), ' -f');
  52. if (!$return_path_set) {
  53. $message['Return-Path'] = $message['headers']['Return-Path'];
  54. unset($message['headers']['Return-Path']);
  55. }
  56. }
  57. $mimeheaders = array();
  58. foreach ($message['headers'] as $name => $value) {
  59. $mimeheaders[] = $name . ': ' . Unicode::mimeHeaderEncode($value);
  60. }
  61. $line_endings = Settings::get('mail_line_endings', PHP_EOL);
  62. // Prepare mail commands.
  63. $mail_subject = Unicode::mimeHeaderEncode($message['subject']);
  64. // Note: email uses CRLF for line-endings. PHP's API requires LF
  65. // on Unix and CRLF on Windows. Drupal automatically guesses the
  66. // line-ending format appropriate for your system. If you need to
  67. // override this, adjust $settings['mail_line_endings'] in settings.php.
  68. $mail_body = preg_replace('@\r?\n@', $line_endings, $message['body']);
  69. // For headers, PHP's API suggests that we use CRLF normally,
  70. // but some MTAs incorrectly replace LF with CRLF. See #234403.
  71. $mail_headers = join("\n", $mimeheaders);
  72. $request = \Drupal::request();
  73. // We suppress warnings and notices from mail() because of issues on some
  74. // hosts. The return value of this method will still indicate whether mail
  75. // was sent successfully.
  76. if (!$request->server->has('WINDIR') && strpos($request->server->get('SERVER_SOFTWARE'), 'Win32') === FALSE) {
  77. // On most non-Windows systems, the "-f" option to the sendmail command
  78. // is used to set the Return-Path. There is no space between -f and
  79. // the value of the return path.
  80. $additional_headers = isset($message['Return-Path']) ? '-f' . $message['Return-Path'] : '';
  81. $mail_result = @mail(
  82. $message['to'],
  83. $mail_subject,
  84. $mail_body,
  85. $mail_headers,
  86. $additional_headers
  87. );
  88. }
  89. else {
  90. // On Windows, PHP will use the value of sendmail_from for the
  91. // Return-Path header.
  92. $old_from = ini_get('sendmail_from');
  93. ini_set('sendmail_from', $message['Return-Path']);
  94. $mail_result = @mail(
  95. $message['to'],
  96. $mail_subject,
  97. $mail_body,
  98. $mail_headers
  99. );
  100. ini_set('sendmail_from', $old_from);
  101. }
  102. return $mail_result;
  103. }
  104. }