Version 1
[yaffs-website] / web / core / lib / Drupal / Core / Mail / Plugin / Mail / PhpMail.php
1 <?php
2
3 namespace Drupal\Core\Mail\Plugin\Mail;
4
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Mail\MailFormatHelper;
7 use Drupal\Core\Mail\MailInterface;
8 use Drupal\Core\Site\Settings;
9
10 /**
11  * Defines the default Drupal mail backend, using PHP's native mail() function.
12  *
13  * @Mail(
14  *   id = "php_mail",
15  *   label = @Translation("Default PHP mailer"),
16  *   description = @Translation("Sends the message as plain text, using PHP's native mail() function.")
17  * )
18  */
19 class PhpMail implements MailInterface {
20
21   /**
22    * Concatenates and wraps the email body for plain-text mails.
23    *
24    * @param array $message
25    *   A message array, as described in hook_mail_alter().
26    *
27    * @return array
28    *   The formatted $message.
29    */
30   public function format(array $message) {
31     // Join the body array into one string.
32     $message['body'] = implode("\n\n", $message['body']);
33
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']);
38
39     return $message;
40   }
41
42   /**
43    * Sends an email message.
44    *
45    * @param array $message
46    *   A message array, as described in hook_mail_alter().
47    *
48    * @return bool
49    *   TRUE if the mail was successfully accepted, otherwise FALSE.
50    *
51    * @see http://php.net/manual/function.mail.php
52    * @see \Drupal\Core\Mail\MailManagerInterface::mail()
53    */
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']);
62       }
63     }
64     $mimeheaders = [];
65     foreach ($message['headers'] as $name => $value) {
66       $mimeheaders[] = $name . ': ' . Unicode::mimeHeaderEncode($value);
67     }
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 = join("\n", $mimeheaders);
79
80     $request = \Drupal::request();
81
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'] : '';
90       $mail_result = @mail(
91         $message['to'],
92         $mail_subject,
93         $mail_body,
94         $mail_headers,
95         $additional_headers
96       );
97     }
98     else {
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(
104         $message['to'],
105         $mail_subject,
106         $mail_body,
107         $mail_headers
108       );
109       ini_set('sendmail_from', $old_from);
110     }
111
112     return $mail_result;
113   }
114
115 }