5 * Drupal core implementations of MailSystemInterface.
9 * The default Drupal mail backend using PHP's mail function.
11 class DefaultMailSystem implements MailSystemInterface {
13 * Concatenate and wrap the e-mail body for plain-text mails.
16 * A message array, as described in hook_mail_alter().
19 * The formatted $message.
21 public function format(array $message) {
22 // Join the body array into one string.
23 $message['body'] = implode("\n\n", $message['body']);
24 // Convert any HTML to plain-text.
25 $message['body'] = drupal_html_to_text($message['body']);
26 // Wrap the mail body for sending.
27 $message['body'] = drupal_wrap_mail($message['body']);
32 * Send an e-mail message, using Drupal variables and default settings.
34 * @see http://php.net/manual/function.mail.php
38 * A message array, as described in hook_mail_alter().
40 * TRUE if the mail was successfully accepted, otherwise FALSE.
42 public function mail(array $message) {
43 // If 'Return-Path' isn't already set in php.ini, we pass it separately
44 // as an additional parameter instead of in the header.
45 // However, if PHP's 'safe_mode' is on, this is not allowed.
46 if (isset($message['headers']['Return-Path']) && !ini_get('safe_mode')) {
47 $return_path_set = strpos(ini_get('sendmail_path'), ' -f');
48 if (!$return_path_set) {
49 $message['Return-Path'] = $message['headers']['Return-Path'];
50 unset($message['headers']['Return-Path']);
53 $mimeheaders = array();
54 foreach ($message['headers'] as $name => $value) {
55 $mimeheaders[] = $name . ': ' . mime_header_encode($value);
57 $line_endings = variable_get('mail_line_endings', MAIL_LINE_ENDINGS);
58 // Prepare mail commands.
59 $mail_subject = mime_header_encode($message['subject']);
60 // Note: e-mail uses CRLF for line-endings. PHP's API requires LF
61 // on Unix and CRLF on Windows. Drupal automatically guesses the
62 // line-ending format appropriate for your system. If you need to
63 // override this, adjust $conf['mail_line_endings'] in settings.php.
64 $mail_body = preg_replace('@\r?\n@', $line_endings, $message['body']);
65 // For headers, PHP's API suggests that we use CRLF normally,
66 // but some MTAs incorrectly replace LF with CRLF. See #234403.
67 $mail_headers = join("\n", $mimeheaders);
69 // We suppress warnings and notices from mail() because of issues on some
70 // hosts. The return value of this method will still indicate whether mail
71 // was sent successfully.
72 if (!isset($_SERVER['WINDIR']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Win32') === FALSE) {
73 if (isset($message['Return-Path']) && !ini_get('safe_mode')) {
74 // On most non-Windows systems, the "-f" option to the sendmail command
75 // is used to set the Return-Path. There is no space between -f and
76 // the value of the return path.
82 '-f' . $message['Return-Path']
86 // The optional $additional_parameters argument to mail() is not
87 // allowed if safe_mode is enabled. Passing any value throws a PHP
88 // warning and makes mail() return FALSE.
98 // On Windows, PHP will use the value of sendmail_from for the
99 // Return-Path header.
100 $old_from = ini_get('sendmail_from');
101 ini_set('sendmail_from', $message['Return-Path']);
102 $mail_result = @mail(
108 ini_set('sendmail_from', $old_from);
115 * A mail sending implementation that captures sent messages to a variable.
117 * This class is for running tests or for development.
119 class TestingMailSystem extends DefaultMailSystem implements MailSystemInterface {
121 * Accept an e-mail message and store it in a variable.
126 public function mail(array $message) {
127 $captured_emails = variable_get('drupal_test_email_collector', array());
128 $captured_emails[] = $message;
129 variable_set('drupal_test_email_collector', $captured_emails);