3 // +----------------------------------------------------------------------+
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2002 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 // | Authors: Richard Heyes <richard@phpguru.org> |
17 // +----------------------------------------------------------------------+
21 * Raw mime encoding class
24 * This class enables you to manipulate and build
25 * a mime email from the ground up.
27 * Why use this instead of mime.php?
28 * mime.php is a userfriendly api to this class for
29 * people who aren't interested in the internals of
30 * mime mail. This class however allows full control
35 * // Since multipart/mixed has no real body, (the body is
36 * // the subpart), we set the body argument to blank.
38 * $params['content_type'] = 'multipart/mixed';
39 * $email = new Mail_mimePart('', $params);
41 * // Here we add a text part to the multipart we have
42 * // already. Assume $body contains plain text.
44 * $params['content_type'] = 'text/plain';
45 * $params['encoding'] = '7bit';
46 * $text = $email->addSubPart($body, $params);
48 * // Now add an attachment. Assume $attach is
49 * the contents of the attachment
51 * $params['content_type'] = 'application/zip';
52 * $params['encoding'] = 'base64';
53 * $params['disposition'] = 'attachment';
54 * $params['dfilename'] = 'example.zip';
55 * $attach =& $email->addSubPart($body, $params);
57 * // Now build the email. Note that the encode
58 * // function returns an associative array containing two
59 * // elements, body and headers. You will need to add extra
60 * // headers, (eg. Mime-Version) before sending.
62 * $email = $message->encode();
63 * $email['headers'][] = 'Mime-Version: 1.0';
66 * Further examples are available at http://www.phpguru.org
69 * - Set encode() to return the $obj->encoded if encode()
70 * has already been run. Unless a flag is passed to specifically
71 * re-build the message.
73 * @author Richard Heyes <richard@phpguru.org>
74 * @version $Revision: 1.1 $
81 * The encoding type of this part
87 * An array of subparts
93 * The output of this part after being built
99 * Headers for this part
105 * The body of this part (not encoded)
113 * Sets up the object.
115 * @param $body - The body of the mime part if any.
116 * @param $params - An associative array of parameters:
117 * content_type - The content type for this part eg multipart/mixed
118 * encoding - The encoding to use, 7bit, 8bit, base64, or quoted-printable
119 * cid - Content ID to apply
120 * disposition - Content disposition, inline or attachment
121 * dfilename - Optional filename parameter for content disposition
122 * description - Content description
123 * charset - Character set to use
126 function Mail_mimePart($body = '', $params = array())
128 if (!defined('MAIL_MIMEPART_CRLF')) {
129 define('MAIL_MIMEPART_CRLF', defined('MAIL_MIME_CRLF') ? MAIL_MIME_CRLF
: "\r\n", TRUE);
132 foreach ($params as $key => $value) {
135 $headers['Content-Type'] = $value . (isset($charset) ?
'; charset="' . $charset . '"' : '');
139 $this->_encoding
= $value;
140 $headers['Content-Transfer-Encoding'] = $value;
144 $headers['Content-ID'] = '<' . $value . '>';
148 $headers['Content-Disposition'] = $value . (isset($dfilename) ?
'; filename="' . $dfilename . '"' : '');
152 if (isset($headers['Content-Disposition'])) {
153 $headers['Content-Disposition'] .= '; filename="' . $value . '"';
160 $headers['Content-Description'] = $value;
164 if (isset($headers['Content-Type'])) {
165 $headers['Content-Type'] .= '; charset="' . $value . '"';
173 // Default content-type
174 if (!isset($headers['Content-Type'])) {
175 $headers['Content-Type'] = 'text/plain';
179 if (!isset($this->_encoding
)) {
180 $this->_encoding
= '7bit';
183 // Assign stuff to member variables
184 $this->_encoded
= array();
185 $this->_headers
= $headers;
186 $this->_body
= $body;
192 * Encodes and returns the email. Also stores
193 * it in the encoded member variable
195 * @return An associative array containing two elements,
196 * body and headers. The headers element is itself
202 $encoded =& $this->_encoded
;
204 if (!empty($this->_subparts
)) {
205 srand((double)microtime()*1000000);
206 $boundary = '=_' . md5(uniqid(rand()) . microtime());
207 $this->_headers
['Content-Type'] .= ';' . MAIL_MIMEPART_CRLF
. "\t" . 'boundary="' . $boundary . '"';
209 // Add body parts to $subparts
210 for ($i = 0; $i < count($this->_subparts
); $i++
) {
212 $tmp = $this->_subparts
[$i]->encode();
213 foreach ($tmp['headers'] as $key => $value) {
214 $headers[] = $key . ': ' . $value;
216 $subparts[] = implode(MAIL_MIMEPART_CRLF
, $headers) . MAIL_MIMEPART_CRLF
. MAIL_MIMEPART_CRLF
. $tmp['body'];
219 $encoded['body'] = '--' . $boundary . MAIL_MIMEPART_CRLF
.
220 implode('--' . $boundary . MAIL_MIMEPART_CRLF
, $subparts) .
221 '--' . $boundary.'--' . MAIL_MIMEPART_CRLF
;
224 $encoded['body'] = $this->_getEncodedData($this->_body
, $this->_encoding
) . MAIL_MIMEPART_CRLF
;
227 // Add headers to $encoded
228 $encoded['headers'] =& $this->_headers
;
236 * Adds a subpart to current mime part and returns
239 * @param $body The body of the subpart, if any.
240 * @param $params The parameters for the subpart, same
241 * as the $params argument for constructor.
242 * @return A reference to the part you just added. It is
243 * crucial if using multipart/* in your subparts that
244 * you use =& in your script when calling this function,
245 * otherwise you will not be able to add further subparts.
248 function &addSubPart($body, $params)
250 $this->_subparts
[] = new Mail_mimePart($body, $params);
251 return $this->_subparts
[count($this->_subparts
) - 1];
257 * Returns encoded data based upon encoding passed to it
259 * @param $data The data to encode.
260 * @param $encoding The encoding type to use, 7bit, base64,
261 * or quoted-printable.
264 function _getEncodedData($data, $encoding)
272 case 'quoted-printable':
273 return $this->_quotedPrintableEncode($data);
277 return rtrim(chunk_split(base64_encode($data), 76, MAIL_MIMEPART_CRLF
));
286 * quoteadPrintableEncode()
288 * Encodes data to quoted-printable standard.
290 * @param $input The data to encode
291 * @param $line_max Optional max line length. Should
292 * not be more than 76 chars
296 function _quotedPrintableEncode($input , $line_max = 76)
298 $lines = preg_split("/\r?\n/", $input);
299 $eol = MAIL_MIMEPART_CRLF
;
303 while(list(, $line) = each($lines)){
305 $linlen = strlen($line);
308 for ($i = 0; $i < $linlen; $i++
) {
309 $char = substr($line, $i, 1);
312 if (($dec == 32) AND ($i == ($linlen - 1))){ // convert space at eol only
315 } elseif($dec == 9) {
316 ; // Do nothing if a tab.
317 } elseif(($dec == 61) OR ($dec < 32 ) OR ($dec > 126)) {
318 $char = $escape . strtoupper(sprintf('%02s', dechex($dec)));
321 if ((strlen($newline) +
strlen($char)) >= $line_max) { // MAIL_MIMEPART_CRLF is not counted
322 $output .= $newline . $escape . $eol; // soft line break; " =\r\n" is okay
327 $output .= $newline . $eol;
329 $output = substr($output, 0, -1 * strlen($eol)); // Don't want last crlf