Projet

Général

Profil

Paste
Télécharger (4,54 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / system / system.mail.inc @ db2d93dd

1
<?php
2

    
3
/**
4
 * @file
5
 * Drupal core implementations of MailSystemInterface.
6
 */
7

    
8
/**
9
 * The default Drupal mail backend using PHP's mail function.
10
 */
11
class DefaultMailSystem implements MailSystemInterface {
12
  /**
13
   * Concatenate and wrap the e-mail body for plain-text mails.
14
   *
15
   * @param $message
16
   *   A message array, as described in hook_mail_alter().
17
   *
18
   * @return
19
   *   The formatted $message.
20
   */
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']);
28
    return $message;
29
  }
30

    
31
  /**
32
   * Send an e-mail message, using Drupal variables and default settings.
33
   *
34
   * @see http://php.net/manual/function.mail.php
35
   * @see drupal_mail()
36
   *
37
   * @param $message
38
   *   A message array, as described in hook_mail_alter().
39
   * @return
40
   *   TRUE if the mail was successfully accepted, otherwise FALSE.
41
   */
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']);
51
      }
52
    }
53
    $mimeheaders = array();
54
    foreach ($message['headers'] as $name => $value) {
55
      $mimeheaders[] = $name . ': ' . mime_header_encode($value);
56
    }
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);
68

    
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.
77
        $mail_result = @mail(
78
          $message['to'],
79
          $mail_subject,
80
          $mail_body,
81
          $mail_headers,
82
          '-f' . $message['Return-Path']
83
        );
84
      }
85
      else {
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.
89
        $mail_result = @mail(
90
          $message['to'],
91
          $mail_subject,
92
          $mail_body,
93
          $mail_headers
94
        );
95
      }
96
     }
97
     else {
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(
103
         $message['to'],
104
         $mail_subject,
105
         $mail_body,
106
         $mail_headers
107
       );
108
      ini_set('sendmail_from', $old_from);
109
     }
110
     return $mail_result;
111
  }
112
}
113

    
114
/**
115
 * A mail sending implementation that captures sent messages to a variable.
116
 *
117
 * This class is for running tests or for development.
118
 */
119
class TestingMailSystem extends DefaultMailSystem implements MailSystemInterface {
120
  /**
121
   * Accept an e-mail message and store it in a variable.
122
   *
123
   * @param $message
124
   *   An e-mail message.
125
   */
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);
130
    return TRUE;
131
  }
132
}
133