3 * Sendmail implementation of the PEAR Mail interface.
9 * Copyright (c) 2010-2017, Chuck Hagenbuch & Jon Parise
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 Jon Parise <jon@php.net>
42 * @author Chuck Hagenbuch <chuck@horde.org>
43 * @copyright 2010-2017 Chuck Hagenbuch
44 * @license http://opensource.org/licenses/BSD-3-Clause New BSD License
46 * @link http://pear.php.net/package/Mail/
50 * Sendmail implementation of the PEAR Mail:: interface.
55 class Mail_sendmail extends Mail {
58 * The location of the sendmail or sendmail wrapper binary on the
62 var $sendmail_path = '/usr/sbin/sendmail';
65 * Any extra command-line parameters to pass to the sendmail or
66 * sendmail wrapper binary.
69 var $sendmail_args = '-i';
74 * Instantiates a new Mail_sendmail:: object based on the parameters
75 * passed in. It looks for the following parameters:
76 * sendmail_path The location of the sendmail binary on the
77 * filesystem. Defaults to '/usr/sbin/sendmail'.
79 * sendmail_args Any extra parameters to pass to the sendmail
80 * or sendmail wrapper binary.
82 * If a parameter is present in the $params array, it replaces the
85 * @param array $params Hash containing any parameters different from the
88 public function __construct($params)
90 if (isset($params['sendmail_path'])) {
91 $this->sendmail_path = $params['sendmail_path'];
93 if (isset($params['sendmail_args'])) {
94 $this->sendmail_args = $params['sendmail_args'];
98 * Because we need to pass message headers to the sendmail program on
99 * the commandline, we can't guarantee the use of the standard "\r\n"
100 * separator. Instead, we use the system's native line separator.
102 if (defined('PHP_EOL')) {
103 $this->sep = PHP_EOL;
105 $this->sep = (strpos(PHP_OS, 'WIN') === false) ? "\n" : "\r\n";
110 * Implements Mail::send() function using the sendmail
111 * command-line binary.
113 * @param mixed $recipients Either a comma-seperated list of recipients
114 * (RFC822 compliant), or an array of recipients,
115 * each RFC822 valid. This may contain recipients not
116 * specified in the headers, for Bcc:, resending
119 * @param array $headers The array of headers to send with the mail, in an
120 * associative array, where the array key is the
121 * header name (ie, 'Subject'), and the array value
122 * is the header value (ie, 'test'). The header
123 * produced from those values would be 'Subject:
126 * @param string $body The full text of the message body, including any
129 * @return mixed Returns true on success, or a PEAR_Error
130 * containing a descriptive error message on
133 public function send($recipients, $headers, $body)
135 if (!is_array($headers)) {
136 return PEAR::raiseError('$headers must be an array');
139 $result = $this->_sanitizeHeaders($headers);
140 if (is_a($result, 'PEAR_Error')) {
144 $recipients = $this->parseRecipients($recipients);
145 if (is_a($recipients, 'PEAR_Error')) {
148 $recipients = implode(' ', array_map('escapeshellarg', $recipients));
150 $headerElements = $this->prepareHeaders($headers);
151 if (is_a($headerElements, 'PEAR_Error')) {
152 return $headerElements;
154 list($from, $text_headers) = $headerElements;
156 /* Since few MTAs are going to allow this header to be forged
157 * unless it's in the MAIL FROM: exchange, we'll use
158 * Return-Path instead of From: if it's set. */
159 if (!empty($headers['Return-Path'])) {
160 $from = $headers['Return-Path'];
164 return PEAR::raiseError('No from address given.');
165 } elseif (strpos($from, ' ') !== false ||
166 strpos($from, ';') !== false ||
167 strpos($from, '&') !== false ||
168 strpos($from, '`') !== false) {
169 return PEAR::raiseError('From address specified with dangerous characters.');
172 $from = escapeshellarg($from); // Security bug #16200
174 $mail = @popen($this->sendmail_path . (!empty($this->sendmail_args) ? ' ' . $this->sendmail_args : '') . " -f$from -- $recipients", 'w');
176 return PEAR::raiseError('Failed to open sendmail [' . $this->sendmail_path . '] for execution.');
179 // Write the headers following by two newlines: one to end the headers
180 // section and a second to separate the headers block from the body.
181 fputs($mail, $text_headers . $this->sep . $this->sep);
184 $result = pclose($mail);
185 if (version_compare(phpversion(), '4.2.3') == -1) {
186 // With older php versions, we need to shift the pclose
187 // result to get the exit code.
188 $result = $result >> 8 & 0xFF;
192 return PEAR::raiseError('sendmail returned error code ' . $result,