Debian Codegraph @

vendor/pear/mail/Mail/mail.php

raw
1 <?php
2 /**
3 * internal PHP-mail() implementation of the PEAR Mail:: interface.
4 *
5 * PHP version 5
6 *
7 * LICENSE:
8 *
9 * Copyright (c) 2010-2017, Chuck Hagenbuch
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 Chuck Hagenbuch <[email protected]>
42 * @copyright 2010-2017 Chuck Hagenbuch
43 * @license http://opensource.org/licenses/BSD-3-Clause New BSD License
44 * @version CVS: $Id$
45 * @link http://pear.php.net/package/Mail/
46 */
47
48 /**
49 * internal PHP-mail() implementation of the PEAR Mail:: interface.
50 * @package Mail
51 * @version $Revision$
52 */
53 class Mail_mail extends Mail {
54
55 /**
56 * Any arguments to pass to the mail() function.
57 * @var string
58 */
59 var $_params = '';
60
61 /**
62 * Constructor.
63 *
64 * Instantiates a new Mail_mail:: object based on the parameters
65 * passed in.
66 *
67 * @param array $params Extra arguments for the mail() function.
68 */
69 public function __construct($params = null)
70 {
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);
76 } else {
77 $this->_params = $params;
78 }
79 }
80
81 /**
82 * Implements Mail_mail::send() function using php's built-in mail()
83 * command.
84 *
85 * @param mixed $recipients Either a comma-seperated list of recipients
86 * (RFC822 compliant), or an array of recipients,
87 * each RFC822 valid. This may contain recipients not
88 * specified in the headers, for Bcc:, resending
89 * messages, etc.
90 *
91 * @param array $headers The array of headers to send with the mail, in an
92 * associative array, where the array key is the
93 * header name (ie, 'Subject'), and the array value
94 * is the header value (ie, 'test'). The header
95 * produced from those values would be 'Subject:
96 * test'.
97 *
98 * @param string $body The full text of the message body, including any
99 * Mime parts, etc.
100 *
101 * @return mixed Returns true on success, or a PEAR_Error
102 * containing a descriptive error message on
103 * failure.
104 */
105 public function send($recipients, $headers, $body)
106 {
107 if (!is_array($headers)) {
108 return PEAR::raiseError('$headers must be an array');
109 }
110
111 $result = $this->_sanitizeHeaders($headers);
112 if (is_a($result, 'PEAR_Error')) {
113 return $result;
114 }
115
116 // If we're passed an array of recipients, implode it.
117 if (is_array($recipients)) {
118 $recipients = implode(', ', $recipients);
119 }
120
121 // Get the Subject out of the headers array so that we can
122 // pass it as a seperate argument to mail().
123 $subject = '';
124 if (isset($headers['Subject'])) {
125 $subject = $headers['Subject'];
126 unset($headers['Subject']);
127 }
128
129 // Also remove the To: header. The mail() function will add its own
130 // To: header based on the contents of $recipients.
131 unset($headers['To']);
132
133 // Flatten the headers out.
134 $headerElements = $this->prepareHeaders($headers);
135 if (is_a($headerElements, 'PEAR_Error')) {
136 return $headerElements;
137 }
138 list(, $text_headers) = $headerElements;
139
140 // We only use mail()'s optional fifth parameter if the additional
141 // parameters have been provided and we're not running in safe mode.
142 if (empty($this->_params) || ini_get('safe_mode')) {
143 $result = mail($recipients, $subject, $body, $text_headers);
144 } else {
145 $result = mail($recipients, $subject, $body, $text_headers,
146 $this->_params);
147 }
148
149 // If the mail() function returned failure, we need to create a
150 // PEAR_Error object and return it instead of the boolean result.
151 if ($result === false) {
152 $result = PEAR::raiseError('mail() returned failure');
153 }
154
155 return $result;
156 }
157
158 }