1<?php
2/**
3 * Sendmail implementation of the PEAR Mail interface.
4 *
5 * PHP version 5
6 *
7 * LICENSE:
8 *
9 * Copyright (c) 2010-2017, Chuck Hagenbuch & Jon Parise
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 *
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.
22 *
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.
26 *
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.
38 *
39 * @category    Mail
40 * @package     Mail
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
45 * @version     CVS: $Id$
46 * @link        http://pear.php.net/package/Mail/
47 */
48
49/**
50 * Sendmail implementation of the PEAR Mail:: interface.
51 * @access public
52 * @package Mail
53 * @version $Revision$
54 */
55class Mail_sendmail extends Mail {
56
57    /**
58     * The location of the sendmail or sendmail wrapper binary on the
59     * filesystem.
60     * @var string
61     */
62    var $sendmail_path = '/usr/sbin/sendmail';
63
64    /**
65     * Any extra command-line parameters to pass to the sendmail or
66     * sendmail wrapper binary.
67     * @var string
68     */
69    var $sendmail_args = '-i';
70
71    /**
72     * Constructor.
73     *
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'.
78     *
79     *     sendmail_args    Any extra parameters to pass to the sendmail
80     *                      or sendmail wrapper binary.
81     *
82     * If a parameter is present in the $params array, it replaces the
83     * default.
84     *
85     * @param array $params Hash containing any parameters different from the
86     *              defaults.
87     */
88    public function __construct($params)
89    {
90        if (isset($params['sendmail_path'])) {
91            $this->sendmail_path = $params['sendmail_path'];
92        }
93        if (isset($params['sendmail_args'])) {
94            $this->sendmail_args = $params['sendmail_args'];
95        }
96
97        /*
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.
101         */
102        if (defined('PHP_EOL')) {
103            $this->sep = PHP_EOL;
104        } else {
105            $this->sep = (strpos(PHP_OS, 'WIN') === false) ? "\n" : "\r\n";
106        }
107    }
108
109    /**
110     * Implements Mail::send() function using the sendmail
111     * command-line binary.
112     *
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
117     *              messages, etc.
118     *
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:
124     *              test'.
125     *
126     * @param string $body The full text of the message body, including any
127     *               Mime parts, etc.
128     *
129     * @return mixed Returns true on success, or a PEAR_Error
130     *               containing a descriptive error message on
131     *               failure.
132     */
133    public function send($recipients, $headers, $body)
134    {
135        if (!is_array($headers)) {
136            return PEAR::raiseError('$headers must be an array');
137        }
138
139        $result = $this->_sanitizeHeaders($headers);
140        if (is_a($result, 'PEAR_Error')) {
141            return $result;
142        }
143
144        $recipients = $this->parseRecipients($recipients);
145        if (is_a($recipients, 'PEAR_Error')) {
146            return $recipients;
147        }
148        $recipients = implode(' ', array_map('escapeshellarg', $recipients));
149
150        $headerElements = $this->prepareHeaders($headers);
151        if (is_a($headerElements, 'PEAR_Error')) {
152            return $headerElements;
153        }
154        list($from, $text_headers) = $headerElements;
155
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'];
161        }
162
163        if (!isset($from)) {
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.');
170        }
171
172        $from = escapeshellarg($from); // Security bug #16200
173
174        $mail = @popen($this->sendmail_path . (!empty($this->sendmail_args) ? ' ' . $this->sendmail_args : '') . " -f$from -- $recipients", 'w');
175        if (!$mail) {
176            return PEAR::raiseError('Failed to open sendmail [' . $this->sendmail_path . '] for execution.');
177        }
178
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);
182
183        fputs($mail, $body);
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;
189        }
190
191        if ($result != 0) {
192            return PEAR::raiseError('sendmail returned error code ' . $result,
193                                    $result);
194        }
195
196        return true;
197    }
198
199}
200