Changeset 19941 for branches/version-2_5-dev/data/module/Mail/mime.php
- Timestamp:
- 2011/01/17 14:34:18 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/version-2_5-dev/data/module/Mail/mime.php
r15532 r19941 1 1 <?php 2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */3 // +-----------------------------------------------------------------------+4 // | Copyright (c) 2002-2003 Richard Heyes |5 // | Copyright (c) 2003-2005 The PHP Group |6 // | All rights reserved. |7 // | |8 // | Redistribution and use in source and binary forms, with or without |9 // | modification, are permitted provided that the following conditions |10 // | are met: |11 // | |12 // | o Redistributions of source code must retain the above copyright |13 // | notice, this list of conditions and the following disclaimer. |14 // | o Redistributions in binary form must reproduce the above copyright |15 // | notice, this list of conditions and the following disclaimer in the |16 // | documentation and/or other materials provided with the distribution.|17 // | o The names of the authors may not be used to endorse or promote |18 // | products derived from this software without specific prior written |19 // | permission. |20 // | |21 // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |22 // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |23 // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |24 // | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |25 // | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |26 // | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |27 // | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |28 // | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |29 // | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |30 // | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |31 // | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |32 // | |33 // +-----------------------------------------------------------------------+34 // | Author: Richard Heyes <[email protected]> |35 // | Tomas V.V.Cox <[email protected]> (port to PEAR) |36 // +-----------------------------------------------------------------------+37 //38 // $Id$39 40 require_once(dirname(__FILE__) . '/../PEAR.php');41 require_once(dirname(__FILE__) . '/mimePart.php');42 43 2 /** 44 * Mime mail composer class. Can handle: text and html bodies, embedded html 45 * images and attachments. 46 * Documentation and examples of this class are avaible here: 47 * http://pear.php.net/manual/ 3 * The Mail_Mime class is used to create MIME E-mail messages 48 4 * 49 * @notes This class is based on HTML Mime Mail class from50 * Richard Heyes <[email protected]> which was based also51 * in the mime_mail.class by Tobias Ratschiller <[email protected]> and52 * Sascha Schumann <[email protected]>5 * The Mail_Mime class provides an OO interface to create MIME 6 * enabled email messages. This way you can create emails that 7 * contain plain-text bodies, HTML bodies, attachments, inline 8 * images and specific headers. 53 9 * 54 * @author Richard Heyes <[email protected]> 55 * @author Tomas V.V.Cox <[email protected]> 56 * @package Mail 57 * @access public 10 * Compatible with PHP versions 4 and 5 11 * 12 * LICENSE: This LICENSE is in the BSD license style. 13 * Copyright (c) 2002-2003, Richard Heyes <[email protected]> 14 * Copyright (c) 2003-2006, PEAR <[email protected]> 15 * All rights reserved. 16 * 17 * Redistribution and use in source and binary forms, with or 18 * without modification, are permitted provided that the following 19 * conditions are met: 20 * 21 * - Redistributions of source code must retain the above copyright 22 * notice, this list of conditions and the following disclaimer. 23 * - Redistributions in binary form must reproduce the above copyright 24 * notice, this list of conditions and the following disclaimer in the 25 * documentation and/or other materials provided with the distribution. 26 * - Neither the name of the authors, nor the names of its contributors 27 * may be used to endorse or promote products derived from this 28 * software without specific prior written permission. 29 * 30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 31 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 34 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 40 * THE POSSIBILITY OF SUCH DAMAGE. 41 * 42 * @category Mail 43 * @package Mail_Mime 44 * @author Richard Heyes <[email protected]> 45 * @author Tomas V.V. Cox <[email protected]> 46 * @author Cipriano Groenendal <[email protected]> 47 * @author Sean Coates <[email protected]> 48 * @author Aleksander Machniak <[email protected]> 49 * @copyright 2003-2006 PEAR <[email protected]> 50 * @license http://www.opensource.org/licenses/bsd-license.php BSD License 51 * @version CVS: $Id$ 52 * @link http://pear.php.net/package/Mail_mime 53 * 54 * This class is based on HTML Mime Mail class from 55 * Richard Heyes <[email protected]> which was based also 56 * in the mime_mail.class by Tobias Ratschiller <[email protected]> 57 * and Sascha Schumann <[email protected]> 58 */ 59 60 61 /** 62 * require PEAR 63 * 64 * This package depends on PEAR to raise errors. 65 */ 66 require_once 'PEAR.php'; 67 68 /** 69 * require Mail_mimePart 70 * 71 * Mail_mimePart contains the code required to 72 * create all the different parts a mail can 73 * consist of. 74 */ 75 require_once 'Mail/mimePart.php'; 76 77 78 /** 79 * The Mail_Mime class provides an OO interface to create MIME 80 * enabled email messages. This way you can create emails that 81 * contain plain-text bodies, HTML bodies, attachments, inline 82 * images and specific headers. 83 * 84 * @category Mail 85 * @package Mail_Mime 86 * @author Richard Heyes <[email protected]> 87 * @author Tomas V.V. Cox <[email protected]> 88 * @author Cipriano Groenendal <[email protected]> 89 * @author Sean Coates <[email protected]> 90 * @copyright 2003-2006 PEAR <[email protected]> 91 * @license http://www.opensource.org/licenses/bsd-license.php BSD License 92 * @version Release: @package_version@ 93 * @link http://pear.php.net/package/Mail_mime 58 94 */ 59 95 class Mail_mime … … 61 97 /** 62 98 * Contains the plain text part of the email 99 * 63 100 * @var string 101 * @access private 64 102 */ 65 103 var $_txtbody; 104 66 105 /** 67 106 * Contains the html part of the email 107 * 68 108 * @var string 109 * @access private 69 110 */ 70 111 var $_htmlbody; 71 /** 72 * contains the mime encoded text 73 * @var string 74 */ 75 var $_mime; 76 /** 77 * contains the multipart content 78 * @var string 79 */ 80 var $_multipart; 112 81 113 /** 82 114 * list of the attached images 115 * 83 116 * @var array 117 * @access private 84 118 */ 85 119 var $_html_images = array(); 120 86 121 /** 87 122 * list of the attachements 123 * 88 124 * @var array 125 * @access private 89 126 */ 90 127 var $_parts = array(); 128 129 /** 130 * Headers for the mail 131 * 132 * @var array 133 * @access private 134 */ 135 var $_headers = array(); 136 91 137 /** 92 138 * Build parameters 139 * 93 140 * @var array 94 */ 95 var $_build_params = array(); 96 /** 97 * Headers for the mail 98 * @var array 99 */ 100 var $_headers = array(); 101 /** 102 * End Of Line sequence (for serialize) 103 * @var string 104 */ 105 var $_eol; 106 141 * @access private 142 */ 143 var $_build_params = array( 144 // What encoding to use for the headers 145 // Options: quoted-printable or base64 146 'head_encoding' => 'quoted-printable', 147 // What encoding to use for plain text 148 // Options: 7bit, 8bit, base64, or quoted-printable 149 'text_encoding' => 'quoted-printable', 150 // What encoding to use for html 151 // Options: 7bit, 8bit, base64, or quoted-printable 152 'html_encoding' => 'quoted-printable', 153 // The character set to use for html 154 'html_charset' => 'ISO-8859-1', 155 // The character set to use for text 156 'text_charset' => 'ISO-8859-1', 157 // The character set to use for headers 158 'head_charset' => 'ISO-8859-1', 159 // End-of-line sequence 160 'eol' => "\r\n", 161 // Delay attachment files IO until building the message 162 'delay_file_io' => false 163 ); 107 164 108 165 /** 109 166 * Constructor function 110 167 * 111 * @access public 112 */ 113 function Mail_mime($crlf = "\r\n") 114 { 115 $this->_setEOL($crlf); 116 $this->_build_params = array( 117 'text_encoding' => '7bit', 118 'html_encoding' => 'quoted-printable', 119 '7bit_wrap' => 998, 120 'html_charset' => 'ISO-8859-1', 121 'text_charset' => 'ISO-8859-1', 122 'head_charset' => 'ISO-8859-1' 123 ); 124 } 125 126 /** 127 * Wakeup (unserialize) - re-sets EOL constant 128 * 129 * @access private 130 */ 131 function __wakeup() 132 { 133 $this->_setEOL($this->_eol); 168 * @param mixed $params Build parameters that change the way the email 169 * is built. Should be an associative array. 170 * See $_build_params. 171 * 172 * @return void 173 * @access public 174 */ 175 function Mail_mime($params = array()) 176 { 177 // Backward-compatible EOL setting 178 if (is_string($params)) { 179 $this->_build_params['eol'] = $params; 180 } else if (defined('MAIL_MIME_CRLF') && !isset($params['eol'])) { 181 $this->_build_params['eol'] = MAIL_MIME_CRLF; 182 } 183 184 // Update build parameters 185 if (!empty($params) && is_array($params)) { 186 while (list($key, $value) = each($params)) { 187 $this->_build_params[$key] = $value; 188 } 189 } 190 } 191 192 /** 193 * Set build parameter value 194 * 195 * @param string $name Parameter name 196 * @param string $value Parameter value 197 * 198 * @return void 199 * @access public 200 * @since 1.6.0 201 */ 202 function setParam($name, $value) 203 { 204 $this->_build_params[$name] = $value; 205 } 206 207 /** 208 * Get build parameter value 209 * 210 * @param string $name Parameter name 211 * 212 * @return mixed Parameter value 213 * @access public 214 * @since 1.6.0 215 */ 216 function getParam($name) 217 { 218 return isset($this->_build_params[$name]) ? $this->_build_params[$name] : null; 134 219 } 135 220 … … 140 225 * html should show. 141 226 * 142 * @param string $data Either a string or 143 * the file name with the contents 144 * @param bool $isfile If true the first param should be treated 145 * as a file name, else as a string (default) 146 * @param bool $append If true the text or file is appended to 147 * the existing body, else the old body is 148 * overwritten 149 * @return mixed true on success or PEAR_Error object 227 * @param string $data Either a string or 228 * the file name with the contents 229 * @param bool $isfile If true the first param should be treated 230 * as a file name, else as a string (default) 231 * @param bool $append If true the text or file is appended to 232 * the existing body, else the old body is 233 * overwritten 234 * 235 * @return mixed True on success or PEAR_Error object 150 236 * @access public 151 237 */ … … 173 259 174 260 /** 175 * Adds a html part to the mail 176 * 177 * @param string $data Either a string or the file name with the 178 * contents 179 * @param bool $isfile If true the first param should be treated 180 * as a file name, else as a string (default) 181 * @return mixed true on success or PEAR_Error object 261 * Get message text body 262 * 263 * @return string Text body 264 * @access public 265 * @since 1.6.0 266 */ 267 function getTXTBody() 268 { 269 return $this->_txtbody; 270 } 271 272 /** 273 * Adds a html part to the mail. 274 * 275 * @param string $data Either a string or the file name with the 276 * contents 277 * @param bool $isfile A flag that determines whether $data is a 278 * filename, or a string(false, default) 279 * 280 * @return bool True on success 182 281 * @access public 183 282 */ … … 198 297 199 298 /** 299 * Get message HTML body 300 * 301 * @return string HTML body 302 * @access public 303 * @since 1.6.0 304 */ 305 function getHTMLBody() 306 { 307 return $this->_htmlbody; 308 } 309 310 /** 200 311 * Adds an image to the list of embedded images. 201 312 * 202 * @param string $file The image file name OR image data itself 203 * @param string $c_type The content type 204 * @param string $name The filename of the image. 205 * Only use if $file is the image data 206 * @param bool $isfilename Whether $file is a filename or not 207 * Defaults to true 208 * @return mixed true on success or PEAR_Error object 209 * @access public 210 */ 211 function addHTMLImage($file, $c_type='application/octet-stream', 212 $name = '', $isfilename = true) 213 { 214 $filedata = ($isfilename === true) ? $this->_file2str($file) 215 : $file; 216 if ($isfilename === true) { 217 $filename = ($name == '' ? basename($file) : basename($name)); 218 } else { 219 $filename = basename($name); 220 } 221 if (PEAR::isError($filedata)) { 222 return $filedata; 223 } 313 * @param string $file The image file name OR image data itself 314 * @param string $c_type The content type 315 * @param string $name The filename of the image. 316 * Only used if $file is the image data. 317 * @param bool $isfile Whether $file is a filename or not. 318 * Defaults to true 319 * @param string $content_id Desired Content-ID of MIME part 320 * Defaults to generated unique ID 321 * 322 * @return bool True on success 323 * @access public 324 */ 325 function addHTMLImage($file, 326 $c_type='application/octet-stream', 327 $name = '', 328 $isfile = true, 329 $content_id = null 330 ) { 331 $bodyfile = null; 332 333 if ($isfile) { 334 // Don't load file into memory 335 if ($this->_build_params['delay_file_io']) { 336 $filedata = null; 337 $bodyfile = $file; 338 } else { 339 if (PEAR::isError($filedata = $this->_file2str($file))) { 340 return $filedata; 341 } 342 } 343 $filename = ($name ? $name : $file); 344 } else { 345 $filedata = $file; 346 $filename = $name; 347 } 348 349 if (!$content_id) { 350 $content_id = md5(uniqid(time())); 351 } 352 224 353 $this->_html_images[] = array( 225 'body' => $filedata, 226 'name' => $filename, 227 'c_type' => $c_type, 228 'cid' => md5(uniqid(time())) 229 ); 354 'body' => $filedata, 355 'body_file' => $bodyfile, 356 'name' => $filename, 357 'c_type' => $c_type, 358 'cid' => $content_id 359 ); 360 230 361 return true; 231 362 } … … 234 365 * Adds a file to the list of attachments. 235 366 * 236 * @param string $file The file name of the file to attach 237 * OR the file data itself 238 * @param string $c_type The content type 239 * @param string $name The filename of the attachment 240 * Only use if $file is the file data 241 * @param bool $isFilename Whether $file is a filename or not 242 * Defaults to true 243 * @return mixed true on success or PEAR_Error object 244 * @access public 245 */ 246 function addAttachment($file, $c_type = 'application/octet-stream', 247 $name = '', $isfilename = true, 248 $encoding = 'base64') 249 { 250 $filedata = ($isfilename === true) ? $this->_file2str($file) 251 : $file; 252 if ($isfilename === true) { 367 * @param string $file The file name of the file to attach 368 * or the file contents itself 369 * @param string $c_type The content type 370 * @param string $name The filename of the attachment 371 * Only use if $file is the contents 372 * @param bool $isfile Whether $file is a filename or not. Defaults to true 373 * @param string $encoding The type of encoding to use. Defaults to base64. 374 * Possible values: 7bit, 8bit, base64 or quoted-printable. 375 * @param string $disposition The content-disposition of this file 376 * Defaults to attachment. 377 * Possible values: attachment, inline. 378 * @param string $charset The character set of attachment's content. 379 * @param string $language The language of the attachment 380 * @param string $location The RFC 2557.4 location of the attachment 381 * @param string $n_encoding Encoding of the attachment's name in Content-Type 382 * By default filenames are encoded using RFC2231 method 383 * Here you can set RFC2047 encoding (quoted-printable 384 * or base64) instead 385 * @param string $f_encoding Encoding of the attachment's filename 386 * in Content-Disposition header. 387 * @param string $description Content-Description header 388 * @param string $h_charset The character set of the headers e.g. filename 389 * If not specified, $charset will be used 390 * 391 * @return mixed True on success or PEAR_Error object 392 * @access public 393 */ 394 function addAttachment($file, 395 $c_type = 'application/octet-stream', 396 $name = '', 397 $isfile = true, 398 $encoding = 'base64', 399 $disposition = 'attachment', 400 $charset = '', 401 $language = '', 402 $location = '', 403 $n_encoding = null, 404 $f_encoding = null, 405 $description = '', 406 $h_charset = null 407 ) { 408 $bodyfile = null; 409 410 if ($isfile) { 411 // Don't load file into memory 412 if ($this->_build_params['delay_file_io']) { 413 $filedata = null; 414 $bodyfile = $file; 415 } else { 416 if (PEAR::isError($filedata = $this->_file2str($file))) { 417 return $filedata; 418 } 419 } 253 420 // Force the name the user supplied, otherwise use $file 254 $filename = (!empty($name)) ? $name : $file; 255 } else { 421 $filename = ($name ? $name : $file); 422 } else { 423 $filedata = $file; 256 424 $filename = $name; 257 425 } 258 if (empty($filename)) { 259 return PEAR::raiseError( 260 'The supplied filename for the attachment can\'t be empty' 261 ); 262 } 263 $filename = basename($filename); 264 if (PEAR::isError($filedata)) { 265 return $filedata; 266 } 426 427 if (!strlen($filename)) { 428 $msg = "The supplied filename for the attachment can't be empty"; 429 $err = PEAR::raiseError($msg); 430 return $err; 431 } 432 $filename = $this->_basename($filename); 267 433 268 434 $this->_parts[] = array( 269 'body' => $filedata, 270 'name' => $filename, 271 'c_type' => $c_type, 272 'encoding' => $encoding 273 ); 435 'body' => $filedata, 436 'body_file' => $bodyfile, 437 'name' => $filename, 438 'c_type' => $c_type, 439 'charset' => $charset, 440 'encoding' => $encoding, 441 'language' => $language, 442 'location' => $location, 443 'disposition' => $disposition, 444 'description' => $description, 445 'name_encoding' => $n_encoding, 446 'filename_encoding' => $f_encoding, 447 'headers_charset' => $h_charset, 448 ); 449 274 450 return true; 275 451 } … … 278 454 * Get the contents of the given file name as string 279 455 * 280 * @param string $file_name path of file to process 281 * @return string contents of $file_name 456 * @param string $file_name Path of file to process 457 * 458 * @return string Contents of $file_name 282 459 * @access private 283 460 */ 284 461 function &_file2str($file_name) 285 462 { 463 // Check state of file and raise an error properly 464 if (!file_exists($file_name)) { 465 $err = PEAR::raiseError('File not found: ' . $file_name); 466 return $err; 467 } 468 if (!is_file($file_name)) { 469 $err = PEAR::raiseError('Not a regular file: ' . $file_name); 470 return $err; 471 } 286 472 if (!is_readable($file_name)) { 287 return PEAR::raiseError('File is not readable ' . $file_name); 288 } 289 if (!$fd = fopen($file_name, 'rb')) { 290 return PEAR::raiseError('Could not open ' . $file_name); 291 } 292 $filesize = filesize($file_name); 293 if ($filesize == 0){ 294 $cont = ""; 295 }else{ 296 $cont = fread($fd, $filesize); 297 } 298 fclose($fd); 473 $err = PEAR::raiseError('File is not readable: ' . $file_name); 474 return $err; 475 } 476 477 // Temporarily reset magic_quotes_runtime and read file contents 478 if ($magic_quote_setting = get_magic_quotes_runtime()) { 479 @ini_set('magic_quotes_runtime', 0); 480 } 481 $cont = file_get_contents($file_name); 482 if ($magic_quote_setting) { 483 @ini_set('magic_quotes_runtime', $magic_quote_setting); 484 } 485 299 486 return $cont; 300 487 } … … 304 491 * returns it during the build process. 305 492 * 306 * @param mixed The object to add the part to, or 307 * null if a new object is to be created. 308 * @param string The text to add. 309 * @return object The text mimePart object 493 * @param mixed &$obj The object to add the part to, or 494 * null if a new object is to be created. 495 * @param string $text The text to add. 496 * 497 * @return object The text mimePart object 310 498 * @access private 311 499 */ … … 315 503 $params['encoding'] = $this->_build_params['text_encoding']; 316 504 $params['charset'] = $this->_build_params['text_charset']; 505 $params['eol'] = $this->_build_params['eol']; 506 317 507 if (is_object($obj)) { 318 return $obj->addSubpart($text, $params); 319 } else { 320 return new Mail_mimePart($text, $params); 508 $ret = $obj->addSubpart($text, $params); 509 return $ret; 510 } else { 511 $ret = new Mail_mimePart($text, $params); 512 return $ret; 321 513 } 322 514 } … … 326 518 * returns it during the build process. 327 519 * 328 * @param mixed The object to add the part to, or 329 * null if a new object is to be created. 330 * @return object The html mimePart object 520 * @param mixed &$obj The object to add the part to, or 521 * null if a new object is to be created. 522 * 523 * @return object The html mimePart object 331 524 * @access private 332 525 */ … … 336 529 $params['encoding'] = $this->_build_params['html_encoding']; 337 530 $params['charset'] = $this->_build_params['html_charset']; 531 $params['eol'] = $this->_build_params['eol']; 532 338 533 if (is_object($obj)) { 339 return $obj->addSubpart($this->_htmlbody, $params); 340 } else { 341 return new Mail_mimePart($this->_htmlbody, $params); 534 $ret = $obj->addSubpart($this->_htmlbody, $params); 535 return $ret; 536 } else { 537 $ret = new Mail_mimePart($this->_htmlbody, $params); 538 return $ret; 342 539 } 343 540 } … … 348 545 * build process. 349 546 * 350 * @return object The multipart/mixed mimePart object547 * @return object The multipart/mixed mimePart object 351 548 * @access private 352 549 */ 353 550 function &_addMixedPart() 354 551 { 552 $params = array(); 355 553 $params['content_type'] = 'multipart/mixed'; 356 return new Mail_mimePart('', $params); 554 $params['eol'] = $this->_build_params['eol']; 555 556 // Create empty multipart/mixed Mail_mimePart object to return 557 $ret = new Mail_mimePart('', $params); 558 return $ret; 357 559 } 358 560 … … 362 564 * the build process. 363 565 * 364 * @param mixed The object to add the part to, or 365 * null if a new object is to be created. 366 * @return object The multipart/mixed mimePart object 566 * @param mixed &$obj The object to add the part to, or 567 * null if a new object is to be created. 568 * 569 * @return object The multipart/mixed mimePart object 367 570 * @access private 368 571 */ … … 370 573 { 371 574 $params['content_type'] = 'multipart/alternative'; 575 $params['eol'] = $this->_build_params['eol']; 576 372 577 if (is_object($obj)) { 373 578 return $obj->addSubpart('', $params); 374 579 } else { 375 return new Mail_mimePart('', $params); 580 $ret = new Mail_mimePart('', $params); 581 return $ret; 376 582 } 377 583 } … … 382 588 * the build process. 383 589 * 384 * @param mixed The object to add the part to, or 385 * null if a new object is to be created 386 * @return object The multipart/mixed mimePart object 590 * @param mixed &$obj The object to add the part to, or 591 * null if a new object is to be created 592 * 593 * @return object The multipart/mixed mimePart object 387 594 * @access private 388 595 */ … … 390 597 { 391 598 $params['content_type'] = 'multipart/related'; 599 $params['eol'] = $this->_build_params['eol']; 600 392 601 if (is_object($obj)) { 393 602 return $obj->addSubpart('', $params); 394 603 } else { 395 return new Mail_mimePart('', $params); 604 $ret = new Mail_mimePart('', $params); 605 return $ret; 396 606 } 397 607 } … … 401 611 * and returns it during the build process. 402 612 * 403 * @param object The mimePart to add the image to 404 * @param array The image information 405 * @return object The image mimePart object 613 * @param object &$obj The mimePart to add the image to 614 * @param array $value The image information 615 * 616 * @return object The image mimePart object 406 617 * @access private 407 618 */ … … 411 622 $params['encoding'] = 'base64'; 412 623 $params['disposition'] = 'inline'; 413 $params[' dfilename']= $value['name'];624 $params['filename'] = $value['name']; 414 625 $params['cid'] = $value['cid']; 415 $obj->addSubpart($value['body'], $params); 626 $params['body_file'] = $value['body_file']; 627 $params['eol'] = $this->_build_params['eol']; 628 629 if (!empty($value['name_encoding'])) { 630 $params['name_encoding'] = $value['name_encoding']; 631 } 632 if (!empty($value['filename_encoding'])) { 633 $params['filename_encoding'] = $value['filename_encoding']; 634 } 635 636 $ret = $obj->addSubpart($value['body'], $params); 637 return $ret; 416 638 } 417 639 … … 420 642 * and returns it during the build process. 421 643 * 422 * @param object The mimePart to add the image to 423 * @param array The attachment information 424 * @return object The image mimePart object 644 * @param object &$obj The mimePart to add the image to 645 * @param array $value The attachment information 646 * 647 * @return object The image mimePart object 425 648 * @access private 426 649 */ 427 650 function &_addAttachmentPart(&$obj, $value) 428 651 { 652 $params['eol'] = $this->_build_params['eol']; 653 $params['filename'] = $value['name']; 654 $params['encoding'] = $value['encoding']; 429 655 $params['content_type'] = $value['c_type']; 430 $params['encoding'] = $value['encoding']; 431 $params['disposition'] = 'attachment'; 432 $params['dfilename'] = $value['name']; 433 $obj->addSubpart($value['body'], $params); 656 $params['body_file'] = $value['body_file']; 657 $params['disposition'] = isset($value['disposition']) ? 658 $value['disposition'] : 'attachment'; 659 660 // content charset 661 if (!empty($value['charset'])) { 662 $params['charset'] = $value['charset']; 663 } 664 // headers charset (filename, description) 665 if (!empty($value['headers_charset'])) { 666 $params['headers_charset'] = $value['headers_charset']; 667 } 668 if (!empty($value['language'])) { 669 $params['language'] = $value['language']; 670 } 671 if (!empty($value['location'])) { 672 $params['location'] = $value['location']; 673 } 674 if (!empty($value['name_encoding'])) { 675 $params['name_encoding'] = $value['name_encoding']; 676 } 677 if (!empty($value['filename_encoding'])) { 678 $params['filename_encoding'] = $value['filename_encoding']; 679 } 680 if (!empty($value['description'])) { 681 $params['description'] = $value['description']; 682 } 683 684 $ret = $obj->addSubpart($value['body'], $params); 685 return $ret; 686 } 687 688 /** 689 * Returns the complete e-mail, ready to send using an alternative 690 * mail delivery method. Note that only the mailpart that is made 691 * with Mail_Mime is created. This means that, 692 * YOU WILL HAVE NO TO: HEADERS UNLESS YOU SET IT YOURSELF 693 * using the $headers parameter! 694 * 695 * @param string $separation The separation between these two parts. 696 * @param array $params The Build parameters passed to the 697 * &get() function. See &get for more info. 698 * @param array $headers The extra headers that should be passed 699 * to the &headers() function. 700 * See that function for more info. 701 * @param bool $overwrite Overwrite the existing headers with new. 702 * 703 * @return mixed The complete e-mail or PEAR error object 704 * @access public 705 */ 706 function getMessage($separation = null, $params = null, $headers = null, 707 $overwrite = false 708 ) { 709 if ($separation === null) { 710 $separation = $this->_build_params['eol']; 711 } 712 713 $body = $this->get($params); 714 715 if (PEAR::isError($body)) { 716 return $body; 717 } 718 719 $head = $this->txtHeaders($headers, $overwrite); 720 $mail = $head . $separation . $body; 721 return $mail; 722 } 723 724 /** 725 * Returns the complete e-mail body, ready to send using an alternative 726 * mail delivery method. 727 * 728 * @param array $params The Build parameters passed to the 729 * &get() function. See &get for more info. 730 * 731 * @return mixed The e-mail body or PEAR error object 732 * @access public 733 * @since 1.6.0 734 */ 735 function getMessageBody($params = null) 736 { 737 return $this->get($params, null, true); 738 } 739 740 /** 741 * Writes (appends) the complete e-mail into file. 742 * 743 * @param string $filename Output file location 744 * @param array $params The Build parameters passed to the 745 * &get() function. See &get for more info. 746 * @param array $headers The extra headers that should be passed 747 * to the &headers() function. 748 * See that function for more info. 749 * @param bool $overwrite Overwrite the existing headers with new. 750 * 751 * @return mixed True or PEAR error object 752 * @access public 753 * @since 1.6.0 754 */ 755 function saveMessage($filename, $params = null, $headers = null, $overwrite = false) 756 { 757 // Check state of file and raise an error properly 758 if (file_exists($filename) && !is_writable($filename)) { 759 $err = PEAR::raiseError('File is not writable: ' . $filename); 760 return $err; 761 } 762 763 // Temporarily reset magic_quotes_runtime and read file contents 764 if ($magic_quote_setting = get_magic_quotes_runtime()) { 765 @ini_set('magic_quotes_runtime', 0); 766 } 767 768 if (!($fh = fopen($filename, 'ab'))) { 769 $err = PEAR::raiseError('Unable to open file: ' . $filename); 770 return $err; 771 } 772 773 // Write message headers into file (skipping Content-* headers) 774 $head = $this->txtHeaders($headers, $overwrite, true); 775 if (fwrite($fh, $head) === false) { 776 $err = PEAR::raiseError('Error writing to file: ' . $filename); 777 return $err; 778 } 779 780 fclose($fh); 781 782 if ($magic_quote_setting) { 783 @ini_set('magic_quotes_runtime', $magic_quote_setting); 784 } 785 786 // Write the rest of the message into file 787 $res = $this->get($params, $filename); 788 789 return $res ? $res : true; 790 } 791 792 /** 793 * Writes (appends) the complete e-mail body into file. 794 * 795 * @param string $filename Output file location 796 * @param array $params The Build parameters passed to the 797 * &get() function. See &get for more info. 798 * 799 * @return mixed True or PEAR error object 800 * @access public 801 * @since 1.6.0 802 */ 803 function saveMessageBody($filename, $params = null) 804 { 805 // Check state of file and raise an error properly 806 if (file_exists($filename) && !is_writable($filename)) { 807 $err = PEAR::raiseError('File is not writable: ' . $filename); 808 return $err; 809 } 810 811 // Temporarily reset magic_quotes_runtime and read file contents 812 if ($magic_quote_setting = get_magic_quotes_runtime()) { 813 @ini_set('magic_quotes_runtime', 0); 814 } 815 816 if (!($fh = fopen($filename, 'ab'))) { 817 $err = PEAR::raiseError('Unable to open file: ' . $filename); 818 return $err; 819 } 820 821 // Write the rest of the message into file 822 $res = $this->get($params, $filename, true); 823 824 return $res ? $res : true; 434 825 } 435 826 … … 438 829 * returns the mime content. 439 830 * 440 * @param array Build parameters that change the way the email 441 * is built. Should be associative. Can contain: 442 * text_encoding - What encoding to use for plain text 443 * Default is 7bit 444 * html_encoding - What encoding to use for html 445 * Default is quoted-printable 446 * 7bit_wrap - Number of characters before text is 447 * wrapped in 7bit encoding 448 * Default is 998 449 * html_charset - The character set to use for html. 450 * Default is iso-8859-1 451 * text_charset - The character set to use for text. 452 * Default is iso-8859-1 453 * head_charset - The character set to use for headers. 454 * Default is iso-8859-1 455 * @return string The mime content 456 * @access public 457 */ 458 function &get($build_params = null) 459 { 460 if (isset($build_params)) { 461 while (list($key, $value) = each($build_params)) { 831 * @param array $params Build parameters that change the way the email 832 * is built. Should be associative. See $_build_params. 833 * @param resource $filename Output file where to save the message instead of 834 * returning it 835 * @param boolean $skip_head True if you want to return/save only the message 836 * without headers 837 * 838 * @return mixed The MIME message content string, null or PEAR error object 839 * @access public 840 */ 841 function &get($params = null, $filename = null, $skip_head = false) 842 { 843 if (isset($params)) { 844 while (list($key, $value) = each($params)) { 462 845 $this->_build_params[$key] = $value; 463 846 } 464 847 } 465 848 466 if (!empty($this->_html_images) AND isset($this->_htmlbody)) { 467 foreach ($this->_html_images as $value) { 468 $regex = '#(\s)((?i)src|background|href(?-i))\s*=\s*(["\']?)' . preg_quote($value['name'], '#') . 469 '\3#'; 470 $rep = '\1\2=\3cid:' . $value['cid'] .'\3'; 471 $this->_htmlbody = preg_replace($regex, $rep, 472 $this->_htmlbody 473 ); 474 } 475 } 849 if (isset($this->_headers['From'])) { 850 // Bug #11381: Illegal characters in domain ID 851 if (preg_match('#(@[0-9a-zA-Z\-\.]+)#', $this->_headers['From'], $matches)) { 852 $domainID = $matches[1]; 853 } else { 854 $domainID = '@localhost'; 855 } 856 foreach ($this->_html_images as $i => $img) { 857 $cid = $this->_html_images[$i]['cid']; 858 if (!preg_match('#'.preg_quote($domainID).'$#', $cid)) { 859 $this->_html_images[$i]['cid'] = $cid . $domainID; 860 } 861 } 862 } 863 864 if (count($this->_html_images) && isset($this->_htmlbody)) { 865 foreach ($this->_html_images as $key => $value) { 866 $regex = array(); 867 $regex[] = '#(\s)((?i)src|background|href(?-i))\s*=\s*(["\']?)' . 868 preg_quote($value['name'], '#') . '\3#'; 869 $regex[] = '#(?i)url(?-i)\(\s*(["\']?)' . 870 preg_quote($value['name'], '#') . '\1\s*\)#'; 871 872 $rep = array(); 873 $rep[] = '\1\2=\3cid:' . $value['cid'] .'\3'; 874 $rep[] = 'url(\1cid:' . $value['cid'] . '\1)'; 875 876 $this->_htmlbody = preg_replace($regex, $rep, $this->_htmlbody); 877 $this->_html_images[$key]['name'] 878 = $this->_basename($this->_html_images[$key]['name']); 879 } 880 } 881 882 $this->_checkParams(); 476 883 477 884 $null = null; 478 $attachments = !empty($this->_parts)? true : false;479 $html_images = !empty($this->_html_images)? true : false;480 $html = !empty($this->_htmlbody) ? true : false;481 $text = (!$html AND !empty($this->_txtbody))? true : false;885 $attachments = count($this->_parts) ? true : false; 886 $html_images = count($this->_html_images) ? true : false; 887 $html = strlen($this->_htmlbody) ? true : false; 888 $text = (!$html && strlen($this->_txtbody)) ? true : false; 482 889 483 890 switch (true) { 484 case $text AND!$attachments:891 case $text && !$attachments: 485 892 $message =& $this->_addTextPart($null, $this->_txtbody); 486 893 break; 487 894 488 case !$text AND !$html AND$attachments:895 case !$text && !$html && $attachments: 489 896 $message =& $this->_addMixedPart(); 490 897 for ($i = 0; $i < count($this->_parts); $i++) { … … 493 900 break; 494 901 495 case $text AND$attachments:902 case $text && $attachments: 496 903 $message =& $this->_addMixedPart(); 497 904 $this->_addTextPart($message, $this->_txtbody); … … 501 908 break; 502 909 503 case $html AND !$attachments AND!$html_images:910 case $html && !$attachments && !$html_images: 504 911 if (isset($this->_txtbody)) { 505 912 $message =& $this->_addAlternativePart($null); … … 511 918 break; 512 919 513 case $html AND !$attachments AND $html_images: 920 case $html && !$attachments && $html_images: 921 // * Content-Type: multipart/alternative; 922 // * text 923 // * Content-Type: multipart/related; 924 // * html 925 // * image... 514 926 if (isset($this->_txtbody)) { 515 927 $message =& $this->_addAlternativePart($null); 516 928 $this->_addTextPart($message, $this->_txtbody); 517 $related =& $this->_addRelatedPart($message); 929 930 $ht =& $this->_addRelatedPart($message); 931 $this->_addHtmlPart($ht); 932 for ($i = 0; $i < count($this->_html_images); $i++) { 933 $this->_addHtmlImagePart($ht, $this->_html_images[$i]); 934 } 518 935 } else { 936 // * Content-Type: multipart/related; 937 // * html 938 // * image... 519 939 $message =& $this->_addRelatedPart($null); 520 $related =& $message; 521 } 522 $this->_addHtmlPart($related); 940 $this->_addHtmlPart($message); 941 for ($i = 0; $i < count($this->_html_images); $i++) { 942 $this->_addHtmlImagePart($message, $this->_html_images[$i]); 943 } 944 } 945 /* 946 // #13444, #9725: the code below was a non-RFC compliant hack 947 // * Content-Type: multipart/related; 948 // * Content-Type: multipart/alternative; 949 // * text 950 // * html 951 // * image... 952 $message =& $this->_addRelatedPart($null); 953 if (isset($this->_txtbody)) { 954 $alt =& $this->_addAlternativePart($message); 955 $this->_addTextPart($alt, $this->_txtbody); 956 $this->_addHtmlPart($alt); 957 } else { 958 $this->_addHtmlPart($message); 959 } 523 960 for ($i = 0; $i < count($this->_html_images); $i++) { 524 $this->_addHtmlImagePart($related, $this->_html_images[$i]); 525 } 961 $this->_addHtmlImagePart($message, $this->_html_images[$i]); 962 } 963 */ 526 964 break; 527 965 528 case $html AND $attachments AND!$html_images:966 case $html && $attachments && !$html_images: 529 967 $message =& $this->_addMixedPart(); 530 968 if (isset($this->_txtbody)) { … … 540 978 break; 541 979 542 case $html AND $attachments AND$html_images:980 case $html && $attachments && $html_images: 543 981 $message =& $this->_addMixedPart(); 544 982 if (isset($this->_txtbody)) { … … 560 998 } 561 999 562 if (isset($message)) { 563 $output = $message->encode(); 564 $this->_headers = array_merge($this->_headers, 565 $output['headers']); 566 return $output['body']; 567 568 } else { 569 return false; 1000 if (!isset($message)) { 1001 $ret = null; 1002 return $ret; 1003 } 1004 1005 // Use saved boundary 1006 if (!empty($this->_build_params['boundary'])) { 1007 $boundary = $this->_build_params['boundary']; 1008 } else { 1009 $boundary = null; 1010 } 1011 1012 // Write output to file 1013 if ($filename) { 1014 // Append mimePart message headers and body into file 1015 $headers = $message->encodeToFile($filename, $boundary, $skip_head); 1016 if (PEAR::isError($headers)) { 1017 return $headers; 1018 } 1019 $this->_headers = array_merge($this->_headers, $headers); 1020 $ret = null; 1021 return $ret; 1022 } else { 1023 $output = $message->encode($boundary, $skip_head); 1024 if (PEAR::isError($output)) { 1025 return $output; 1026 } 1027 $this->_headers = array_merge($this->_headers, $output['headers']); 1028 $body = $output['body']; 1029 return $body; 570 1030 } 571 1031 } … … 576 1036 * $array['header-name'] = 'header-value'; 577 1037 * 578 * @param array $xtra_headers Assoc array with any extra headers. 579 * Optional. 580 * @return array Assoc array with the mime headers 581 * @access public 582 */ 583 function &headers($xtra_headers = null) 584 { 585 // Content-Type header should already be present, 586 // So just add mime version header 1038 * @param array $xtra_headers Assoc array with any extra headers (optional) 1039 * (Don't set Content-Type for multipart messages here!) 1040 * @param bool $overwrite Overwrite already existing headers. 1041 * @param bool $skip_content Don't return content headers: Content-Type, 1042 * Content-Disposition and Content-Transfer-Encoding 1043 * 1044 * @return array Assoc array with the mime headers 1045 * @access public 1046 */ 1047 function &headers($xtra_headers = null, $overwrite = false, $skip_content = false) 1048 { 1049 // Add mime version header 587 1050 $headers['MIME-Version'] = '1.0'; 588 if (isset($xtra_headers)) { 1051 1052 // Content-Type and Content-Transfer-Encoding headers should already 1053 // be present if get() was called, but we'll re-set them to make sure 1054 // we got them when called before get() or something in the message 1055 // has been changed after get() [#14780] 1056 if (!$skip_content) { 1057 $headers += $this->_contentHeaders(); 1058 } 1059 1060 if (!empty($xtra_headers)) { 589 1061 $headers = array_merge($headers, $xtra_headers); 590 1062 } 591 $this->_headers = array_merge($headers, $this->_headers); 592 593 return $this->_encodeHeaders($this->_headers); 1063 1064 if ($overwrite) { 1065 $this->_headers = array_merge($this->_headers, $headers); 1066 } else { 1067 $this->_headers = array_merge($headers, $this->_headers); 1068 } 1069 1070 $headers = $this->_headers; 1071 1072 if ($skip_content) { 1073 unset($headers['Content-Type']); 1074 unset($headers['Content-Transfer-Encoding']); 1075 unset($headers['Content-Disposition']); 1076 } else if (!empty($this->_build_params['ctype'])) { 1077 $headers['Content-Type'] = $this->_build_params['ctype']; 1078 } 1079 1080 $encodedHeaders = $this->_encodeHeaders($headers); 1081 return $encodedHeaders; 594 1082 } 595 1083 … … 598 1086 * (usefull if you want to use the PHP mail() function) 599 1087 * 600 * @param array $xtra_headers Assoc array with any extra headers. 601 * Optional. 602 * @return string Plain text headers 603 * @access public 604 */ 605 function txtHeaders($xtra_headers = null) 606 { 607 $headers = $this->headers($xtra_headers); 1088 * @param array $xtra_headers Assoc array with any extra headers (optional) 1089 * (Don't set Content-Type for multipart messages here!) 1090 * @param bool $overwrite Overwrite the existing headers with new. 1091 * @param bool $skip_content Don't return content headers: Content-Type, 1092 * Content-Disposition and Content-Transfer-Encoding 1093 * 1094 * @return string Plain text headers 1095 * @access public 1096 */ 1097 function txtHeaders($xtra_headers = null, $overwrite = false, $skip_content = false) 1098 { 1099 $headers = $this->headers($xtra_headers, $overwrite, $skip_content); 1100 1101 // Place Received: headers at the beginning of the message 1102 // Spam detectors often flag messages with it after the Subject: as spam 1103 if (isset($headers['Received'])) { 1104 $received = $headers['Received']; 1105 unset($headers['Received']); 1106 $headers = array('Received' => $received) + $headers; 1107 } 1108 608 1109 $ret = ''; 1110 $eol = $this->_build_params['eol']; 1111 609 1112 foreach ($headers as $key => $val) { 610 $ret .= "$key: $val" . MAIL_MIME_CRLF; 611 } 1113 if (is_array($val)) { 1114 foreach ($val as $value) { 1115 $ret .= "$key: $value" . $eol; 1116 } 1117 } else { 1118 $ret .= "$key: $val" . $eol; 1119 } 1120 } 1121 612 1122 return $ret; 613 1123 } 614 1124 615 1125 /** 1126 * Sets message Content-Type header. 1127 * Use it to build messages with various content-types e.g. miltipart/raport 1128 * not supported by _contentHeaders() function. 1129 * 1130 * @param string $type Type name 1131 * @param array $params Hash array of header parameters 1132 * 1133 * @return void 1134 * @access public 1135 * @since 1.7.0 1136 */ 1137 function setContentType($type, $params = array()) 1138 { 1139 $header = $type; 1140 1141 $eol = !empty($this->_build_params['eol']) 1142 ? $this->_build_params['eol'] : "\r\n"; 1143 1144 // add parameters 1145 $token_regexp = '#([^\x21,\x23-\x27,\x2A,\x2B,\x2D' 1146 . ',\x2E,\x30-\x39,\x41-\x5A,\x5E-\x7E])#'; 1147 if (is_array($params)) { 1148 foreach ($params as $name => $value) { 1149 if ($name == 'boundary') { 1150 $this->_build_params['boundary'] = $value; 1151 } 1152 if (!preg_match($token_regexp, $value)) { 1153 $header .= ";$eol $name=$value"; 1154 } else { 1155 $value = addcslashes($value, '\\"'); 1156 $header .= ";$eol $name=\"$value\""; 1157 } 1158 } 1159 } 1160 1161 // add required boundary parameter if not defined 1162 if (preg_match('/^multipart\//i', $type)) { 1163 if (empty($this->_build_params['boundary'])) { 1164 $this->_build_params['boundary'] = '=_' . md5(rand() . microtime()); 1165 } 1166 1167 $header .= ";$eol boundary=\"".$this->_build_params['boundary']."\""; 1168 } 1169 1170 $this->_build_params['ctype'] = $header; 1171 } 1172 1173 /** 616 1174 * Sets the Subject header 617 1175 * 618 * @param string $subject String to set the subject to 619 * access public 1176 * @param string $subject String to set the subject to. 1177 * 1178 * @return void 1179 * @access public 620 1180 */ 621 1181 function setSubject($subject) … … 627 1187 * Set an email to the From (the sender) header 628 1188 * 629 * @param string $email The email direction to add 1189 * @param string $email The email address to use 1190 * 1191 * @return void 630 1192 * @access public 631 1193 */ … … 633 1195 { 634 1196 $this->_headers['From'] = $email; 1197 } 1198 1199 /** 1200 * Add an email to the To header 1201 * (multiple calls to this method are allowed) 1202 * 1203 * @param string $email The email direction to add 1204 * 1205 * @return void 1206 * @access public 1207 */ 1208 function addTo($email) 1209 { 1210 if (isset($this->_headers['To'])) { 1211 $this->_headers['To'] .= ", $email"; 1212 } else { 1213 $this->_headers['To'] = $email; 1214 } 635 1215 } 636 1216 … … 639 1219 * (multiple calls to this method are allowed) 640 1220 * 641 * @param string $email The email direction to add 1221 * @param string $email The email direction to add 1222 * 1223 * @return void 642 1224 * @access public 643 1225 */ … … 655 1237 * (multiple calls to this method are allowed) 656 1238 * 657 * @param string $email The email direction to add 1239 * @param string $email The email direction to add 1240 * 1241 * @return void 658 1242 * @access public 659 1243 */ … … 668 1252 669 1253 /** 1254 * Since the PHP send function requires you to specify 1255 * recipients (To: header) separately from the other 1256 * headers, the To: header is not properly encoded. 1257 * To fix this, you can use this public method to 1258 * encode your recipients before sending to the send 1259 * function 1260 * 1261 * @param string $recipients A comma-delimited list of recipients 1262 * 1263 * @return string Encoded data 1264 * @access public 1265 */ 1266 function encodeRecipients($recipients) 1267 { 1268 $input = array("To" => $recipients); 1269 $retval = $this->_encodeHeaders($input); 1270 return $retval["To"] ; 1271 } 1272 1273 /** 1274 * Encodes headers as per RFC2047 1275 * 1276 * @param array $input The header data to encode 1277 * @param array $params Extra build parameters 1278 * 1279 * @return array Encoded data 1280 * @access private 1281 */ 1282 function _encodeHeaders($input, $params = array()) 1283 { 1284 $build_params = $this->_build_params; 1285 while (list($key, $value) = each($params)) { 1286 $build_params[$key] = $value; 1287 } 1288 1289 foreach ($input as $hdr_name => $hdr_value) { 1290 if (is_array($hdr_value)) { 1291 foreach ($hdr_value as $idx => $value) { 1292 $input[$hdr_name][$idx] = $this->encodeHeader( 1293 $hdr_name, $value, 1294 $build_params['head_charset'], $build_params['head_encoding'] 1295 ); 1296 } 1297 } else { 1298 $input[$hdr_name] = $this->encodeHeader( 1299 $hdr_name, $hdr_value, 1300 $build_params['head_charset'], $build_params['head_encoding'] 1301 ); 1302 } 1303 } 1304 1305 return $input; 1306 } 1307 1308 /** 670 1309 * Encodes a header as per RFC2047 671 1310 * 672 * @param string $input The header data to encode 673 * @return string Encoded data 674 * @access private 675 */ 676 function _encodeHeaders($input) 677 { 678 foreach ($input as $hdr_name => $hdr_value) { 679 preg_match_all('/(\w*[\x80-\xFF]+\w*)/', $hdr_value, $matches); 680 foreach ($matches[1] as $value) { 681 $replacement = preg_replace('/([\x80-\xFF])/e', 682 '"=" . 683 strtoupper(dechex(ord("\1")))', 684 $value); 685 $hdr_value = str_replace($value, '=?' . 686 $this->_build_params['head_charset'] . 687 '?Q?' . $replacement . '?=', 688 $hdr_value); 689 } 690 $input[$hdr_name] = $hdr_value; 691 } 692 693 return $input; 694 } 695 696 /** 697 * Set the object's end-of-line and define the constant if applicable 698 * 699 * @param string $eol End Of Line sequence 700 * @access private 701 */ 702 function _setEOL($eol) 703 { 704 $this->_eol = $eol; 705 if (!defined('MAIL_MIME_CRLF')) { 706 define('MAIL_MIME_CRLF', $this->_eol, true); 707 } 708 } 709 710 1311 * @param string $name The header name 1312 * @param string $value The header data to encode 1313 * @param string $charset Character set name 1314 * @param string $encoding Encoding name (base64 or quoted-printable) 1315 * 1316 * @return string Encoded header data (without a name) 1317 * @access public 1318 * @since 1.5.3 1319 */ 1320 function encodeHeader($name, $value, $charset, $encoding) 1321 { 1322 return Mail_mimePart::encodeHeader( 1323 $name, $value, $charset, $encoding, $this->_build_params['eol'] 1324 ); 1325 } 1326 1327 /** 1328 * Get file's basename (locale independent) 1329 * 1330 * @param string $filename Filename 1331 * 1332 * @return string Basename 1333 * @access private 1334 */ 1335 function _basename($filename) 1336 { 1337 // basename() is not unicode safe and locale dependent 1338 if (stristr(PHP_OS, 'win') || stristr(PHP_OS, 'netware')) { 1339 return preg_replace('/^.*[\\\\\\/]/', '', $filename); 1340 } else { 1341 return preg_replace('/^.*[\/]/', '', $filename); 1342 } 1343 } 1344 1345 /** 1346 * Get Content-Type and Content-Transfer-Encoding headers of the message 1347 * 1348 * @return array Headers array 1349 * @access private 1350 */ 1351 function _contentHeaders() 1352 { 1353 $attachments = count($this->_parts) ? true : false; 1354 $html_images = count($this->_html_images) ? true : false; 1355 $html = strlen($this->_htmlbody) ? true : false; 1356 $text = (!$html && strlen($this->_txtbody)) ? true : false; 1357 $headers = array(); 1358 1359 // See get() 1360 switch (true) { 1361 case $text && !$attachments: 1362 $headers['Content-Type'] = 'text/plain'; 1363 break; 1364 1365 case !$text && !$html && $attachments: 1366 case $text && $attachments: 1367 case $html && $attachments && !$html_images: 1368 case $html && $attachments && $html_images: 1369 $headers['Content-Type'] = 'multipart/mixed'; 1370 break; 1371 1372 case $html && !$attachments && !$html_images && isset($this->_txtbody): 1373 case $html && !$attachments && $html_images && isset($this->_txtbody): 1374 $headers['Content-Type'] = 'multipart/alternative'; 1375 break; 1376 1377 case $html && !$attachments && !$html_images && !isset($this->_txtbody): 1378 $headers['Content-Type'] = 'text/html'; 1379 break; 1380 1381 case $html && !$attachments && $html_images && !isset($this->_txtbody): 1382 $headers['Content-Type'] = 'multipart/related'; 1383 break; 1384 1385 default: 1386 return $headers; 1387 } 1388 1389 $this->_checkParams(); 1390 1391 $eol = !empty($this->_build_params['eol']) 1392 ? $this->_build_params['eol'] : "\r\n"; 1393 1394 if ($headers['Content-Type'] == 'text/plain') { 1395 // single-part message: add charset and encoding 1396 $charset = 'charset=' . $this->_build_params['text_charset']; 1397 // place charset parameter in the same line, if possible 1398 // 26 = strlen("Content-Type: text/plain; ") 1399 $headers['Content-Type'] 1400 .= (strlen($charset) + 26 <= 76) ? "; $charset" : ";$eol $charset"; 1401 $headers['Content-Transfer-Encoding'] 1402 = $this->_build_params['text_encoding']; 1403 } else if ($headers['Content-Type'] == 'text/html') { 1404 // single-part message: add charset and encoding 1405 $charset = 'charset=' . $this->_build_params['html_charset']; 1406 // place charset parameter in the same line, if possible 1407 $headers['Content-Type'] 1408 .= (strlen($charset) + 25 <= 76) ? "; $charset" : ";$eol $charset"; 1409 $headers['Content-Transfer-Encoding'] 1410 = $this->_build_params['html_encoding']; 1411 } else { 1412 // multipart message: and boundary 1413 if (!empty($this->_build_params['boundary'])) { 1414 $boundary = $this->_build_params['boundary']; 1415 } else if (!empty($this->_headers['Content-Type']) 1416 && preg_match('/boundary="([^"]+)"/', $this->_headers['Content-Type'], $m) 1417 ) { 1418 $boundary = $m[1]; 1419 } else { 1420 $boundary = '=_' . md5(rand() . microtime()); 1421 } 1422 1423 $this->_build_params['boundary'] = $boundary; 1424 $headers['Content-Type'] .= ";$eol boundary=\"$boundary\""; 1425 } 1426 1427 return $headers; 1428 } 1429 1430 /** 1431 * Validate and set build parameters 1432 * 1433 * @return void 1434 * @access private 1435 */ 1436 function _checkParams() 1437 { 1438 $encodings = array('7bit', '8bit', 'base64', 'quoted-printable'); 1439 1440 $this->_build_params['text_encoding'] 1441 = strtolower($this->_build_params['text_encoding']); 1442 $this->_build_params['html_encoding'] 1443 = strtolower($this->_build_params['html_encoding']); 1444 1445 if (!in_array($this->_build_params['text_encoding'], $encodings)) { 1446 $this->_build_params['text_encoding'] = '7bit'; 1447 } 1448 if (!in_array($this->_build_params['html_encoding'], $encodings)) { 1449 $this->_build_params['html_encoding'] = '7bit'; 1450 } 1451 1452 // text body 1453 if ($this->_build_params['text_encoding'] == '7bit' 1454 && !preg_match('/ascii/i', $this->_build_params['text_charset']) 1455 && preg_match('/[^\x00-\x7F]/', $this->_txtbody) 1456 ) { 1457 $this->_build_params['text_encoding'] = 'quoted-printable'; 1458 } 1459 // html body 1460 if ($this->_build_params['html_encoding'] == '7bit' 1461 && !preg_match('/ascii/i', $this->_build_params['html_charset']) 1462 && preg_match('/[^\x00-\x7F]/', $this->_htmlbody) 1463 ) { 1464 $this->_build_params['html_encoding'] = 'quoted-printable'; 1465 } 1466 } 711 1467 712 1468 } // End of class 713 ?>
Note: See TracChangeset
for help on using the changeset viewer.
