3 * internal PHP-mail() implementation of the PEAR Mail:: interface.
9 * Copyright (c) 2010-2017, Chuck Hagenbuch
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of the copyright holder nor the names of its
24 * contributors may be used to endorse or promote products derived from
25 * this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 * @author Chuck Hagenbuch <chuck@horde.org>
42 * @copyright 2010-2017 Chuck Hagenbuch
43 * @license http://opensource.org/licenses/BSD-3-Clause New BSD License
45 * @link http://pear.php.net/package/Mail/
49 * internal PHP-mail() implementation of the PEAR Mail:: interface.
53 class Mail_mail extends Mail {
56 * Any arguments to pass to the mail() function.
64 * Instantiates a new Mail_mail:: object based on the parameters
67 * @param array $params Extra arguments for the mail() function.
69 public function __construct($params = null)
71 // The other mail implementations accept parameters as arrays.
72 // In the interest of being consistent, explode an array into
73 // a string of parameter arguments.
74 if (is_array($params)) {
75 $this->_params = join(' ', $params);
77 $this->_params = $params;
80 /* Because the mail() function may pass headers as command
81 * line arguments, we can't guarantee the use of the standard
82 * "\r\n" separator. Instead, we use the system's native line
84 if (defined('PHP_EOL')) {
87 $this->sep = (strpos(PHP_OS, 'WIN') === false) ? "\n" : "\r\n";
92 * Implements Mail_mail::send() function using php's built-in mail()
95 * @param mixed $recipients Either a comma-seperated list of recipients
96 * (RFC822 compliant), or an array of recipients,
97 * each RFC822 valid. This may contain recipients not
98 * specified in the headers, for Bcc:, resending
101 * @param array $headers The array of headers to send with the mail, in an
102 * associative array, where the array key is the
103 * header name (ie, 'Subject'), and the array value
104 * is the header value (ie, 'test'). The header
105 * produced from those values would be 'Subject:
108 * @param string $body The full text of the message body, including any
111 * @return mixed Returns true on success, or a PEAR_Error
112 * containing a descriptive error message on
115 public function send($recipients, $headers, $body)
117 if (!is_array($headers)) {
118 return PEAR::raiseError('$headers must be an array');
121 $result = $this->_sanitizeHeaders($headers);
122 if (is_a($result, 'PEAR_Error')) {
126 // If we're passed an array of recipients, implode it.
127 if (is_array($recipients)) {
128 $recipients = implode(', ', $recipients);
131 // Get the Subject out of the headers array so that we can
132 // pass it as a seperate argument to mail().
134 if (isset($headers['Subject'])) {
135 $subject = $headers['Subject'];
136 unset($headers['Subject']);
139 // Also remove the To: header. The mail() function will add its own
140 // To: header based on the contents of $recipients.
141 unset($headers['To']);
143 // Flatten the headers out.
144 $headerElements = $this->prepareHeaders($headers);
145 if (is_a($headerElements, 'PEAR_Error')) {
146 return $headerElements;
148 list(, $text_headers) = $headerElements;
150 // We only use mail()'s optional fifth parameter if the additional
151 // parameters have been provided and we're not running in safe mode.
152 if (empty($this->_params) || ini_get('safe_mode')) {
153 $result = mail($recipients, $subject, $body, $text_headers);
155 $result = mail($recipients, $subject, $body, $text_headers,
159 // If the mail() function returned failure, we need to create a
160 // PEAR_Error object and return it instead of the boolean result.
161 if ($result === false) {
162 $result = PEAR::raiseError('mail() returned failure');