1<?php 2 3/** 4 * The Mail_Mime class is used to create MIME E-mail messages 5 * 6 * The Mail_Mime class provides an OO interface to create MIME 7 * enabled email messages. This way you can create emails that 8 * contain plain-text bodies, HTML bodies, attachments, inline 9 * images and specific headers. 10 * 11 * Compatible with PHP version 5, 7 and 8 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 Tomas V.V. Cox <cox@idecnet.com> 47 * @author Cipriano Groenendal <cipri@php.net> 48 * @author Sean Coates <sean@php.net> 49 * @author Aleksander Machniak <alec@php.net> 50 * @copyright 2003-2006 PEAR <pear-group@php.net> 51 * @license http://www.opensource.org/licenses/bsd-license.php BSD License 52 * @version Release: @package_version@ 53 * @link http://pear.php.net/package/Mail_mime 54 * 55 * This class is based on HTML Mime Mail class from 56 * Richard Heyes <richard@phpguru.org> which was based also 57 * in the mime_mail.class by Tobias Ratschiller <tobias@dnet.it> 58 * and Sascha Schumann <sascha@schumann.cx> 59 */ 60 61 62require_once 'PEAR.php'; 63require_once 'Mail/mimePart.php'; 64 65 66/** 67 * The Mail_Mime class provides an OO interface to create MIME 68 * enabled email messages. This way you can create emails that 69 * contain plain-text bodies, HTML bodies, attachments, inline 70 * images and specific headers. 71 * 72 * @category Mail 73 * @package Mail_Mime 74 * @author Richard Heyes <richard@phpguru.org> 75 * @author Tomas V.V. Cox <cox@idecnet.com> 76 * @author Cipriano Groenendal <cipri@php.net> 77 * @author Sean Coates <sean@php.net> 78 * @copyright 2003-2006 PEAR <pear-group@php.net> 79 * @license http://www.opensource.org/licenses/bsd-license.php BSD License 80 * @version Release: @package_version@ 81 * @link http://pear.php.net/package/Mail_mime 82 */ 83class Mail_mime 84{ 85 /** 86 * Contains the plain text part of the email 87 * 88 * @var string 89 */ 90 protected $txtbody = ''; 91 92 /** 93 * Contains the html part of the email 94 * 95 * @var string 96 */ 97 protected $htmlbody = ''; 98 99 /** 100 * Contains the text/calendar part of the email 101 * 102 * @var string 103 */ 104 protected $calbody = ''; 105 106 /** 107 * List of the attached images 108 * 109 * @var array 110 */ 111 protected $html_images = array(); 112 113 /** 114 * List of the attachements 115 * 116 * @var array 117 */ 118 protected $parts = array(); 119 120 /** 121 * Headers for the mail 122 * 123 * @var array 124 */ 125 protected $headers = array(); 126 127 /** 128 * Build parameters 129 * 130 * @var array 131 */ 132 protected $build_params = array( 133 // What encoding to use for the headers 134 // Options: quoted-printable or base64 135 'head_encoding' => 'quoted-printable', 136 // What encoding to use for plain text 137 // Options: 7bit, 8bit, base64, or quoted-printable 138 'text_encoding' => 'quoted-printable', 139 // What encoding to use for html 140 // Options: 7bit, 8bit, base64, or quoted-printable 141 'html_encoding' => 'quoted-printable', 142 // What encoding to use for calendar part 143 // Options: 7bit, 8bit, base64, or quoted-printable 144 'calendar_encoding' => 'quoted-printable', 145 // The character set to use for html 146 'html_charset' => 'ISO-8859-1', 147 // The character set to use for text 148 'text_charset' => 'ISO-8859-1', 149 // The character set to use for calendar part 150 'calendar_charset' => 'UTF-8', 151 // The character set to use for headers 152 'head_charset' => 'ISO-8859-1', 153 // End-of-line sequence 154 'eol' => "\r\n", 155 // Delay attachment files IO until building the message 156 'delay_file_io' => false, 157 // Default calendar method 158 'calendar_method' => 'request', 159 // multipart part preamble (RFC2046 5.1.1) 160 'preamble' => '', 161 ); 162 163 164 /** 165 * Constructor function 166 * 167 * @param mixed $params Build parameters that change the way the email 168 * is built. Should be an associative array. 169 * See $_build_params. 170 * 171 * @return void 172 */ 173 public function __construct($params = array()) 174 { 175 // Backward-compatible EOL setting 176 if (is_string($params)) { 177 $this->build_params['eol'] = $params; 178 } else if (defined('MAIL_MIME_CRLF') && !isset($params['eol'])) { 179 $this->build_params['eol'] = MAIL_MIME_CRLF; 180 } 181 182 // Update build parameters 183 if (!empty($params) && is_array($params)) { 184 $this->build_params = array_merge($this->build_params, $params); 185 } 186 } 187 188 /** 189 * Set build parameter value 190 * 191 * @param string $name Parameter name 192 * @param string $value Parameter value 193 * 194 * @return void 195 * @since 1.6.0 196 */ 197 public function setParam($name, $value) 198 { 199 $this->build_params[$name] = $value; 200 } 201 202 /** 203 * Get build parameter value 204 * 205 * @param string $name Parameter name 206 * 207 * @return mixed Parameter value 208 * @since 1.6.0 209 */ 210 public function getParam($name) 211 { 212 return isset($this->build_params[$name]) ? $this->build_params[$name] : null; 213 } 214 215 /** 216 * Accessor function to set the body text. Body text is used if 217 * it's not an html mail being sent or else is used to fill the 218 * text/plain part that emails clients who don't support 219 * html should show. 220 * 221 * @param string $data Either a string or the file name with the contents 222 * @param bool $isfile If true the first param should be treated 223 * as a file name, else as a string (default) 224 * @param bool $append If true the text or file is appended to 225 * the existing body, else the old body is 226 * overwritten 227 * 228 * @return mixed True on success or PEAR_Error object 229 */ 230 public function setTXTBody($data, $isfile = false, $append = false) 231 { 232 return $this->setBody('txtbody', $data, $isfile, $append); 233 } 234 235 /** 236 * Get message text body 237 * 238 * @return string Text body 239 * @since 1.6.0 240 */ 241 public function getTXTBody() 242 { 243 return $this->txtbody; 244 } 245 246 /** 247 * Adds a html part to the mail. 248 * 249 * @param string $data Either a string or the file name with the contents 250 * @param bool $isfile A flag that determines whether $data is a 251 * filename, or a string(false, default) 252 * 253 * @return bool True on success or PEAR_Error object 254 */ 255 public function setHTMLBody($data, $isfile = false) 256 { 257 return $this->setBody('htmlbody', $data, $isfile); 258 } 259 260 /** 261 * Get message HTML body 262 * 263 * @return string HTML body 264 * @since 1.6.0 265 */ 266 public function getHTMLBody() 267 { 268 return $this->htmlbody; 269 } 270 271 /** 272 * Function to set a body of text/calendar part (not attachment) 273 * 274 * @param string $data Either a string or the file name with the contents 275 * @param bool $isfile If true the first param should be treated 276 * as a file name, else as a string (default) 277 * @param bool $append If true the text or file is appended to 278 * the existing body, else the old body is 279 * overwritten 280 * @param string $method iCalendar object method 281 * @param string $charset iCalendar character set 282 * @param string $encoding Transfer encoding 283 * 284 * @return mixed True on success or PEAR_Error object 285 * @since 1.9.0 286 */ 287 public function setCalendarBody($data, $isfile = false, $append = false, 288 $method = 'request', $charset = 'UTF-8', $encoding = 'quoted-printable' 289 ) { 290 $result = $this->setBody('calbody', $data, $isfile, $append); 291 292 if ($result === true) { 293 $this->build_params['calendar_method'] = $method; 294 $this->build_params['calendar_charset'] = $charset; 295 $this->build_params['calendar_encoding'] = $encoding; 296 } 297 } 298 299 /** 300 * Get body of calendar part 301 * 302 * @return string Calendar part body 303 * @since 1.9.0 304 */ 305 public function getCalendarBody() 306 { 307 return $this->calbody; 308 } 309 310 /** 311 * Adds an image to the list of embedded images. 312 * Images added this way will be added as related parts of the HTML message. 313 * 314 * To correctly match the HTML image with the related attachment 315 * HTML should refer to it by a filename (specified in $file or $name 316 * arguments) or by cid:<content-id> (specified in $content_id arg). 317 * 318 * @param string $file The image file name OR image data itself 319 * @param string $c_type The content type 320 * @param string $name The filename of the image. Used to find 321 * the image in HTML content. 322 * @param bool $isfile Whether $file is a filename or not. 323 * Defaults to true 324 * @param string $content_id Desired Content-ID of MIME part 325 * Defaults to generated unique ID 326 * 327 * @return bool True on success 328 */ 329 public function addHTMLImage($file, 330 $c_type = 'application/octet-stream', 331 $name = '', 332 $isfile = true, 333 $content_id = null 334 ) { 335 $bodyfile = null; 336 337 if ($isfile) { 338 // Don't load file into memory 339 if ($this->build_params['delay_file_io']) { 340 $filedata = null; 341 $bodyfile = $file; 342 } else { 343 if (self::isError($filedata = $this->file2str($file))) { 344 return $filedata; 345 } 346 } 347 348 $filename = $name ? $name : $file; 349 } else { 350 $filedata = $file; 351 $filename = $name; 352 } 353 354 if (!$content_id) { 355 $content_id = preg_replace('/[^0-9a-zA-Z]/', '', uniqid(time(), true)); 356 } 357 358 $this->html_images[] = array( 359 'body' => $filedata, 360 'body_file' => $bodyfile, 361 'name' => $filename, 362 'c_type' => $c_type, 363 'cid' => $content_id 364 ); 365 366 return true; 367 } 368 369 /** 370 * Adds a file to the list of attachments. 371 * 372 * @param mixed $file The file name or the file contents itself, 373 * it can be also Mail_mimePart object 374 * @param string $c_type The content type 375 * @param string $name The filename of the attachment 376 * Only use if $file is the contents 377 * @param bool $isfile Whether $file is a filename or not. Defaults to true 378 * @param string $encoding The type of encoding to use. Defaults to base64. 379 * Possible values: 7bit, 8bit, base64 or quoted-printable. 380 * @param string $disposition The content-disposition of this file 381 * Defaults to attachment. 382 * Possible values: attachment, inline. 383 * @param string $charset The character set of attachment's content. 384 * @param string $language The language of the attachment 385 * @param string $location The RFC 2557.4 location of the attachment 386 * @param string $n_encoding Encoding of the attachment's name in Content-Type 387 * By default filenames are encoded using RFC2231 method 388 * Here you can set RFC2047 encoding (quoted-printable 389 * or base64) instead 390 * @param string $f_encoding Encoding of the attachment's filename 391 * in Content-Disposition header. 392 * @param string $description Content-Description header 393 * @param string $h_charset The character set of the headers e.g. filename 394 * If not specified, $charset will be used 395 * @param array $add_headers Additional part headers. Array keys can be in form 396 * of <header_name>:<parameter_name> 397 * 398 * @return mixed True on success or PEAR_Error object 399 */ 400 public function addAttachment($file, 401 $c_type = 'application/octet-stream', 402 $name = '', 403 $isfile = true, 404 $encoding = 'base64', 405 $disposition = 'attachment', 406 $charset = '', 407 $language = '', 408 $location = '', 409 $n_encoding = null, 410 $f_encoding = null, 411 $description = '', 412 $h_charset = null, 413 $add_headers = array() 414 ) { 415 if ($file instanceof Mail_mimePart) { 416 $this->parts[] = $file; 417 return true; 418 } 419 420 $bodyfile = null; 421 422 if ($isfile) { 423 // Don't load file into memory 424 if ($this->build_params['delay_file_io']) { 425 $filedata = null; 426 $bodyfile = $file; 427 } else { 428 if (self::isError($filedata = $this->file2str($file))) { 429 return $filedata; 430 } 431 } 432 // Force the name the user supplied, otherwise use $file 433 $filename = ($name ? $name : $this->basename($file)); 434 } else { 435 $filedata = $file; 436 $filename = $name; 437 } 438 439 if (!strlen($filename)) { 440 $msg = "The supplied filename for the attachment can't be empty"; 441 return self::raiseError($msg); 442 } 443 444 $this->parts[] = array( 445 'body' => $filedata, 446 'body_file' => $bodyfile, 447 'name' => $filename, 448 'c_type' => $c_type, 449 'charset' => $charset, 450 'encoding' => $encoding, 451 'language' => $language, 452 'location' => $location, 453 'disposition' => $disposition, 454 'description' => $description, 455 'add_headers' => $add_headers, 456 'name_encoding' => $n_encoding, 457 'filename_encoding' => $f_encoding, 458 'headers_charset' => $h_charset, 459 ); 460 461 return true; 462 } 463 464 /** 465 * Checks if the current message has many parts 466 * 467 * @return bool True if the message has many parts, False otherwise. 468 * @since 1.9.0 469 */ 470 public function isMultipart() 471 { 472 return count($this->parts) > 0 || count($this->html_images) > 0 473 || (strlen($this->htmlbody) > 0 && strlen($this->txtbody) > 0); 474 } 475 476 /** 477 * Get the contents of the given file name as string 478 * 479 * @param string $file_name Path of file to process 480 * 481 * @return string Contents of $file_name 482 */ 483 protected function file2str($file_name) 484 { 485 // Check state of file and raise an error properly 486 if (!file_exists($file_name)) { 487 return self::raiseError('File not found: ' . $file_name); 488 } 489 if (!is_file($file_name)) { 490 return self::raiseError('Not a regular file: ' . $file_name); 491 } 492 if (!is_readable($file_name)) { 493 return self::raiseError('File is not readable: ' . $file_name); 494 } 495 496 // Temporarily reset magic_quotes_runtime and read file contents 497 if (version_compare(PHP_VERSION, '5.4.0', '<')) { 498 $magic_quotes = @ini_set('magic_quotes_runtime', 0); 499 } 500 501 $cont = file_get_contents($file_name); 502 503 if (isset($magic_quotes)) { 504 @ini_set('magic_quotes_runtime', $magic_quotes); 505 } 506 507 return $cont; 508 } 509 510 /** 511 * Adds a text subpart to the mimePart object and 512 * returns it during the build process. 513 * 514 * @param mixed $obj The object to add the part to, or 515 * anything else if a new object is to be created. 516 * 517 * @return object The text mimePart object 518 */ 519 protected function addTextPart($obj = null) 520 { 521 return $this->addBodyPart($obj, $this->txtbody, 'text/plain', 'text'); 522 } 523 524 /** 525 * Adds a html subpart to the mimePart object and 526 * returns it during the build process. 527 * 528 * @param mixed $obj The object to add the part to, or 529 * anything else if a new object is to be created. 530 * 531 * @return object The html mimePart object 532 */ 533 protected function addHtmlPart($obj = null) 534 { 535 return $this->addBodyPart($obj, $this->htmlbody, 'text/html', 'html'); 536 } 537 538 /** 539 * Adds a calendar subpart to the mimePart object and 540 * returns it during the build process. 541 * 542 * @param mixed $obj The object to add the part to, or 543 * anything else if a new object is to be created. 544 * 545 * @return object The text mimePart object 546 */ 547 protected function addCalendarPart($obj = null) 548 { 549 $ctype = 'text/calendar; method='. $this->build_params['calendar_method']; 550 551 return $this->addBodyPart($obj, $this->calbody, $ctype, 'calendar'); 552 } 553 554 /** 555 * Creates a new mimePart object, using multipart/mixed as 556 * the initial content-type and returns it during the 557 * build process. 558 * 559 * @param array $params Additional part parameters 560 * 561 * @return object The multipart/mixed mimePart object 562 */ 563 protected function addMixedPart($params = array()) 564 { 565 $params['content_type'] = 'multipart/mixed'; 566 $params['eol'] = $this->build_params['eol']; 567 568 // Create empty multipart/mixed Mail_mimePart object to return 569 return new Mail_mimePart('', $params); 570 } 571 572 /** 573 * Adds a multipart/alternative part to a mimePart 574 * object (or creates one), and returns it during 575 * the build process. 576 * 577 * @param mixed $obj The object to add the part to, or 578 * anything else if a new object is to be created. 579 * 580 * @return object The multipart/mixed mimePart object 581 */ 582 protected function addAlternativePart($obj = null) 583 { 584 $params['content_type'] = 'multipart/alternative'; 585 $params['eol'] = $this->build_params['eol']; 586 587 if (is_object($obj)) { 588 $ret = $obj->addSubpart('', $params); 589 } else { 590 $ret = new Mail_mimePart('', $params); 591 } 592 593 return $ret; 594 } 595 596 /** 597 * Adds a multipart/related part to a mimePart 598 * object (or creates one), and returns it during 599 * the build process. 600 * 601 * @param mixed $obj The object to add the part to, or 602 * anything else if a new object is to be created 603 * 604 * @return object The multipart/mixed mimePart object 605 */ 606 protected function addRelatedPart($obj = null) 607 { 608 $params['content_type'] = 'multipart/related'; 609 $params['eol'] = $this->build_params['eol']; 610 611 if (is_object($obj)) { 612 $ret = $obj->addSubpart('', $params); 613 } else { 614 $ret = new Mail_mimePart('', $params); 615 } 616 617 return $ret; 618 } 619 620 /** 621 * Adds an html image subpart to a mimePart object 622 * and returns it during the build process. 623 * 624 * @param object $obj The mimePart to add the image to 625 * @param array $value The image information 626 * 627 * @return object The image mimePart object 628 */ 629 protected function addHtmlImagePart($obj, $value) 630 { 631 $params['content_type'] = $value['c_type']; 632 $params['encoding'] = 'base64'; 633 $params['disposition'] = 'inline'; 634 $params['filename'] = $value['name']; 635 $params['cid'] = $value['cid']; 636 $params['body_file'] = $value['body_file']; 637 $params['eol'] = $this->build_params['eol']; 638 639 if (!empty($value['name_encoding'])) { 640 $params['name_encoding'] = $value['name_encoding']; 641 } 642 if (!empty($value['filename_encoding'])) { 643 $params['filename_encoding'] = $value['filename_encoding']; 644 } 645 646 return $obj->addSubpart($value['body'], $params); 647 } 648 649 /** 650 * Adds an attachment subpart to a mimePart object 651 * and returns it during the build process. 652 * 653 * @param object $obj The mimePart to add the image to 654 * @param mixed $value The attachment information array or Mail_mimePart object 655 * 656 * @return object The image mimePart object 657 */ 658 protected function addAttachmentPart($obj, $value) 659 { 660 if ($value instanceof Mail_mimePart) { 661 return $obj->addSubpart($value); 662 } 663 664 $params['eol'] = $this->build_params['eol']; 665 $params['filename'] = $value['name']; 666 $params['encoding'] = $value['encoding']; 667 $params['content_type'] = $value['c_type']; 668 $params['body_file'] = $value['body_file']; 669 $params['disposition'] = isset($value['disposition']) ? 670 $value['disposition'] : 'attachment'; 671 672 // content charset 673 if (!empty($value['charset'])) { 674 $params['charset'] = $value['charset']; 675 } 676 // headers charset (filename, description) 677 if (!empty($value['headers_charset'])) { 678 $params['headers_charset'] = $value['headers_charset']; 679 } 680 if (!empty($value['language'])) { 681 $params['language'] = $value['language']; 682 } 683 if (!empty($value['location'])) { 684 $params['location'] = $value['location']; 685 } 686 if (!empty($value['name_encoding'])) { 687 $params['name_encoding'] = $value['name_encoding']; 688 } 689 if (!empty($value['filename_encoding'])) { 690 $params['filename_encoding'] = $value['filename_encoding']; 691 } 692 if (!empty($value['description'])) { 693 $params['description'] = $value['description']; 694 } 695 if (is_array($value['add_headers'])) { 696 $params['headers'] = $value['add_headers']; 697 } 698 699 return $obj->addSubpart($value['body'], $params); 700 } 701 702 /** 703 * Returns the complete e-mail, ready to send using an alternative 704 * mail delivery method. Note that only the mailpart that is made 705 * with Mail_Mime is created. This means that, 706 * YOU WILL HAVE NO TO: HEADERS UNLESS YOU SET IT YOURSELF 707 * using the $headers parameter! 708 * 709 * @param string $separation The separation between these two parts. 710 * @param array $params The Build parameters passed to the 711 * get() function. See get() for more info. 712 * @param array $headers The extra headers that should be passed 713 * to the headers() method. 714 * See that function for more info. 715 * @param bool $overwrite Overwrite the existing headers with new. 716 * 717 * @return mixed The complete e-mail or PEAR error object 718 */ 719 public function getMessage($separation = null, $params = null, $headers = null, 720 $overwrite = false 721 ) { 722 if ($separation === null) { 723 $separation = $this->build_params['eol']; 724 } 725 726 $body = $this->get($params); 727 728 if (self::isError($body)) { 729 return $body; 730 } 731 732 return $this->txtHeaders($headers, $overwrite) . $separation . $body; 733 } 734 735 /** 736 * Returns the complete e-mail body, ready to send using an alternative 737 * mail delivery method. 738 * 739 * @param array $params The Build parameters passed to the 740 * get() method. See get() for more info. 741 * 742 * @return mixed The e-mail body or PEAR error object 743 * @since 1.6.0 744 */ 745 public function getMessageBody($params = null) 746 { 747 return $this->get($params, null, true); 748 } 749 750 /** 751 * Writes (appends) the complete e-mail into file. 752 * 753 * @param string $filename Output file location 754 * @param array $params The Build parameters passed to the 755 * get() method. See get() for more info. 756 * @param array $headers The extra headers that should be passed 757 * to the headers() function. 758 * See that function for more info. 759 * @param bool $overwrite Overwrite the existing headers with new. 760 * 761 * @return mixed True or PEAR error object 762 * @since 1.6.0 763 */ 764 public function saveMessage($filename, $params = null, $headers = null, $overwrite = false) 765 { 766 // Check state of file and raise an error properly 767 if (file_exists($filename) && !is_writable($filename)) { 768 return self::raiseError('File is not writable: ' . $filename); 769 } 770 771 // Temporarily reset magic_quotes_runtime and read file contents 772 if (version_compare(PHP_VERSION, '5.4.0', '<')) { 773 $magic_quotes = @ini_set('magic_quotes_runtime', 0); 774 } 775 776 if (!($fh = fopen($filename, 'ab'))) { 777 return self::raiseError('Unable to open file: ' . $filename); 778 } 779 780 // Write message headers into file (skipping Content-* headers) 781 $head = $this->txtHeaders($headers, $overwrite, true); 782 if (fwrite($fh, $head) === false) { 783 return self::raiseError('Error writing to file: ' . $filename); 784 } 785 786 fclose($fh); 787 788 if (isset($magic_quotes)) { 789 @ini_set('magic_quotes_runtime', $magic_quotes); 790 } 791 792 // Write the rest of the message into file 793 $res = $this->get($params, $filename); 794 795 return $res ? $res : true; 796 } 797 798 /** 799 * Writes (appends) the complete e-mail body into file or stream. 800 * 801 * @param mixed $filename Output filename or file pointer where to save 802 * the message instead of returning it 803 * @param array $params The Build parameters passed to the 804 * get() method. See get() for more info. 805 * 806 * @return mixed True or PEAR error object 807 * @since 1.6.0 808 */ 809 public function saveMessageBody($filename, $params = null) 810 { 811 if (!is_resource($filename)) { 812 // Check state of file and raise an error properly 813 if (!file_exists($filename) || !is_writable($filename)) { 814 return self::raiseError('File is not writable: ' . $filename); 815 } 816 817 if (!($fh = fopen($filename, 'ab'))) { 818 return self::raiseError('Unable to open file: ' . $filename); 819 } 820 } else { 821 $fh = $filename; 822 } 823 824 // Temporarily reset magic_quotes_runtime and read file contents 825 if (version_compare(PHP_VERSION, '5.4.0', '<')) { 826 $magic_quotes = @ini_set('magic_quotes_runtime', 0); 827 } 828 829 // Write the rest of the message into file 830 $res = $this->get($params, $fh, true); 831 832 if (!is_resource($filename)) { 833 fclose($fh); 834 } 835 836 if (isset($magic_quotes)) { 837 @ini_set('magic_quotes_runtime', $magic_quotes); 838 } 839 840 return $res ? $res : true; 841 } 842 843 /** 844 * Builds the multipart message from the list ($this->parts) and 845 * returns the mime content. 846 * 847 * @param array $params Build parameters that change the way the email 848 * is built. Should be associative. See $_build_params. 849 * @param mixed $filename Output filename or file pointer where to save 850 * the message instead of returning it 851 * @param boolean $skip_head True if you want to return/save only the message 852 * without headers 853 * 854 * @return mixed The MIME message content string, null or PEAR error object 855 */ 856 public function get($params = null, $filename = null, $skip_head = false) 857 { 858 if (!empty($params) && is_array($params)) { 859 $this->build_params = array_merge($this->build_params, $params); 860 } 861 862 if (isset($this->headers['From'])) { 863 // Bug #11381: Illegal characters in domain ID 864 if (preg_match('#(@[0-9a-zA-Z\-\.]+)#', $this->headers['From'], $matches)) { 865 $domainID = $matches[1]; 866 } else { 867 $domainID = '@localhost'; 868 } 869 870 foreach ($this->html_images as $i => $img) { 871 $cid = $this->html_images[$i]['cid']; 872 if (!preg_match('#'.preg_quote($domainID).'$#', $cid)) { 873 $this->html_images[$i]['cid'] = $cid . $domainID; 874 } 875 } 876 } 877 878 if (count($this->html_images) && strlen($this->htmlbody) > 0) { 879 foreach ($this->html_images as $key => $value) { 880 $rval = preg_quote($value['name'], '#'); 881 $regex = array( 882 '#(\s)((?i)src|background|href(?-i))\s*=\s*(["\']?)' . $rval . '\3#', 883 '#(?i)url(?-i)\(\s*(["\']?)' . $rval . '\1\s*\)#', 884 ); 885 886 $rep = array( 887 '\1\2=\3cid:' . $value['cid'] .'\3', 888 'url(\1cid:' . $value['cid'] . '\1)', 889 ); 890 891 $this->htmlbody = preg_replace($regex, $rep, $this->htmlbody); 892 $this->html_images[$key]['name'] 893 = $this->basename($this->html_images[$key]['name']); 894 } 895 } 896 897 $this->checkParams(); 898 899 $message = $this->buildBodyPart(); 900 901 if (!isset($message)) { 902 return null; 903 } 904 905 // Use saved boundary 906 if (!empty($this->build_params['boundary'])) { 907 $boundary = $this->build_params['boundary']; 908 } else { 909 $boundary = null; 910 } 911 912 // Write output to file 913 if ($filename) { 914 // Append mimePart message headers and body into file 915 $headers = $message->encodeToFile($filename, $boundary, $skip_head); 916 if (self::isError($headers)) { 917 return $headers; 918 } 919 $this->headers = array_merge($this->headers, $headers); 920 } else { 921 $output = $message->encode($boundary, $skip_head); 922 if (self::isError($output)) { 923 return $output; 924 } 925 $this->headers = array_merge($this->headers, $output['headers']); 926 } 927 928 // remember the boundary used, in case we'd handle headers() call later 929 if (empty($boundary) && !empty($this->headers['Content-Type'])) { 930 if (preg_match('/boundary="([^"]+)/', $this->headers['Content-Type'], $m)) { 931 $this->build_params['boundary'] = $m[1]; 932 } 933 } 934 935 return $filename ? null : $output['body']; 936 } 937 938 /** 939 * Builds the main body MIME part for the email body. It will add a mixed part 940 * if attachments are found. If no attachments are found it will return an 941 * alternative part if several body texts are found (text, html, calendar), 942 * or a single part if only one body text is found. 943 * 944 * @return Mail_mimePart|null The corresponding part for the body or null. 945 * 946 * @see buildAlternativeParts 947 * @see buildHtmlParts 948 */ 949 protected function buildBodyPart() 950 { 951 $parts_count = count($this->parts); 952 $mixed_params = array('preamble' => $this->build_params['preamble']); 953 $message = null; 954 955 if ($parts_count > 0) { 956 $message = $this->addMixedPart($mixed_params); 957 $this->buildAlternativeParts($message, null); 958 for ($i = 0; $i < $parts_count; $i++) { 959 $this->addAttachmentPart($message, $this->parts[$i]); 960 } 961 } else { 962 $message = $this->buildAlternativeParts(null, $mixed_params); 963 } 964 965 return $message; 966 } 967 968 /** 969 * Builds a single text, html, or calendar part only if one of them is found. 970 * If two or more parts are found, then an alternative part containing them is built. 971 * 972 * @param Mail_mimePart|null $parent_part The parent mime part to add 973 * the part or null 974 * @param array $mixed_params The needed params to create the 975 * part when no parent_part is 976 * received. 977 * 978 * @return null|object The main part built inside the method. It will be an 979 * alternative part or text, html, or calendar part. 980 * Null if no body texts are found. 981 */ 982 protected function buildAlternativeParts($parent_part, $mixed_params = null) 983 { 984 $html = strlen($this->htmlbody) > 0; 985 $calendar = strlen($this->calbody) > 0; 986 $has_text = strlen($this->txtbody) > 0; 987 $alternatives_count = $html + $calendar + $has_text; 988 989 if ($alternatives_count > 1) { 990 $alt_part = $this->addAlternativePart($parent_part ? $parent_part : $mixed_params); 991 } else { 992 $alt_part = null; 993 } 994 995 $dest_part = $alt_part ? $alt_part : $parent_part; 996 $part = null; 997 998 if ($has_text) { 999 $part = $this->addTextPart($dest_part); 1000 } 1001 1002 if ($html) { 1003 $part = $this->buildHtmlParts($dest_part); 1004 } 1005 1006 if ($calendar) { 1007 $part = $this->addCalendarPart($dest_part); 1008 } 1009 1010 return $dest_part ? $dest_part : $part; 1011 } 1012 1013 /** 1014 * Builds html part as a single part or inside a related part with the html 1015 * images thar were found. 1016 * 1017 * @param Mail_mimePart|null $parent_part The object to add the part to, 1018 * or anything else if a new object 1019 * is to be created. 1020 * 1021 * @return Mail_mimePart|null The created part or null if no htmlbody found. 1022 */ 1023 protected function buildHtmlParts($parent_part) 1024 { 1025 if (!strlen($this->htmlbody)) { 1026 return null; 1027 } 1028 1029 $count_html_images = count($this->html_images); 1030 1031 if ($count_html_images > 0) { 1032 $part = $this->addRelatedPart($parent_part); 1033 $this->addHtmlPart($part); 1034 } else { 1035 $part = $this->addHtmlPart($parent_part); 1036 } 1037 1038 for ($i = 0; $i < $count_html_images; $i++) { 1039 $this->addHtmlImagePart($part, $this->html_images[$i]); 1040 } 1041 1042 return $part; 1043 } 1044 1045 /** 1046 * Returns an array with the headers needed to prepend to the email 1047 * (MIME-Version and Content-Type). Format of argument is: 1048 * $array['header-name'] = 'header-value'; 1049 * 1050 * @param array $xtra_headers Assoc array with any extra headers (optional) 1051 * (Don't set Content-Type for multipart messages here!) 1052 * @param bool $overwrite Overwrite already existing headers. 1053 * @param bool $skip_content Don't return content headers: Content-Type, 1054 * Content-Disposition and Content-Transfer-Encoding 1055 * 1056 * @return array Assoc array with the mime headers 1057 */ 1058 public function headers($xtra_headers = null, $overwrite = false, $skip_content = false) 1059 { 1060 // Add mime version header 1061 $headers['MIME-Version'] = '1.0'; 1062 1063 if (!empty($xtra_headers)) { 1064 $headers = array_merge($headers, $xtra_headers); 1065 } 1066 1067 if ($overwrite) { 1068 $this->headers = array_merge($this->headers, $headers); 1069 } else { 1070 $this->headers = array_merge($headers, $this->headers); 1071 } 1072 1073 // Always reset Content-Type/Content-Transfer-Encoding headers 1074 // In case the message structure changed in meantime 1075 unset($this->headers['Content-Type']); 1076 unset($this->headers['Content-Transfer-Encoding']); 1077 unset($this->headers['Content-Disposition']); 1078 1079 $this->headers = array_merge($this->headers, $this->contentHeaders()); 1080 1081 $headers = $this->headers; 1082 1083 if ($skip_content) { 1084 unset($headers['Content-Type']); 1085 unset($headers['Content-Transfer-Encoding']); 1086 unset($headers['Content-Disposition']); 1087 } else if (!empty($this->build_params['ctype'])) { 1088 $headers['Content-Type'] = $this->build_params['ctype']; 1089 } 1090 1091 return $this->encodeHeaders($headers); 1092 } 1093 1094 /** 1095 * Get the text version of the headers 1096 * (usefull if you want to use the PHP mail() function) 1097 * 1098 * @param array $xtra_headers Assoc array with any extra headers (optional) 1099 * (Don't set Content-Type for multipart messages here!) 1100 * @param bool $overwrite Overwrite the existing headers with new. 1101 * @param bool $skip_content Don't return content headers: Content-Type, 1102 * Content-Disposition and Content-Transfer-Encoding 1103 * 1104 * @return string Plain text headers 1105 */ 1106 public function txtHeaders($xtra_headers = null, $overwrite = false, $skip_content = false) 1107 { 1108 $headers = $this->headers($xtra_headers, $overwrite, $skip_content); 1109 1110 // Place Received: headers at the beginning of the message 1111 // Spam detectors often flag messages with it after the Subject: as spam 1112 if (isset($headers['Received'])) { 1113 $received = $headers['Received']; 1114 unset($headers['Received']); 1115 $headers = array('Received' => $received) + $headers; 1116 } 1117 1118 $ret = ''; 1119 $eol = $this->build_params['eol']; 1120 1121 foreach ($headers as $key => $val) { 1122 if (is_array($val)) { 1123 foreach ($val as $value) { 1124 $ret .= "$key: $value" . $eol; 1125 } 1126 } else { 1127 $ret .= "$key: $val" . $eol; 1128 } 1129 } 1130 1131 return $ret; 1132 } 1133 1134 /** 1135 * Sets message Content-Type header. 1136 * Use it to build messages with various content-types e.g. miltipart/raport 1137 * not supported by contentHeaders() function. 1138 * 1139 * @param string $type Type name 1140 * @param array $params Hash array of header parameters 1141 * 1142 * @return void 1143 * @since 1.7.0 1144 */ 1145 public function setContentType($type, $params = array()) 1146 { 1147 $header = $type; 1148 1149 $eol = !empty($this->build_params['eol']) ? $this->build_params['eol'] : "\r\n"; 1150 1151 // add parameters 1152 $token_regexp = '#([^\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7E])#'; 1153 1154 if (is_array($params)) { 1155 foreach ($params as $name => $value) { 1156 if ($name == 'boundary') { 1157 $this->build_params['boundary'] = $value; 1158 } else if (!preg_match($token_regexp, $value)) { 1159 $header .= ";$eol $name=$value"; 1160 } else { 1161 $value = addcslashes($value, '\\"'); 1162 $header .= ";$eol $name=\"$value\""; 1163 } 1164 } 1165 } 1166 1167 // add required boundary parameter if not defined 1168 if (stripos($type, 'multipart/') === 0) { 1169 if (empty($this->build_params['boundary'])) { 1170 $this->build_params['boundary'] = '=_' . md5(rand() . microtime()); 1171 } 1172 1173 $header .= ";$eol boundary=\"".$this->build_params['boundary']."\""; 1174 } 1175 1176 $this->build_params['ctype'] = $header; 1177 } 1178 1179 /** 1180 * Sets the Subject header 1181 * 1182 * @param string $subject String to set the subject to. 1183 * 1184 * @return void 1185 */ 1186 public function setSubject($subject) 1187 { 1188 $this->headers['Subject'] = $subject; 1189 } 1190 1191 /** 1192 * Set an email to the From (the sender) header 1193 * 1194 * @param string $email The email address to use 1195 * 1196 * @return void 1197 */ 1198 public function setFrom($email) 1199 { 1200 $this->headers['From'] = $email; 1201 } 1202 1203 /** 1204 * Add an email to the To header 1205 * (multiple calls to this method are allowed) 1206 * 1207 * @param string $email The email direction to add 1208 * 1209 * @return void 1210 */ 1211 public function addTo($email) 1212 { 1213 if (isset($this->headers['To'])) { 1214 $this->headers['To'] .= ", $email"; 1215 } else { 1216 $this->headers['To'] = $email; 1217 } 1218 } 1219 1220 /** 1221 * Add an email to the Cc (carbon copy) header 1222 * (multiple calls to this method are allowed) 1223 * 1224 * @param string $email The email direction to add 1225 * 1226 * @return void 1227 */ 1228 public function addCc($email) 1229 { 1230 if (isset($this->headers['Cc'])) { 1231 $this->headers['Cc'] .= ", $email"; 1232 } else { 1233 $this->headers['Cc'] = $email; 1234 } 1235 } 1236 1237 /** 1238 * Add an email to the Bcc (blank carbon copy) header 1239 * (multiple calls to this method are allowed) 1240 * 1241 * @param string $email The email direction to add 1242 * 1243 * @return void 1244 */ 1245 public function addBcc($email) 1246 { 1247 if (isset($this->headers['Bcc'])) { 1248 $this->headers['Bcc'] .= ", $email"; 1249 } else { 1250 $this->headers['Bcc'] = $email; 1251 } 1252 } 1253 1254 /** 1255 * Since the PHP send function requires you to specify 1256 * recipients (To: header) separately from the other 1257 * headers, the To: header is not properly encoded. 1258 * To fix this, you can use this public method to encode 1259 * your recipients before sending to the send function. 1260 * 1261 * @param string $recipients A comma-delimited list of recipients 1262 * 1263 * @return string Encoded data 1264 */ 1265 public function encodeRecipients($recipients) 1266 { 1267 $input = array('To' => $recipients); 1268 $retval = $this->encodeHeaders($input); 1269 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 */ 1281 protected function encodeHeaders($input, $params = array()) 1282 { 1283 $build_params = $this->build_params; 1284 1285 if (!empty($params)) { 1286 $build_params = array_merge($build_params, $params); 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 if ($hdr_value !== null) { 1298 $input[$hdr_name] = $this->encodeHeader( 1299 $hdr_name, $hdr_value, 1300 $build_params['head_charset'], $build_params['head_encoding'] 1301 ); 1302 } else { 1303 unset($input[$hdr_name]); 1304 } 1305 } 1306 1307 return $input; 1308 } 1309 1310 /** 1311 * Encodes a header as per RFC2047 1312 * 1313 * @param string $name The header name 1314 * @param string $value The header data to encode 1315 * @param string $charset Character set name 1316 * @param string $encoding Encoding name (base64 or quoted-printable) 1317 * 1318 * @return string Encoded header data (without a name) 1319 * @since 1.5.3 1320 */ 1321 public function encodeHeader($name, $value, $charset, $encoding) 1322 { 1323 return Mail_mimePart::encodeHeader( 1324 $name, $value, $charset, $encoding, $this->build_params['eol'] 1325 ); 1326 } 1327 1328 /** 1329 * Get file's basename (locale independent) 1330 * 1331 * @param string $filename Filename 1332 * 1333 * @return string Basename 1334 */ 1335 protected 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 */ 1350 protected function contentHeaders() 1351 { 1352 $attachments = count($this->parts) > 0; 1353 $html_images = count($this->html_images) > 0; 1354 $html = strlen($this->htmlbody) > 0; 1355 $calendar = strlen($this->calbody) > 0; 1356 $has_text = strlen($this->txtbody) > 0; 1357 $has_alternatives = ($html + $calendar + $has_text) > 1; 1358 $headers = array(); 1359 1360 // See get() 1361 switch (true) { 1362 case $has_text && !$attachments && !$has_alternatives: 1363 $headers['Content-Type'] = 'text/plain'; 1364 break; 1365 1366 case $html && !$html_images && !$attachments && !$has_alternatives: 1367 $headers['Content-Type'] = 'text/html'; 1368 break; 1369 1370 case $html && $html_images && !$attachments && !$has_alternatives: 1371 $headers['Content-Type'] = 'multipart/related'; 1372 break; 1373 1374 case $calendar && !$attachments && !$has_alternatives: 1375 $headers['Content-Type'] = 'text/calendar'; 1376 break; 1377 1378 case $has_alternatives && !$attachments: 1379 $headers['Content-Type'] = 'multipart/alternative'; 1380 break; 1381 1382 case $attachments: 1383 $headers['Content-Type'] = 'multipart/mixed'; 1384 break; 1385 } 1386 1387 // Note: This is outside of the above switch construct to workaround 1388 // opcache bug: https://bugzilla.opensuse.org/show_bug.cgi?id=1166235 1389 if (empty($headers)) { 1390 return $headers; 1391 } 1392 1393 $this->checkParams(); 1394 1395 $eol = !empty($this->build_params['eol']) 1396 ? $this->build_params['eol'] : "\r\n"; 1397 1398 if ($headers['Content-Type'] == 'text/plain') { 1399 // single-part message: add charset and encoding 1400 if ($this->build_params['text_charset']) { 1401 $charset = 'charset=' . $this->build_params['text_charset']; 1402 // place charset parameter in the same line, if possible 1403 // 26 = strlen("Content-Type: text/plain; ") 1404 $headers['Content-Type'] 1405 .= (strlen($charset) + 26 <= 76) ? "; $charset" : ";$eol $charset"; 1406 } 1407 1408 $headers['Content-Transfer-Encoding'] 1409 = $this->build_params['text_encoding']; 1410 } else if ($headers['Content-Type'] == 'text/html') { 1411 // single-part message: add charset and encoding 1412 if ($this->build_params['html_charset']) { 1413 $charset = 'charset=' . $this->build_params['html_charset']; 1414 // place charset parameter in the same line, if possible 1415 $headers['Content-Type'] 1416 .= (strlen($charset) + 25 <= 76) ? "; $charset" : ";$eol $charset"; 1417 } 1418 $headers['Content-Transfer-Encoding'] 1419 = $this->build_params['html_encoding']; 1420 } else if ($headers['Content-Type'] == 'text/calendar') { 1421 // single-part message: add charset and encoding 1422 if ($this->build_params['calendar_charset']) { 1423 $charset = 'charset=' . $this->build_params['calendar_charset']; 1424 $headers['Content-Type'] .= "; $charset"; 1425 } 1426 1427 if ($this->build_params['calendar_method']) { 1428 $method = 'method=' . $this->build_params['calendar_method']; 1429 $headers['Content-Type'] .= "; $method"; 1430 } 1431 1432 $headers['Content-Transfer-Encoding'] 1433 = $this->build_params['calendar_encoding']; 1434 } else { 1435 // multipart message: and boundary 1436 if (!empty($this->build_params['boundary'])) { 1437 $boundary = $this->build_params['boundary']; 1438 } else if (!empty($this->headers['Content-Type']) 1439 && preg_match('/boundary="([^"]+)"/', $this->headers['Content-Type'], $m) 1440 ) { 1441 $boundary = $m[1]; 1442 } else { 1443 $boundary = '=_' . md5(rand() . microtime()); 1444 } 1445 1446 $this->build_params['boundary'] = $boundary; 1447 $headers['Content-Type'] .= ";$eol boundary=\"$boundary\""; 1448 } 1449 1450 return $headers; 1451 } 1452 1453 /** 1454 * Validate and set build parameters 1455 * 1456 * @return void 1457 */ 1458 protected function checkParams() 1459 { 1460 $encodings = array('7bit', '8bit', 'base64', 'quoted-printable'); 1461 1462 $this->build_params['text_encoding'] 1463 = strtolower($this->build_params['text_encoding']); 1464 $this->build_params['html_encoding'] 1465 = strtolower($this->build_params['html_encoding']); 1466 $this->build_params['calendar_encoding'] 1467 = strtolower($this->build_params['calendar_encoding']); 1468 1469 if (!in_array($this->build_params['text_encoding'], $encodings)) { 1470 $this->build_params['text_encoding'] = '7bit'; 1471 } 1472 if (!in_array($this->build_params['html_encoding'], $encodings)) { 1473 $this->build_params['html_encoding'] = '7bit'; 1474 } 1475 if (!in_array($this->build_params['calendar_encoding'], $encodings)) { 1476 $this->build_params['calendar_encoding'] = '7bit'; 1477 } 1478 1479 // text body 1480 if ($this->build_params['text_encoding'] == '7bit' 1481 && !preg_match('/ascii/i', $this->build_params['text_charset']) 1482 && preg_match('/[^\x00-\x7F]/', $this->txtbody) 1483 ) { 1484 $this->build_params['text_encoding'] = 'quoted-printable'; 1485 } 1486 // html body 1487 if ($this->build_params['html_encoding'] == '7bit' 1488 && !preg_match('/ascii/i', $this->build_params['html_charset']) 1489 && preg_match('/[^\x00-\x7F]/', $this->htmlbody) 1490 ) { 1491 $this->build_params['html_encoding'] = 'quoted-printable'; 1492 } 1493 // calendar body 1494 if ($this->build_params['calendar_encoding'] == '7bit' 1495 && !preg_match('/ascii/i', $this->build_params['calendar_charset']) 1496 && preg_match('/[^\x00-\x7F]/', $this->calbody) 1497 ) { 1498 $this->build_params['calendar_encoding'] = 'quoted-printable'; 1499 } 1500 } 1501 1502 /** 1503 * Set body of specified message part 1504 * 1505 * @param string $type One of: txtbody, calbody, htmlbody 1506 * @param string $data Either a string or the file name with the contents 1507 * @param bool $isfile If true the first param should be treated 1508 * as a file name, else as a string (default) 1509 * @param bool $append If true the text or file is appended to 1510 * the existing body, else the old body is 1511 * overwritten 1512 * 1513 * @return mixed True on success or PEAR_Error object 1514 */ 1515 protected function setBody($type, $data, $isfile = false, $append = false) 1516 { 1517 if ($isfile) { 1518 $data = $this->file2str($data); 1519 if (self::isError($data)) { 1520 return $data; 1521 } 1522 } 1523 1524 if (!$append) { 1525 $this->{$type} = $data; 1526 } else { 1527 $this->{$type} .= $data; 1528 } 1529 1530 return true; 1531 } 1532 1533 /** 1534 * Adds a subpart to the mimePart object and 1535 * returns it during the build process. 1536 * 1537 * @param mixed $obj The object to add the part to, or 1538 * anything else if a new object is to be created. 1539 * @param string $body Part body 1540 * @param string $ctype Part content type 1541 * @param string $type Internal part type 1542 * 1543 * @return object The mimePart object 1544 */ 1545 protected function addBodyPart($obj, $body, $ctype, $type) 1546 { 1547 $params['content_type'] = $ctype; 1548 $params['encoding'] = $this->build_params[$type . '_encoding']; 1549 $params['charset'] = $this->build_params[$type . '_charset']; 1550 $params['eol'] = $this->build_params['eol']; 1551 1552 if (is_object($obj)) { 1553 $ret = $obj->addSubpart($body, $params); 1554 } else { 1555 $ret = new Mail_mimePart($body, $params); 1556 } 1557 1558 return $ret; 1559 } 1560 1561 /** 1562 * PEAR::isError implementation 1563 * 1564 * @param mixed $data Object 1565 * 1566 * @return bool True if object is an instance of PEAR_Error 1567 */ 1568 public static function isError($data) 1569 { 1570 // PEAR::isError() is not PHP 5.4 compatible (see Bug #19473) 1571 if (is_a($data, 'PEAR_Error')) { 1572 return true; 1573 } 1574 1575 return false; 1576 } 1577 1578 /** 1579 * PEAR::raiseError implementation 1580 * 1581 * @param string $message A text error message 1582 * 1583 * @return PEAR_Error Instance of PEAR_Error 1584 */ 1585 public static function raiseError($message) 1586 { 1587 // PEAR::raiseError() is not PHP 5.4 compatible 1588 return new PEAR_Error($message); 1589 } 1590} 1591