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 | // +----------------------------------------------------------------------+ |
---|
18 | // |
---|
19 | // $Id: Mail.php,v 1.17 2006/09/15 03:41:18 jon Exp $ |
---|
20 | |
---|
21 | require_once dirname(__FILE__) . "/../PEAR.php"; |
---|
22 | |
---|
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. |
---|
27 | * |
---|
28 | * @access public |
---|
29 | * @version $Revision: 1.17 $ |
---|
30 | * @package Mail |
---|
31 | */ |
---|
32 | class Mail |
---|
33 | { |
---|
34 | /** |
---|
35 | * Line terminator used for separating header lines. |
---|
36 | * @var string |
---|
37 | */ |
---|
38 | var $sep = "\r\n"; |
---|
39 | |
---|
40 | /** |
---|
41 | * Provides an interface for generating Mail:: objects of various |
---|
42 | * types |
---|
43 | * |
---|
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 |
---|
48 | */ |
---|
49 | function &factory($driver, $params = array()) |
---|
50 | { |
---|
51 | $driver = strtolower($driver); |
---|
52 | $include_dir = realpath(dirname( __FILE__)); |
---|
53 | include_once $include_dir ."/../Mail/" . $driver . ".php"; |
---|
54 | $class = 'Mail_' . $driver; |
---|
55 | if (class_exists($class)) { |
---|
56 | $mailer = new $class($params); |
---|
57 | return $mailer; |
---|
58 | } else { |
---|
59 | return PEAR::raiseError('Unable to find class for driver ' . $driver); |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | * Implements Mail::send() function using php's built-in mail() |
---|
65 | * command. |
---|
66 | * |
---|
67 | * @param mixed $recipients Either a comma-seperated list of recipients |
---|
68 | * (RFC822 compliant), or an array of recipients, |
---|
69 | * each RFC822 valid. This may contain recipients not |
---|
70 | * specified in the headers, for Bcc:, resending |
---|
71 | * messages, etc. |
---|
72 | * |
---|
73 | * @param array $headers The array of headers to send with the mail, in an |
---|
74 | * associative array, where the array key is the |
---|
75 | * header name (ie, 'Subject'), and the array value |
---|
76 | * is the header value (ie, 'test'). The header |
---|
77 | * produced from those values would be 'Subject: |
---|
78 | * test'. |
---|
79 | * |
---|
80 | * @param string $body The full text of the message body, including any |
---|
81 | * Mime parts, etc. |
---|
82 | * |
---|
83 | * @return mixed Returns true on success, or a PEAR_Error |
---|
84 | * containing a descriptive error message on |
---|
85 | * failure. |
---|
86 | * @access public |
---|
87 | * @deprecated use Mail_mail::send instead |
---|
88 | */ |
---|
89 | function send($recipients, $headers, $body) |
---|
90 | { |
---|
91 | $this->_sanitizeHeaders($headers); |
---|
92 | |
---|
93 | // if we're passed an array of recipients, implode it. |
---|
94 | if (is_array($recipients)) { |
---|
95 | $recipients = implode(', ', $recipients); |
---|
96 | } |
---|
97 | |
---|
98 | // get the Subject out of the headers array so that we can |
---|
99 | // pass it as a seperate argument to mail(). |
---|
100 | $subject = ''; |
---|
101 | if (isset($headers['Subject'])) { |
---|
102 | $subject = $headers['Subject']; |
---|
103 | unset($headers['Subject']); |
---|
104 | } |
---|
105 | |
---|
106 | // flatten the headers out. |
---|
107 | list(,$text_headers) = Mail::prepareHeaders($headers); |
---|
108 | |
---|
109 | return mail($recipients, $subject, $body, $text_headers); |
---|
110 | |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * Sanitize an array of mail headers by removing any additional header |
---|
115 | * strings present in a legitimate header's value. The goal of this |
---|
116 | * filter is to prevent mail injection attacks. |
---|
117 | * |
---|
118 | * @param array $headers The associative array of headers to sanitize. |
---|
119 | * |
---|
120 | * @access private |
---|
121 | */ |
---|
122 | function _sanitizeHeaders(&$headers) |
---|
123 | { |
---|
124 | foreach ($headers as $key => $value) { |
---|
125 | $headers[$key] = |
---|
126 | preg_replace('=((<CR>|<LF>|0x0A/%0A|0x0D/%0D|\\n|\\r)\S).*=i', |
---|
127 | null, $value); |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | * Take an array of mail headers and return a string containing |
---|
133 | * text usable in sending a message. |
---|
134 | * |
---|
135 | * @param array $headers The array of headers to prepare, in an associative |
---|
136 | * array, where the array key is the header name (ie, |
---|
137 | * 'Subject'), and the array value is the header |
---|
138 | * value (ie, 'test'). The header produced from those |
---|
139 | * values would be 'Subject: test'. |
---|
140 | * |
---|
141 | * @return mixed Returns false if it encounters a bad address, |
---|
142 | * otherwise returns an array containing two |
---|
143 | * elements: Any From: address found in the headers, |
---|
144 | * and the plain text version of the headers. |
---|
145 | * @access private |
---|
146 | */ |
---|
147 | function prepareHeaders($headers) |
---|
148 | { |
---|
149 | $lines = array(); |
---|
150 | $from = null; |
---|
151 | |
---|
152 | foreach ($headers as $key => $value) { |
---|
153 | if (strcasecmp($key, 'From') === 0) { |
---|
154 | $include_dir = realpath(dirname( __FILE__)); |
---|
155 | include_once $include_dir ."/../Mail/RFC822.php"; |
---|
156 | $parser = &new Mail_RFC822(); |
---|
157 | $addresses = $parser->parseAddressList($value, 'localhost', false); |
---|
158 | if (PEAR::isError($addresses)) { |
---|
159 | return $addresses; |
---|
160 | } |
---|
161 | |
---|
162 | $from = $addresses[0]->mailbox . '@' . $addresses[0]->host; |
---|
163 | |
---|
164 | // Reject envelope From: addresses with spaces. |
---|
165 | if (strstr($from, ' ')) { |
---|
166 | return false; |
---|
167 | } |
---|
168 | |
---|
169 | $lines[] = $key . ': ' . $value; |
---|
170 | } elseif (strcasecmp($key, 'Received') === 0) { |
---|
171 | $received = array(); |
---|
172 | if (is_array($value)) { |
---|
173 | foreach ($value as $line) { |
---|
174 | $received[] = $key . ': ' . $line; |
---|
175 | } |
---|
176 | } |
---|
177 | else { |
---|
178 | $received[] = $key . ': ' . $value; |
---|
179 | } |
---|
180 | // Put Received: headers at the top. Spam detectors often |
---|
181 | // flag messages with Received: headers after the Subject: |
---|
182 | // as spam. |
---|
183 | $lines = array_merge($received, $lines); |
---|
184 | } else { |
---|
185 | // If $value is an array (i.e., a list of addresses), convert |
---|
186 | // it to a comma-delimited string of its elements (addresses). |
---|
187 | if (is_array($value)) { |
---|
188 | $value = implode(', ', $value); |
---|
189 | } |
---|
190 | $lines[] = $key . ': ' . $value; |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | return array($from, join($this->sep, $lines)); |
---|
195 | } |
---|
196 | |
---|
197 | /** |
---|
198 | * Take a set of recipients and parse them, returning an array of |
---|
199 | * bare addresses (forward paths) that can be passed to sendmail |
---|
200 | * or an smtp server with the rcpt to: command. |
---|
201 | * |
---|
202 | * @param mixed Either a comma-seperated list of recipients |
---|
203 | * (RFC822 compliant), or an array of recipients, |
---|
204 | * each RFC822 valid. |
---|
205 | * |
---|
206 | * @return mixed An array of forward paths (bare addresses) or a PEAR_Error |
---|
207 | * object if the address list could not be parsed. |
---|
208 | * @access private |
---|
209 | */ |
---|
210 | function parseRecipients($recipients) |
---|
211 | { |
---|
212 | include_once dirname(__FILE__) . "/../Mail/RFC822.php"; |
---|
213 | |
---|
214 | // if we're passed an array, assume addresses are valid and |
---|
215 | // implode them before parsing. |
---|
216 | if (is_array($recipients)) { |
---|
217 | $recipients = implode(', ', $recipients); |
---|
218 | } |
---|
219 | |
---|
220 | // Parse recipients, leaving out all personal info. This is |
---|
221 | // for smtp recipients, etc. All relevant personal information |
---|
222 | // should already be in the headers. |
---|
223 | $addresses = Mail_RFC822::parseAddressList($recipients, 'localhost', false); |
---|
224 | |
---|
225 | // If parseAddressList() returned a PEAR_Error object, just return it. |
---|
226 | if (PEAR::isError($addresses)) { |
---|
227 | return $addresses; |
---|
228 | } |
---|
229 | |
---|
230 | $recipients = array(); |
---|
231 | if (is_array($addresses)) { |
---|
232 | foreach ($addresses as $ob) { |
---|
233 | $recipients[] = $ob->mailbox . '@' . $ob->host; |
---|
234 | } |
---|
235 | } |
---|
236 | |
---|
237 | return $recipients; |
---|
238 | } |
---|
239 | |
---|
240 | } |
---|