3 * PEAR's Mail:: interface.
9 * Copyright (c) 2002-2007, Richard Heyes
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 * o Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * o Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * o The names of the authors may not be used to endorse or promote
22 * products derived from this software without specific prior written
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * @author Chuck Hagenbuch <chuck@horde.org>
40 * @copyright 1997-2010 Chuck Hagenbuch
41 * @license http://opensource.org/licenses/bsd-license.php New BSD License
42 * @version CVS: $Id: Mail.php 294747 2010-02-08 08:18:33Z clockwerx $
43 * @link http://pear.php.net/package/Mail/
46 require_once 'PEAR.php';
49 * PEAR's Mail:: interface. Defines the interface for implementing
50 * mailers under the PEAR hierarchy, and provides supporting functions
51 * useful in multiple mailer backends.
54 * @version $Revision: 294747 $
60 * Line terminator used for separating header lines.
66 * Provides an interface for generating Mail:: objects of various
69 * @param string $driver The kind of Mail:: object to instantiate.
70 * @param array $params The parameters to pass to the Mail:: object.
71 * @return object Mail a instance of the driver class or if fails a PEAR Error
74 function &factory($driver, $params = array())
76 print_r($driver);exit;
77 $driver = strtolower($driver);
78 @include_once 'Mail/' . $driver . '.php';
79 $class = 'Mail_' . $driver;
80 if (class_exists($class)) {
81 $mailer = new $class($params);
84 return PEAR::raiseError('Unable to find class for driver ' . $driver);
89 * Implements Mail::send() function using php's built-in mail()
92 * @param mixed $recipients Either a comma-seperated list of recipients
93 * (RFC822 compliant), or an array of recipients,
94 * each RFC822 valid. This may contain recipients not
95 * specified in the headers, for Bcc:, resending
98 * @param array $headers The array of headers to send with the mail, in an
99 * associative array, where the array key is the
100 * header name (ie, 'Subject'), and the array value
101 * is the header value (ie, 'test'). The header
102 * produced from those values would be 'Subject:
105 * @param string $body The full text of the message body, including any
108 * @return mixed Returns true on success, or a PEAR_Error
109 * containing a descriptive error message on
113 * @deprecated use Mail_mail::send instead
115 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 // flatten the headers out.
140 list(, $text_headers) = Mail::prepareHeaders($headers);
142 return mail($recipients, $subject, $body, $text_headers);
146 * Sanitize an array of mail headers by removing any additional header
147 * strings present in a legitimate header's value. The goal of this
148 * filter is to prevent mail injection attacks.
150 * @param array $headers The associative array of headers to sanitize.
154 function _sanitizeHeaders(&$headers)
156 foreach ($headers as $key => $value) {
158 preg_replace('=((<CR>|<LF>|0x0A/%0A|0x0D/%0D|\\n|\\r)\S).*=i',
164 * Take an array of mail headers and return a string containing
165 * text usable in sending a message.
167 * @param array $headers The array of headers to prepare, in an associative
168 * array, where the array key is the header name (ie,
169 * 'Subject'), and the array value is the header
170 * value (ie, 'test'). The header produced from those
171 * values would be 'Subject: test'.
173 * @return mixed Returns false if it encounters a bad address,
174 * otherwise returns an array containing two
175 * elements: Any From: address found in the headers,
176 * and the plain text version of the headers.
179 function prepareHeaders($headers)
184 foreach ($headers as $key => $value) {
185 if (strcasecmp($key, 'From') === 0) {
186 include_once 'Mail/RFC822.php';
187 $parser = new Mail_RFC822();
188 $addresses = $parser->parseAddressList($value, 'localhost', false);
189 if (is_a($addresses, 'PEAR_Error')) {
193 $from = $addresses[0]->mailbox . '@' . $addresses[0]->host;
195 // Reject envelope From: addresses with spaces.
196 if (strstr($from, ' ')) {
200 $lines[] = $key . ': ' . $value;
201 } elseif (strcasecmp($key, 'Received') === 0) {
203 if (is_array($value)) {
204 foreach ($value as $line) {
205 $received[] = $key . ': ' . $line;
209 $received[] = $key . ': ' . $value;
211 // Put Received: headers at the top. Spam detectors often
212 // flag messages with Received: headers after the Subject:
214 $lines = array_merge($received, $lines);
216 // If $value is an array (i.e., a list of addresses), convert
217 // it to a comma-delimited string of its elements (addresses).
218 if (is_array($value)) {
219 $value = implode(', ', $value);
221 $lines[] = $key . ': ' . $value;
225 return array($from, join($this->sep, $lines));
229 * Take a set of recipients and parse them, returning an array of
230 * bare addresses (forward paths) that can be passed to sendmail
231 * or an smtp server with the rcpt to: command.
233 * @param mixed Either a comma-seperated list of recipients
234 * (RFC822 compliant), or an array of recipients,
237 * @return mixed An array of forward paths (bare addresses) or a PEAR_Error
238 * object if the address list could not be parsed.
241 function parseRecipients($recipients)
243 include_once 'Mail/RFC822.php';
245 // if we're passed an array, assume addresses are valid and
246 // implode them before parsing.
247 if (is_array($recipients)) {
248 $recipients = implode(', ', $recipients);
251 // Parse recipients, leaving out all personal info. This is
252 // for smtp recipients, etc. All relevant personal information
253 // should already be in the headers.
254 $obj = new Mail_RFC822($recipients, 'localhost', false);
255 $addresses = $obj->parseAddressList();
258 // If parseAddressList() returned a PEAR_Error object, just return it.
259 if (is_a($addresses, 'PEAR_Error')) {
263 $recipients = array();
264 if (is_array($addresses)) {
265 foreach ($addresses as $ob) {
266 $recipients[] = $ob->mailbox . '@' . $ob->host;