commit | author | age
|
4e17e6
|
1 |
<?php |
ab6f80
|
2 |
/** |
T |
3 |
* The Mail_mimePart class is used to create MIME E-mail messages |
|
4 |
* |
|
5 |
* This class enables you to manipulate and build a mime email |
|
6 |
* from the ground up. The Mail_Mime class is a userfriendly api |
|
7 |
* to this class for people who aren't interested in the internals |
|
8 |
* of mime mail. |
|
9 |
* This class however allows full control over the email. |
|
10 |
* |
|
11 |
* Compatible with PHP versions 4 and 5 |
|
12 |
* |
|
13 |
* LICENSE: This LICENSE is in the BSD license style. |
|
14 |
* Copyright (c) 2002-2003, Richard Heyes <richard@phpguru.org> |
|
15 |
* Copyright (c) 2003-2006, PEAR <pear-group@php.net> |
|
16 |
* All rights reserved. |
|
17 |
* |
|
18 |
* Redistribution and use in source and binary forms, with or |
|
19 |
* without modification, are permitted provided that the following |
|
20 |
* conditions are met: |
|
21 |
* |
|
22 |
* - Redistributions of source code must retain the above copyright |
|
23 |
* notice, this list of conditions and the following disclaimer. |
|
24 |
* - Redistributions in binary form must reproduce the above copyright |
|
25 |
* notice, this list of conditions and the following disclaimer in the |
|
26 |
* documentation and/or other materials provided with the distribution. |
|
27 |
* - Neither the name of the authors, nor the names of its contributors |
|
28 |
* may be used to endorse or promote products derived from this |
|
29 |
* software without specific prior written permission. |
|
30 |
* |
|
31 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
32 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
33 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
34 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
|
35 |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
36 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
37 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
38 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
39 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
40 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
|
41 |
* THE POSSIBILITY OF SUCH DAMAGE. |
|
42 |
* |
|
43 |
* @category Mail |
|
44 |
* @package Mail_Mime |
|
45 |
* @author Richard Heyes <richard@phpguru.org> |
|
46 |
* @author Cipriano Groenendal <cipri@php.net> |
|
47 |
* @author Sean Coates <sean@php.net> |
|
48 |
* @copyright 2003-2006 PEAR <pear-group@php.net> |
|
49 |
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
50 |
* @version CVS: $Id$ |
|
51 |
* @link http://pear.php.net/package/Mail_mime |
|
52 |
*/ |
|
53 |
|
4e17e6
|
54 |
|
T |
55 |
/** |
ab6f80
|
56 |
* The Mail_mimePart class is used to create MIME E-mail messages |
T |
57 |
* |
|
58 |
* This class enables you to manipulate and build a mime email |
|
59 |
* from the ground up. The Mail_Mime class is a userfriendly api |
|
60 |
* to this class for people who aren't interested in the internals |
|
61 |
* of mime mail. |
|
62 |
* This class however allows full control over the email. |
|
63 |
* |
|
64 |
* @category Mail |
|
65 |
* @package Mail_Mime |
|
66 |
* @author Richard Heyes <richard@phpguru.org> |
|
67 |
* @author Cipriano Groenendal <cipri@php.net> |
|
68 |
* @author Sean Coates <sean@php.net> |
|
69 |
* @copyright 2003-2006 PEAR <pear-group@php.net> |
|
70 |
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
71 |
* @version Release: @package_version@ |
|
72 |
* @link http://pear.php.net/package/Mail_mime |
|
73 |
*/ |
4e17e6
|
74 |
class Mail_mimePart { |
T |
75 |
|
|
76 |
/** |
|
77 |
* The encoding type of this part |
ab6f80
|
78 |
* |
4e17e6
|
79 |
* @var string |
ab6f80
|
80 |
* @access private |
4e17e6
|
81 |
*/ |
T |
82 |
var $_encoding; |
|
83 |
|
|
84 |
/** |
|
85 |
* An array of subparts |
ab6f80
|
86 |
* |
4e17e6
|
87 |
* @var array |
ab6f80
|
88 |
* @access private |
4e17e6
|
89 |
*/ |
T |
90 |
var $_subparts; |
|
91 |
|
|
92 |
/** |
|
93 |
* The output of this part after being built |
ab6f80
|
94 |
* |
4e17e6
|
95 |
* @var string |
ab6f80
|
96 |
* @access private |
4e17e6
|
97 |
*/ |
T |
98 |
var $_encoded; |
|
99 |
|
|
100 |
/** |
|
101 |
* Headers for this part |
ab6f80
|
102 |
* |
4e17e6
|
103 |
* @var array |
ab6f80
|
104 |
* @access private |
4e17e6
|
105 |
*/ |
T |
106 |
var $_headers; |
|
107 |
|
|
108 |
/** |
|
109 |
* The body of this part (not encoded) |
ab6f80
|
110 |
* |
4e17e6
|
111 |
* @var string |
ab6f80
|
112 |
* @access private |
4e17e6
|
113 |
*/ |
T |
114 |
var $_body; |
|
115 |
|
|
116 |
/** |
|
117 |
* Constructor. |
|
118 |
* |
|
119 |
* Sets up the object. |
|
120 |
* |
|
121 |
* @param $body - The body of the mime part if any. |
|
122 |
* @param $params - An associative array of parameters: |
|
123 |
* content_type - The content type for this part eg multipart/mixed |
|
124 |
* encoding - The encoding to use, 7bit, 8bit, base64, or quoted-printable |
|
125 |
* cid - Content ID to apply |
|
126 |
* disposition - Content disposition, inline or attachment |
|
127 |
* dfilename - Optional filename parameter for content disposition |
|
128 |
* description - Content description |
|
129 |
* charset - Character set to use |
|
130 |
* @access public |
|
131 |
*/ |
|
132 |
function Mail_mimePart($body = '', $params = array()) |
|
133 |
{ |
|
134 |
if (!defined('MAIL_MIMEPART_CRLF')) { |
|
135 |
define('MAIL_MIMEPART_CRLF', defined('MAIL_MIME_CRLF') ? MAIL_MIME_CRLF : "\r\n", TRUE); |
|
136 |
} |
|
137 |
|
ee289d
|
138 |
$contentType = array(); |
A |
139 |
$contentDisp = array(); |
4e17e6
|
140 |
foreach ($params as $key => $value) { |
T |
141 |
switch ($key) { |
|
142 |
case 'content_type': |
ee289d
|
143 |
$contentType['type'] = $value; |
A |
144 |
//$headers['Content-Type'] = $value . (isset($charset) ? '; charset="' . $charset . '"' : ''); |
4e17e6
|
145 |
break; |
T |
146 |
|
|
147 |
case 'encoding': |
|
148 |
$this->_encoding = $value; |
|
149 |
$headers['Content-Transfer-Encoding'] = $value; |
|
150 |
break; |
|
151 |
|
|
152 |
case 'cid': |
|
153 |
$headers['Content-ID'] = '<' . $value . '>'; |
|
154 |
break; |
|
155 |
|
|
156 |
case 'disposition': |
ee289d
|
157 |
$contentDisp['disp'] = $value; |
4e17e6
|
158 |
break; |
T |
159 |
|
|
160 |
case 'dfilename': |
ee289d
|
161 |
$contentDisp['filename'] = $value; |
A |
162 |
$contentType['name'] = $value; |
4e17e6
|
163 |
break; |
T |
164 |
|
|
165 |
case 'description': |
|
166 |
$headers['Content-Description'] = $value; |
|
167 |
break; |
|
168 |
|
|
169 |
case 'charset': |
ee289d
|
170 |
$contentType['charset'] = $value; |
A |
171 |
$contentDisp['charset'] = $value; |
4e17e6
|
172 |
break; |
ee289d
|
173 |
|
A |
174 |
case 'language': |
|
175 |
$contentType['language'] = $value; |
|
176 |
$contentDisp['language'] = $value; |
|
177 |
break; |
|
178 |
|
|
179 |
case 'location': |
|
180 |
$headers['Content-Location'] = $value; |
|
181 |
break; |
|
182 |
|
|
183 |
} |
|
184 |
} |
|
185 |
if (isset($contentType['type'])) { |
|
186 |
$headers['Content-Type'] = $contentType['type']; |
|
187 |
if (isset($contentType['name'])) { |
|
188 |
$headers['Content-Type'] .= ';' . MAIL_MIMEPART_CRLF; |
|
189 |
$headers['Content-Type'] .= $this->_buildHeaderParam('name', $contentType['name'], |
|
190 |
isset($contentType['charset']) ? $contentType['charset'] : 'US-ASCII', |
|
191 |
isset($contentType['language']) ? $contentType['language'] : NULL); |
|
192 |
} elseif (isset($contentType['charset'])) { |
|
193 |
$headers['Content-Type'] .= "; charset=\"{$contentType['charset']}\""; |
4e17e6
|
194 |
} |
T |
195 |
} |
|
196 |
|
ee289d
|
197 |
|
A |
198 |
if (isset($contentDisp['disp'])) { |
|
199 |
$headers['Content-Disposition'] = $contentDisp['disp']; |
|
200 |
if (isset($contentDisp['filename'])) { |
|
201 |
$headers['Content-Disposition'] .= ';' . MAIL_MIMEPART_CRLF; |
|
202 |
$headers['Content-Disposition'] .= $this->_buildHeaderParam('filename', $contentDisp['filename'], |
|
203 |
isset($contentDisp['charset']) ? $contentDisp['charset'] : 'US-ASCII', |
|
204 |
isset($contentDisp['language']) ? $contentDisp['language'] : NULL); |
|
205 |
} |
|
206 |
} |
|
207 |
|
|
208 |
|
|
209 |
|
|
210 |
|
4e17e6
|
211 |
// Default content-type |
T |
212 |
if (!isset($headers['Content-Type'])) { |
|
213 |
$headers['Content-Type'] = 'text/plain'; |
|
214 |
} |
|
215 |
|
|
216 |
//Default encoding |
|
217 |
if (!isset($this->_encoding)) { |
|
218 |
$this->_encoding = '7bit'; |
|
219 |
} |
|
220 |
|
|
221 |
// Assign stuff to member variables |
|
222 |
$this->_encoded = array(); |
|
223 |
$this->_headers = $headers; |
|
224 |
$this->_body = $body; |
|
225 |
} |
|
226 |
|
|
227 |
/** |
|
228 |
* encode() |
|
229 |
* |
|
230 |
* Encodes and returns the email. Also stores |
|
231 |
* it in the encoded member variable |
|
232 |
* |
|
233 |
* @return An associative array containing two elements, |
|
234 |
* body and headers. The headers element is itself |
|
235 |
* an indexed array. |
|
236 |
* @access public |
|
237 |
*/ |
|
238 |
function encode() |
|
239 |
{ |
|
240 |
$encoded =& $this->_encoded; |
|
241 |
|
ee289d
|
242 |
if (count($this->_subparts)) { |
A |
243 |
srand((double)microtime()*1000000); |
4e17e6
|
244 |
$boundary = '=_' . md5(rand() . microtime()); |
T |
245 |
$this->_headers['Content-Type'] .= ';' . MAIL_MIMEPART_CRLF . "\t" . 'boundary="' . $boundary . '"'; |
|
246 |
|
|
247 |
// Add body parts to $subparts |
|
248 |
for ($i = 0; $i < count($this->_subparts); $i++) { |
|
249 |
$headers = array(); |
|
250 |
$tmp = $this->_subparts[$i]->encode(); |
|
251 |
foreach ($tmp['headers'] as $key => $value) { |
|
252 |
$headers[] = $key . ': ' . $value; |
|
253 |
} |
ee289d
|
254 |
$subparts[] = implode(MAIL_MIMEPART_CRLF, $headers) . MAIL_MIMEPART_CRLF . MAIL_MIMEPART_CRLF . $tmp['body'] . MAIL_MIMEPART_CRLF; |
4e17e6
|
255 |
} |
T |
256 |
|
ee289d
|
257 |
$encoded['body'] = '--' . $boundary . MAIL_MIMEPART_CRLF . |
A |
258 |
rtrim(implode('--' . $boundary . MAIL_MIMEPART_CRLF , $subparts), MAIL_MIMEPART_CRLF) . MAIL_MIMEPART_CRLF . |
|
259 |
'--' . $boundary.'--' . MAIL_MIMEPART_CRLF; |
4e17e6
|
260 |
|
T |
261 |
} else { |
ee289d
|
262 |
$encoded['body'] = $this->_getEncodedData($this->_body, $this->_encoding); |
4e17e6
|
263 |
} |
T |
264 |
|
|
265 |
// Add headers to $encoded |
|
266 |
$encoded['headers'] =& $this->_headers; |
|
267 |
|
|
268 |
return $encoded; |
|
269 |
} |
|
270 |
|
|
271 |
/** |
|
272 |
* &addSubPart() |
|
273 |
* |
|
274 |
* Adds a subpart to current mime part and returns |
|
275 |
* a reference to it |
|
276 |
* |
|
277 |
* @param $body The body of the subpart, if any. |
|
278 |
* @param $params The parameters for the subpart, same |
|
279 |
* as the $params argument for constructor. |
|
280 |
* @return A reference to the part you just added. It is |
|
281 |
* crucial if using multipart/* in your subparts that |
|
282 |
* you use =& in your script when calling this function, |
|
283 |
* otherwise you will not be able to add further subparts. |
|
284 |
* @access public |
|
285 |
*/ |
|
286 |
function &addSubPart($body, $params) |
|
287 |
{ |
|
288 |
$this->_subparts[] = new Mail_mimePart($body, $params); |
|
289 |
return $this->_subparts[count($this->_subparts) - 1]; |
|
290 |
} |
|
291 |
|
|
292 |
/** |
|
293 |
* _getEncodedData() |
|
294 |
* |
|
295 |
* Returns encoded data based upon encoding passed to it |
|
296 |
* |
|
297 |
* @param $data The data to encode. |
|
298 |
* @param $encoding The encoding type to use, 7bit, base64, |
|
299 |
* or quoted-printable. |
|
300 |
* @access private |
|
301 |
*/ |
|
302 |
function _getEncodedData($data, $encoding) |
|
303 |
{ |
|
304 |
switch ($encoding) { |
|
305 |
case '8bit': |
|
306 |
case '7bit': |
|
307 |
return $data; |
|
308 |
break; |
|
309 |
|
|
310 |
case 'quoted-printable': |
|
311 |
return $this->_quotedPrintableEncode($data); |
|
312 |
break; |
|
313 |
|
|
314 |
case 'base64': |
|
315 |
return rtrim(chunk_split(base64_encode($data), 76, MAIL_MIMEPART_CRLF)); |
|
316 |
break; |
|
317 |
|
|
318 |
default: |
|
319 |
return $data; |
|
320 |
} |
|
321 |
} |
|
322 |
|
|
323 |
/** |
ab6f80
|
324 |
* quotedPrintableEncode() |
4e17e6
|
325 |
* |
T |
326 |
* Encodes data to quoted-printable standard. |
|
327 |
* |
|
328 |
* @param $input The data to encode |
|
329 |
* @param $line_max Optional max line length. Should |
|
330 |
* not be more than 76 chars |
|
331 |
* |
|
332 |
* @access private |
|
333 |
*/ |
|
334 |
function _quotedPrintableEncode($input , $line_max = 76) |
|
335 |
{ |
|
336 |
$lines = preg_split("/\r?\n/", $input); |
|
337 |
$eol = MAIL_MIMEPART_CRLF; |
|
338 |
$escape = '='; |
|
339 |
$output = ''; |
|
340 |
|
ee289d
|
341 |
while (list(, $line) = each($lines)) { |
4e17e6
|
342 |
|
ab6f80
|
343 |
$line = preg_split('||', $line, -1, PREG_SPLIT_NO_EMPTY); |
T |
344 |
$linlen = count($line); |
4e17e6
|
345 |
$newline = ''; |
T |
346 |
|
|
347 |
for ($i = 0; $i < $linlen; $i++) { |
ab6f80
|
348 |
$char = $line[$i]; |
4e17e6
|
349 |
$dec = ord($char); |
T |
350 |
|
ee289d
|
351 |
if (($dec == 32) AND ($i == ($linlen - 1))) { // convert space at eol only |
4e17e6
|
352 |
$char = '=20'; |
T |
353 |
|
ee289d
|
354 |
} elseif (($dec == 9) AND ($i == ($linlen - 1))) { // convert tab at eol only |
4e17e6
|
355 |
$char = '=09'; |
ee289d
|
356 |
} elseif ($dec == 9) { |
4e17e6
|
357 |
; // Do nothing if a tab. |
ee289d
|
358 |
} elseif (($dec == 61) OR ($dec < 32 ) OR ($dec > 126)) { |
4e17e6
|
359 |
$char = $escape . strtoupper(sprintf('%02s', dechex($dec))); |
ee289d
|
360 |
} elseif (($dec == 46) AND (($newline == '') || ((strlen($newline) + strlen("=2E")) >= $line_max))) { |
A |
361 |
//Bug #9722: convert full-stop at bol, |
|
362 |
//some Windows servers need this, won't break anything (cipri) |
|
363 |
//Bug #11731: full-stop at bol also needs to be encoded |
|
364 |
//if this line would push us over the line_max limit. |
|
365 |
$char = '=2E'; |
4e17e6
|
366 |
} |
T |
367 |
|
ee289d
|
368 |
//Note, when changing this line, also change the ($dec == 46) |
A |
369 |
//check line, as it mimics this line due to Bug #11731 |
4e17e6
|
370 |
if ((strlen($newline) + strlen($char)) >= $line_max) { // MAIL_MIMEPART_CRLF is not counted |
T |
371 |
$output .= $newline . $escape . $eol; // soft line break; " =\r\n" is okay |
|
372 |
$newline = ''; |
|
373 |
} |
|
374 |
$newline .= $char; |
|
375 |
} // end of for |
|
376 |
$output .= $newline . $eol; |
|
377 |
} |
|
378 |
$output = substr($output, 0, -1 * strlen($eol)); // Don't want last crlf |
|
379 |
return $output; |
|
380 |
} |
ee289d
|
381 |
|
A |
382 |
/** |
|
383 |
* _buildHeaderParam() |
|
384 |
* |
|
385 |
* Encodes the paramater of a header. |
|
386 |
* |
|
387 |
* @param $name The name of the header-parameter |
|
388 |
* @param $value The value of the paramter |
|
389 |
* @param $charset The characterset of $value |
|
390 |
* @param $language The language used in $value |
|
391 |
* @param $maxLength The maximum length of a line. Defauls to 75 |
|
392 |
* |
|
393 |
* @access private |
|
394 |
*/ |
|
395 |
function _buildHeaderParam($name, $value, $charset=NULL, $language=NULL, $maxLength=75) |
|
396 |
{ |
|
397 |
//If we find chars to encode, or if charset or language |
|
398 |
//is not any of the defaults, we need to encode the value. |
|
399 |
$shouldEncode = 0; |
|
400 |
$secondAsterisk = ''; |
1c2536
|
401 |
if (preg_match('#([ \x80-\xFF \*\'\\%\t(\)\<\>\@\,\;\:\\\"/\[\]\?\=]){1}#', $value)) { |
ee289d
|
402 |
$shouldEncode = 1; |
A |
403 |
} elseif ($charset && (strtolower($charset) != 'us-ascii')) { |
|
404 |
$shouldEncode = 1; |
|
405 |
} elseif ($language && ($language != 'en' && $language != 'en-us')) { |
|
406 |
$shouldEncode = 1; |
|
407 |
} |
|
408 |
if ($shouldEncode) { |
1c2536
|
409 |
$encValue = preg_replace('#([\x80-\xFF \*\'\%\t\(\)\<\>\@\,\;\:\\\"/\[\]\?\=])#e', '"%" . strtoupper(dechex(ord("\1")))', $value); |
ee289d
|
410 |
$value = "$charset'$language'$encValue"; |
A |
411 |
$secondAsterisk = '*'; |
|
412 |
} |
|
413 |
$header = " {$name}{$secondAsterisk}=\"{$value}\"; "; |
|
414 |
if (strlen($header) <= $maxLength) { |
|
415 |
return $header; |
|
416 |
} |
|
417 |
|
|
418 |
$preLength = strlen(" {$name}*0{$secondAsterisk}=\""); |
|
419 |
$sufLength = strlen("\";"); |
|
420 |
$maxLength = MAX(16, $maxLength - $preLength - $sufLength - 2); |
|
421 |
$maxLengthReg = "|(.{0,$maxLength}[^\%][^\%])|"; |
|
422 |
|
|
423 |
$headers = array(); |
|
424 |
$headCount = 0; |
|
425 |
while ($value) { |
|
426 |
$matches = array(); |
|
427 |
$found = preg_match($maxLengthReg, $value, $matches); |
|
428 |
if ($found) { |
|
429 |
$headers[] = " {$name}*{$headCount}{$secondAsterisk}=\"{$matches[0]}\""; |
|
430 |
$value = substr($value, strlen($matches[0])); |
|
431 |
} else { |
|
432 |
$headers[] = " {$name}*{$headCount}{$secondAsterisk}=\"{$value}\""; |
|
433 |
$value = ""; |
|
434 |
} |
|
435 |
$headCount++; |
|
436 |
} |
|
437 |
$headers = implode(MAIL_MIMEPART_CRLF, $headers) . ';'; |
|
438 |
return $headers; |
|
439 |
} |
4e17e6
|
440 |
} // End of class |