]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - extlib/Mail/mail.php
b13d695656f5445c348017c7e6f8653e19fd433e
[quix0rs-gnu-social.git] / extlib / Mail / mail.php
1 <?php
2 //
3 // +----------------------------------------------------------------------+
4 // | PHP Version 4                                                        |
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2003 The PHP Group                                |
7 // +----------------------------------------------------------------------+
8 // | This source file is subject to version 2.02 of the PHP license,      |
9 // | that is bundled with this package in the file LICENSE, and is        |
10 // | available at through the world-wide-web at                           |
11 // | http://www.php.net/license/2_02.txt.                                 |
12 // | If you did not receive a copy of the PHP license and are unable to   |
13 // | obtain it through the world-wide-web, please send a note to          |
14 // | license@php.net so we can mail you a copy immediately.               |
15 // +----------------------------------------------------------------------+
16 // | Author: Chuck Hagenbuch <chuck@horde.org>                            |
17 // +----------------------------------------------------------------------+
18 //
19 // $Id: mail.php,v 1.20 2007/10/06 17:00:00 chagenbu Exp $
20
21 /**
22  * internal PHP-mail() implementation of the PEAR Mail:: interface.
23  * @package Mail
24  * @version $Revision: 1.20 $
25  */
26 class Mail_mail extends Mail {
27
28     /**
29      * Any arguments to pass to the mail() function.
30      * @var string
31      */
32     var $_params = '';
33
34     /**
35      * Constructor.
36      *
37      * Instantiates a new Mail_mail:: object based on the parameters
38      * passed in.
39      *
40      * @param array $params Extra arguments for the mail() function.
41      */
42     function Mail_mail($params = null)
43     {
44         // The other mail implementations accept parameters as arrays.
45         // In the interest of being consistent, explode an array into
46         // a string of parameter arguments.
47         if (is_array($params)) {
48             $this->_params = join(' ', $params);
49         } else {
50             $this->_params = $params;
51         }
52
53         /* Because the mail() function may pass headers as command
54          * line arguments, we can't guarantee the use of the standard
55          * "\r\n" separator.  Instead, we use the system's native line
56          * separator. */
57         if (defined('PHP_EOL')) {
58             $this->sep = PHP_EOL;
59         } else {
60             $this->sep = (strpos(PHP_OS, 'WIN') === false) ? "\n" : "\r\n";
61         }
62     }
63
64     /**
65      * Implements Mail_mail::send() function using php's built-in mail()
66      * command.
67      *
68      * @param mixed $recipients Either a comma-seperated list of recipients
69      *              (RFC822 compliant), or an array of recipients,
70      *              each RFC822 valid. This may contain recipients not
71      *              specified in the headers, for Bcc:, resending
72      *              messages, etc.
73      *
74      * @param array $headers The array of headers to send with the mail, in an
75      *              associative array, where the array key is the
76      *              header name (ie, 'Subject'), and the array value
77      *              is the header value (ie, 'test'). The header
78      *              produced from those values would be 'Subject:
79      *              test'.
80      *
81      * @param string $body The full text of the message body, including any
82      *               Mime parts, etc.
83      *
84      * @return mixed Returns true on success, or a PEAR_Error
85      *               containing a descriptive error message on
86      *               failure.
87      *
88      * @access public
89      */
90     function send($recipients, $headers, $body)
91     {
92         if (!is_array($headers)) {
93             return PEAR::raiseError('$headers must be an array');
94         }
95
96         $result = $this->_sanitizeHeaders($headers);
97         if (is_a($result, 'PEAR_Error')) {
98             return $result;
99         }
100
101         // If we're passed an array of recipients, implode it.
102         if (is_array($recipients)) {
103             $recipients = implode(', ', $recipients);
104         }
105
106         // Get the Subject out of the headers array so that we can
107         // pass it as a seperate argument to mail().
108         $subject = '';
109         if (isset($headers['Subject'])) {
110             $subject = $headers['Subject'];
111             unset($headers['Subject']);
112         }
113
114         // Also remove the To: header.  The mail() function will add its own
115         // To: header based on the contents of $recipients.
116         unset($headers['To']);
117
118         // Flatten the headers out.
119         $headerElements = $this->prepareHeaders($headers);
120         if (is_a($headerElements, 'PEAR_Error')) {
121             return $headerElements;
122         }
123         list(, $text_headers) = $headerElements;
124
125         // We only use mail()'s optional fifth parameter if the additional
126         // parameters have been provided and we're not running in safe mode.
127         if (empty($this->_params) || ini_get('safe_mode')) {
128             $result = mail($recipients, $subject, $body, $text_headers);
129         } else {
130             $result = mail($recipients, $subject, $body, $text_headers,
131                            $this->_params);
132         }
133
134         // If the mail() function returned failure, we need to create a
135         // PEAR_Error object and return it instead of the boolean result.
136         if ($result === false) {
137             $result = PEAR::raiseError('mail() returned failure');
138         }
139
140         return $result;
141     }
142
143 }