| 1 | <?php |
|---|
| 2 | /** |
|---|
| 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 | |
|---|
| 54 | |
|---|
| 55 | /** |
|---|
| 56 | * The Mail_mimePart class is used to create MIME E-mail messages |
|---|
| 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 | */ |
|---|
| 74 | class Mail_mimePart { |
|---|
| 75 | |
|---|
| 76 | /** |
|---|
| 77 | * The encoding type of this part |
|---|
| 78 | * |
|---|
| 79 | * @var string |
|---|
| 80 | * @access private |
|---|
| 81 | */ |
|---|
| 82 | var $_encoding; |
|---|
| 83 | |
|---|
| 84 | /** |
|---|
| 85 | * An array of subparts |
|---|
| 86 | * |
|---|
| 87 | * @var array |
|---|
| 88 | * @access private |
|---|
| 89 | */ |
|---|
| 90 | var $_subparts; |
|---|
| 91 | |
|---|
| 92 | /** |
|---|
| 93 | * The output of this part after being built |
|---|
| 94 | * |
|---|
| 95 | * @var string |
|---|
| 96 | * @access private |
|---|
| 97 | */ |
|---|
| 98 | var $_encoded; |
|---|
| 99 | |
|---|
| 100 | /** |
|---|
| 101 | * Headers for this part |
|---|
| 102 | * |
|---|
| 103 | * @var array |
|---|
| 104 | * @access private |
|---|
| 105 | */ |
|---|
| 106 | var $_headers; |
|---|
| 107 | |
|---|
| 108 | /** |
|---|
| 109 | * The body of this part (not encoded) |
|---|
| 110 | * |
|---|
| 111 | * @var string |
|---|
| 112 | * @access private |
|---|
| 113 | */ |
|---|
| 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 | |
|---|
| 138 | foreach ($params as $key => $value) { |
|---|
| 139 | switch ($key) { |
|---|
| 140 | case 'content_type': |
|---|
| 141 | $headers['Content-Type'] = $value . (isset($charset) ? '; charset="' . $charset . '"' : ''); |
|---|
| 142 | break; |
|---|
| 143 | |
|---|
| 144 | case 'encoding': |
|---|
| 145 | $this->_encoding = $value; |
|---|
| 146 | $headers['Content-Transfer-Encoding'] = $value; |
|---|
| 147 | break; |
|---|
| 148 | |
|---|
| 149 | case 'cid': |
|---|
| 150 | $headers['Content-ID'] = '<' . $value . '>'; |
|---|
| 151 | break; |
|---|
| 152 | |
|---|
| 153 | case 'disposition': |
|---|
| 154 | $headers['Content-Disposition'] = $value . (isset($dfilename) ? '; filename="' . $dfilename . '"' : ''); |
|---|
| 155 | break; |
|---|
| 156 | |
|---|
| 157 | case 'dfilename': |
|---|
| 158 | if (isset($headers['Content-Disposition'])) { |
|---|
| 159 | $headers['Content-Disposition'] .= '; filename="' . $value . '"'; |
|---|
| 160 | } else { |
|---|
| 161 | $dfilename = $value; |
|---|
| 162 | } |
|---|
| 163 | break; |
|---|
| 164 | |
|---|
| 165 | case 'description': |
|---|
| 166 | $headers['Content-Description'] = $value; |
|---|
| 167 | break; |
|---|
| 168 | |
|---|
| 169 | case 'charset': |
|---|
| 170 | if (isset($headers['Content-Type'])) { |
|---|
| 171 | $headers['Content-Type'] .= '; charset="' . $value . '"'; |
|---|
| 172 | } else { |
|---|
| 173 | $charset = $value; |
|---|
| 174 | } |
|---|
| 175 | break; |
|---|
| 176 | } |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | // Default content-type |
|---|
| 180 | if (!isset($headers['Content-Type'])) { |
|---|
| 181 | $headers['Content-Type'] = 'text/plain'; |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | //Default encoding |
|---|
| 185 | if (!isset($this->_encoding)) { |
|---|
| 186 | $this->_encoding = '7bit'; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | // Assign stuff to member variables |
|---|
| 190 | $this->_encoded = array(); |
|---|
| 191 | $this->_headers = $headers; |
|---|
| 192 | $this->_body = $body; |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | /** |
|---|
| 196 | * encode() |
|---|
| 197 | * |
|---|
| 198 | * Encodes and returns the email. Also stores |
|---|
| 199 | * it in the encoded member variable |
|---|
| 200 | * |
|---|
| 201 | * @return An associative array containing two elements, |
|---|
| 202 | * body and headers. The headers element is itself |
|---|
| 203 | * an indexed array. |
|---|
| 204 | * @access public |
|---|
| 205 | */ |
|---|
| 206 | function encode() |
|---|
| 207 | { |
|---|
| 208 | $encoded =& $this->_encoded; |
|---|
| 209 | |
|---|
| 210 | if (!empty($this->_subparts)) { |
|---|
| 211 | // http://pear.php.net/bugs/bug.php?id=13032 |
|---|
| 212 | // srand((double)microtime()*1000000); |
|---|
| 213 | $boundary = '=_' . md5(rand() . microtime()); |
|---|
| 214 | $this->_headers['Content-Type'] .= ';' . MAIL_MIMEPART_CRLF . "\t" . 'boundary="' . $boundary . '"'; |
|---|
| 215 | |
|---|
| 216 | // Add body parts to $subparts |
|---|
| 217 | for ($i = 0; $i < count($this->_subparts); $i++) { |
|---|
| 218 | $headers = array(); |
|---|
| 219 | $tmp = $this->_subparts[$i]->encode(); |
|---|
| 220 | foreach ($tmp['headers'] as $key => $value) { |
|---|
| 221 | $headers[] = $key . ': ' . $value; |
|---|
| 222 | } |
|---|
| 223 | $subparts[] = implode(MAIL_MIMEPART_CRLF, $headers) . MAIL_MIMEPART_CRLF . MAIL_MIMEPART_CRLF . $tmp['body']; |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | $encoded['body'] = '--' . $boundary . MAIL_MIMEPART_CRLF . |
|---|
| 227 | implode('--' . $boundary . MAIL_MIMEPART_CRLF, $subparts) . |
|---|
| 228 | '--' . $boundary.'--' . MAIL_MIMEPART_CRLF . MAIL_MIMEPART_CRLF; |
|---|
| 229 | |
|---|
| 230 | } else { |
|---|
| 231 | $encoded['body'] = $this->_getEncodedData($this->_body, $this->_encoding) . MAIL_MIMEPART_CRLF; |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | // Add headers to $encoded |
|---|
| 235 | $encoded['headers'] =& $this->_headers; |
|---|
| 236 | |
|---|
| 237 | return $encoded; |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | /** |
|---|
| 241 | * &addSubPart() |
|---|
| 242 | * |
|---|
| 243 | * Adds a subpart to current mime part and returns |
|---|
| 244 | * a reference to it |
|---|
| 245 | * |
|---|
| 246 | * @param $body The body of the subpart, if any. |
|---|
| 247 | * @param $params The parameters for the subpart, same |
|---|
| 248 | * as the $params argument for constructor. |
|---|
| 249 | * @return A reference to the part you just added. It is |
|---|
| 250 | * crucial if using multipart/* in your subparts that |
|---|
| 251 | * you use =& in your script when calling this function, |
|---|
| 252 | * otherwise you will not be able to add further subparts. |
|---|
| 253 | * @access public |
|---|
| 254 | */ |
|---|
| 255 | function &addSubPart($body, $params) |
|---|
| 256 | { |
|---|
| 257 | $this->_subparts[] = new Mail_mimePart($body, $params); |
|---|
| 258 | return $this->_subparts[count($this->_subparts) - 1]; |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | /** |
|---|
| 262 | * _getEncodedData() |
|---|
| 263 | * |
|---|
| 264 | * Returns encoded data based upon encoding passed to it |
|---|
| 265 | * |
|---|
| 266 | * @param $data The data to encode. |
|---|
| 267 | * @param $encoding The encoding type to use, 7bit, base64, |
|---|
| 268 | * or quoted-printable. |
|---|
| 269 | * @access private |
|---|
| 270 | */ |
|---|
| 271 | function _getEncodedData($data, $encoding) |
|---|
| 272 | { |
|---|
| 273 | switch ($encoding) { |
|---|
| 274 | case '8bit': |
|---|
| 275 | case '7bit': |
|---|
| 276 | return $data; |
|---|
| 277 | break; |
|---|
| 278 | |
|---|
| 279 | case 'quoted-printable': |
|---|
| 280 | return $this->_quotedPrintableEncode($data); |
|---|
| 281 | break; |
|---|
| 282 | |
|---|
| 283 | case 'base64': |
|---|
| 284 | return rtrim(chunk_split(base64_encode($data), 76, MAIL_MIMEPART_CRLF)); |
|---|
| 285 | break; |
|---|
| 286 | |
|---|
| 287 | default: |
|---|
| 288 | return $data; |
|---|
| 289 | } |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | /** |
|---|
| 293 | * quotedPrintableEncode() |
|---|
| 294 | * |
|---|
| 295 | * Encodes data to quoted-printable standard. |
|---|
| 296 | * |
|---|
| 297 | * @param $input The data to encode |
|---|
| 298 | * @param $line_max Optional max line length. Should |
|---|
| 299 | * not be more than 76 chars |
|---|
| 300 | * |
|---|
| 301 | * @access private |
|---|
| 302 | */ |
|---|
| 303 | function _quotedPrintableEncode($input , $line_max = 76) |
|---|
| 304 | { |
|---|
| 305 | $lines = preg_split("/\r?\n/", $input); |
|---|
| 306 | $eol = MAIL_MIMEPART_CRLF; |
|---|
| 307 | $escape = '='; |
|---|
| 308 | $output = ''; |
|---|
| 309 | |
|---|
| 310 | while(list(, $line) = each($lines)){ |
|---|
| 311 | |
|---|
| 312 | $line = preg_split('||', $line, -1, PREG_SPLIT_NO_EMPTY); |
|---|
| 313 | $linlen = count($line); |
|---|
| 314 | $newline = ''; |
|---|
| 315 | |
|---|
| 316 | for ($i = 0; $i < $linlen; $i++) { |
|---|
| 317 | $char = $line[$i]; |
|---|
| 318 | $dec = ord($char); |
|---|
| 319 | |
|---|
| 320 | if (($dec == 32) AND ($i == ($linlen - 1))){ // convert space at eol only |
|---|
| 321 | $char = '=20'; |
|---|
| 322 | |
|---|
| 323 | } elseif(($dec == 9) AND ($i == ($linlen - 1))) { // convert tab at eol only |
|---|
| 324 | $char = '=09'; |
|---|
| 325 | } elseif($dec == 9) { |
|---|
| 326 | ; // Do nothing if a tab. |
|---|
| 327 | } elseif(($dec == 61) OR ($dec < 32 ) OR ($dec > 126)) { |
|---|
| 328 | $char = $escape . strtoupper(sprintf('%02s', dechex($dec))); |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | if ((strlen($newline) + strlen($char)) >= $line_max) { // MAIL_MIMEPART_CRLF is not counted |
|---|
| 332 | $output .= $newline . $escape . $eol; // soft line break; " =\r\n" is okay |
|---|
| 333 | $newline = ''; |
|---|
| 334 | } |
|---|
| 335 | $newline .= $char; |
|---|
| 336 | } // end of for |
|---|
| 337 | $output .= $newline . $eol; |
|---|
| 338 | } |
|---|
| 339 | $output = substr($output, 0, -1 * strlen($eol)); // Don't want last crlf |
|---|
| 340 | return $output; |
|---|
| 341 | } |
|---|
| 342 | } // End of class |
|---|