Updating PEAR Mail, Net and Auth
[akelos.git] / vendor / pear / Mail.php
blob51421869e0e2e995926a7b90e17112e9b056fe6b
1 <?php
2 //
3 // +----------------------------------------------------------------------+
4 // | PHP Version 4 |
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2003 The PHP Group |
7 // +----------------------------------------------------------------------+
8 // | This source file is subject to version 2.02 of the PHP license, |
9 // | that is bundled with this package in the file LICENSE, and is |
10 // | available at through the world-wide-web at |
11 // | http://www.php.net/license/2_02.txt. |
12 // | If you did not receive a copy of the PHP license and are unable to |
13 // | obtain it through the world-wide-web, please send a note to |
14 // | license@php.net so we can mail you a copy immediately. |
15 // +----------------------------------------------------------------------+
16 // | Author: Chuck Hagenbuch <chuck@horde.org> |
17 // +----------------------------------------------------------------------+
19 // $Id: Mail.php,v 1.20 2007/10/06 17:00:00 chagenbu Exp $
21 require_once 'PEAR.php';
23 /**
24 * PEAR's Mail:: interface. Defines the interface for implementing
25 * mailers under the PEAR hierarchy, and provides supporting functions
26 * useful in multiple mailer backends.
28 * @access public
29 * @version $Revision: 1.20 $
30 * @package Mail
32 class Mail
34 /**
35 * Line terminator used for separating header lines.
36 * @var string
38 var $sep = "\r\n";
40 /**
41 * Provides an interface for generating Mail:: objects of various
42 * types
44 * @param string $driver The kind of Mail:: object to instantiate.
45 * @param array $params The parameters to pass to the Mail:: object.
46 * @return object Mail a instance of the driver class or if fails a PEAR Error
47 * @access public
49 function &factory($driver, $params = array())
51 $driver = strtolower($driver);
52 @include_once 'Mail/' . $driver . '.php';
53 $class = 'Mail_' . $driver;
54 if (class_exists($class)) {
55 $mailer = new $class($params);
56 return $mailer;
57 } else {
58 return PEAR::raiseError('Unable to find class for driver ' . $driver);
62 /**
63 * Implements Mail::send() function using php's built-in mail()
64 * command.
66 * @param mixed $recipients Either a comma-seperated list of recipients
67 * (RFC822 compliant), or an array of recipients,
68 * each RFC822 valid. This may contain recipients not
69 * specified in the headers, for Bcc:, resending
70 * messages, etc.
72 * @param array $headers The array of headers to send with the mail, in an
73 * associative array, where the array key is the
74 * header name (ie, 'Subject'), and the array value
75 * is the header value (ie, 'test'). The header
76 * produced from those values would be 'Subject:
77 * test'.
79 * @param string $body The full text of the message body, including any
80 * Mime parts, etc.
82 * @return mixed Returns true on success, or a PEAR_Error
83 * containing a descriptive error message on
84 * failure.
86 * @access public
87 * @deprecated use Mail_mail::send instead
89 function send($recipients, $headers, $body)
91 if (!is_array($headers)) {
92 return PEAR::raiseError('$headers must be an array');
95 $result = $this->_sanitizeHeaders($headers);
96 if (is_a($result, 'PEAR_Error')) {
97 return $result;
100 // if we're passed an array of recipients, implode it.
101 if (is_array($recipients)) {
102 $recipients = implode(', ', $recipients);
105 // get the Subject out of the headers array so that we can
106 // pass it as a seperate argument to mail().
107 $subject = '';
108 if (isset($headers['Subject'])) {
109 $subject = $headers['Subject'];
110 unset($headers['Subject']);
113 // flatten the headers out.
114 list(, $text_headers) = Mail::prepareHeaders($headers);
116 return mail($recipients, $subject, $body, $text_headers);
120 * Sanitize an array of mail headers by removing any additional header
121 * strings present in a legitimate header's value. The goal of this
122 * filter is to prevent mail injection attacks.
124 * @param array $headers The associative array of headers to sanitize.
126 * @access private
128 function _sanitizeHeaders(&$headers)
130 foreach ($headers as $key => $value) {
131 $headers[$key] =
132 preg_replace('=((<CR>|<LF>|0x0A/%0A|0x0D/%0D|\\n|\\r)\S).*=i',
133 null, $value);
138 * Take an array of mail headers and return a string containing
139 * text usable in sending a message.
141 * @param array $headers The array of headers to prepare, in an associative
142 * array, where the array key is the header name (ie,
143 * 'Subject'), and the array value is the header
144 * value (ie, 'test'). The header produced from those
145 * values would be 'Subject: test'.
147 * @return mixed Returns false if it encounters a bad address,
148 * otherwise returns an array containing two
149 * elements: Any From: address found in the headers,
150 * and the plain text version of the headers.
151 * @access private
153 function prepareHeaders($headers)
155 $lines = array();
156 $from = null;
158 foreach ($headers as $key => $value) {
159 if (strcasecmp($key, 'From') === 0) {
160 include_once 'Mail/RFC822.php';
161 $parser = new Mail_RFC822();
162 $addresses = $parser->parseAddressList($value, 'localhost', false);
163 if (is_a($addresses, 'PEAR_Error')) {
164 return $addresses;
167 $from = $addresses[0]->mailbox . '@' . $addresses[0]->host;
169 // Reject envelope From: addresses with spaces.
170 if (strstr($from, ' ')) {
171 return false;
174 $lines[] = $key . ': ' . $value;
175 } elseif (strcasecmp($key, 'Received') === 0) {
176 $received = array();
177 if (is_array($value)) {
178 foreach ($value as $line) {
179 $received[] = $key . ': ' . $line;
182 else {
183 $received[] = $key . ': ' . $value;
185 // Put Received: headers at the top. Spam detectors often
186 // flag messages with Received: headers after the Subject:
187 // as spam.
188 $lines = array_merge($received, $lines);
189 } else {
190 // If $value is an array (i.e., a list of addresses), convert
191 // it to a comma-delimited string of its elements (addresses).
192 if (is_array($value)) {
193 $value = implode(', ', $value);
195 $lines[] = $key . ': ' . $value;
199 return array($from, join($this->sep, $lines));
203 * Take a set of recipients and parse them, returning an array of
204 * bare addresses (forward paths) that can be passed to sendmail
205 * or an smtp server with the rcpt to: command.
207 * @param mixed Either a comma-seperated list of recipients
208 * (RFC822 compliant), or an array of recipients,
209 * each RFC822 valid.
211 * @return mixed An array of forward paths (bare addresses) or a PEAR_Error
212 * object if the address list could not be parsed.
213 * @access private
215 function parseRecipients($recipients)
217 include_once 'Mail/RFC822.php';
219 // if we're passed an array, assume addresses are valid and
220 // implode them before parsing.
221 if (is_array($recipients)) {
222 $recipients = implode(', ', $recipients);
225 // Parse recipients, leaving out all personal info. This is
226 // for smtp recipients, etc. All relevant personal information
227 // should already be in the headers.
228 $addresses = Mail_RFC822::parseAddressList($recipients, 'localhost', false);
230 // If parseAddressList() returned a PEAR_Error object, just return it.
231 if (is_a($addresses, 'PEAR_Error')) {
232 return $addresses;
235 $recipients = array();
236 if (is_array($addresses)) {
237 foreach ($addresses as $ob) {
238 $recipients[] = $ob->mailbox . '@' . $ob->host;
242 return $recipients;