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