3 * The Mail_mimePart class is used to create MIME E-mail messages
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
9 * This class however allows full control over the email.
11 * Compatible with PHP versions 4 and 5
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.
18 * Redistribution and use in source and binary forms, with or
19 * without modification, are permitted provided that the following
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.
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.
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: mimePart.php 514 2007-03-21 09:54:10Z thomasb $
51 * @link http://pear.php.net/package/Mail_mime
56 * The Mail_mimePart class is used to create MIME E-mail messages
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
62 * This class however allows full control over the email.
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
77 * The encoding type of this part
85 * An array of subparts
93 * The output of this part after being built
101 * Headers for this part
109 * The body of this part (not encoded)
119 * Sets up the object.
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
132 function Mail_mimePart($body = '', $params = array())
134 if (!defined('MAIL_MIMEPART_CRLF')) {
135 define('MAIL_MIMEPART_CRLF', defined('MAIL_MIME_CRLF') ? MAIL_MIME_CRLF : "\r\n", TRUE);
138 foreach ($params as $key => $value) {
141 $headers['Content-Type'] = $value . (isset($charset) ? '; charset="' . $charset . '"' : '');
145 $this->_encoding = $value;
146 $headers['Content-Transfer-Encoding'] = $value;
150 $headers['Content-ID'] = '<' . $value . '>';
154 $headers['Content-Disposition'] = $value . (isset($dfilename) ? '; filename="' . $dfilename . '"' : '');
158 if (isset($headers['Content-Disposition'])) {
159 $headers['Content-Disposition'] .= '; filename="' . $value . '"';
166 $headers['Content-Description'] = $value;
170 if (isset($headers['Content-Type'])) {
171 $headers['Content-Type'] .= '; charset="' . $value . '"';
179 // Default content-type
180 if (!isset($headers['Content-Type'])) {
181 $headers['Content-Type'] = 'text/plain';
185 if (!isset($this->_encoding)) {
186 $this->_encoding = '7bit';
189 // Assign stuff to member variables
190 $this->_encoded = array();
191 $this->_headers = $headers;
192 $this->_body = $body;
198 * Encodes and returns the email. Also stores
199 * it in the encoded member variable
201 * @return An associative array containing two elements,
202 * body and headers. The headers element is itself
208 $encoded =& $this->_encoded;
210 if (!empty($this->_subparts)) {
211 srand((double)microtime()*1000000);
212 $boundary = '=_' . md5(rand() . microtime());
213 $this->_headers['Content-Type'] .= ';' . MAIL_MIMEPART_CRLF . "\t" . 'boundary="' . $boundary . '"';
215 // Add body parts to $subparts
216 for ($i = 0; $i < count($this->_subparts); $i++) {
218 $tmp = $this->_subparts[$i]->encode();
219 foreach ($tmp['headers'] as $key => $value) {
220 $headers[] = $key . ': ' . $value;
222 $subparts[] = implode(MAIL_MIMEPART_CRLF, $headers) . MAIL_MIMEPART_CRLF . MAIL_MIMEPART_CRLF . $tmp['body'];
225 $encoded['body'] = '--' . $boundary . MAIL_MIMEPART_CRLF .
226 implode('--' . $boundary . MAIL_MIMEPART_CRLF, $subparts) .
227 '--' . $boundary.'--' . MAIL_MIMEPART_CRLF . MAIL_MIMEPART_CRLF;
230 $encoded['body'] = $this->_getEncodedData($this->_body, $this->_encoding) . MAIL_MIMEPART_CRLF;
233 // Add headers to $encoded
234 $encoded['headers'] =& $this->_headers;
242 * Adds a subpart to current mime part and returns
245 * @param $body The body of the subpart, if any.
246 * @param $params The parameters for the subpart, same
247 * as the $params argument for constructor.
248 * @return A reference to the part you just added. It is
249 * crucial if using multipart/* in your subparts that
250 * you use =& in your script when calling this function,
251 * otherwise you will not be able to add further subparts.
254 function &addSubPart($body, $params)
256 $this->_subparts[] = new Mail_mimePart($body, $params);
257 return $this->_subparts[count($this->_subparts) - 1];
263 * Returns encoded data based upon encoding passed to it
265 * @param $data The data to encode.
266 * @param $encoding The encoding type to use, 7bit, base64,
267 * or quoted-printable.
270 function _getEncodedData($data, $encoding)
278 case 'quoted-printable':
279 return $this->_quotedPrintableEncode($data);
283 return rtrim(chunk_split(base64_encode($data), 76, MAIL_MIMEPART_CRLF));
292 * quotedPrintableEncode()
294 * Encodes data to quoted-printable standard.
296 * @param $input The data to encode
297 * @param $line_max Optional max line length. Should
298 * not be more than 76 chars
302 function _quotedPrintableEncode($input , $line_max = 76)
304 $lines = preg_split("/\r?\n/", $input);
305 $eol = MAIL_MIMEPART_CRLF;
309 while(list(, $line) = each($lines)){
311 $line = preg_split('||', $line, -1, PREG_SPLIT_NO_EMPTY);
312 $linlen = count($line);
315 for ($i = 0; $i < $linlen; $i++) {
319 if (($dec == 32) AND ($i == ($linlen - 1))){ // convert space at eol only
322 } elseif(($dec == 9) AND ($i == ($linlen - 1))) { // convert tab at eol only
324 } elseif($dec == 9) {
325 ; // Do nothing if a tab.
326 } elseif(($dec == 61) OR ($dec < 32 ) OR ($dec > 126)) {
327 $char = $escape . strtoupper(sprintf('%02s', dechex($dec)));
330 if ((strlen($newline) + strlen($char)) >= $line_max) { // MAIL_MIMEPART_CRLF is not counted
331 $output .= $newline . $escape . $eol; // soft line break; " =\r\n" is okay
336 $output .= $newline . $eol;
338 $output = substr($output, 0, -1 * strlen($eol)); // Don't want last crlf