Debian Codegraph @

vendor/pear/mail_mime/Mail/mime.php

raw
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 <[email protected]>
15 * Copyright (c) 2003-2006, PEAR <[email protected]>
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 <[email protected]>
46 * @author Tomas V.V. Cox <[email protected]>
47 * @author Cipriano Groenendal <[email protected]>
48 * @author Sean Coates <[email protected]>
49 * @author Aleksander Machniak <[email protected]>
50 * @copyright 2003-2006 PEAR <[email protected]>
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 <[email protected]> which was based also
57 * in the mime_mail.class by Tobias Ratschiller <[email protected]>
58 * and Sascha Schumann <[email protected]>
59 */
60
61
62 require_once 'PEAR.php';
63 require_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 <[email protected]>
75 * @author Tomas V.V. Cox <[email protected]>
76 * @author Cipriano Groenendal <[email protected]>
77 * @author Sean Coates <[email protected]>
78 * @copyright 2003-2006 PEAR <[email protected]>
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 */
83 class 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 (!is_string($file_name)) {
487 return self::raiseError('Invalid or empty file name');
488 }
489 if (!file_exists($file_name)) {
490 return self::raiseError('File not found: ' . $file_name);
491 }
492 if (!is_file($file_name)) {
493 return self::raiseError('Not a regular file: ' . $file_name);
494 }
495 if (!is_readable($file_name)) {
496 return self::raiseError('File is not readable: ' . $file_name);
497 }
498
499 // Temporarily reset magic_quotes_runtime and read file contents
500 if (version_compare(PHP_VERSION, '5.4.0', '<')) {
501 $magic_quotes = @ini_set('magic_quotes_runtime', 0);
502 }
503
504 $cont = file_get_contents($file_name);
505
506 if (isset($magic_quotes)) {
507 @ini_set('magic_quotes_runtime', $magic_quotes);
508 }
509
510 return $cont;
511 }
512
513 /**
514 * Adds a text subpart to the mimePart object and
515 * returns it during the build process.
516 *
517 * @param mixed $obj The object to add the part to, or
518 * anything else if a new object is to be created.
519 *
520 * @return Mail_mimePart The text mimePart object
521 */
522 protected function addTextPart($obj = null)
523 {
524 return $this->addBodyPart($obj, $this->txtbody, 'text/plain', 'text');
525 }
526
527 /**
528 * Adds a html subpart to the mimePart object and
529 * returns it during the build process.
530 *
531 * @param mixed $obj The object to add the part to, or
532 * anything else if a new object is to be created.
533 *
534 * @return Mail_mimePart The html mimePart object
535 */
536 protected function addHtmlPart($obj = null)
537 {
538 return $this->addBodyPart($obj, $this->htmlbody, 'text/html', 'html');
539 }
540
541 /**
542 * Adds a calendar subpart to the mimePart object and
543 * returns it during the build process.
544 *
545 * @param mixed $obj The object to add the part to, or
546 * anything else if a new object is to be created.
547 *
548 * @return Mail_mimePart The text mimePart object
549 */
550 protected function addCalendarPart($obj = null)
551 {
552 $ctype = 'text/calendar; method='. $this->build_params['calendar_method'];
553
554 return $this->addBodyPart($obj, $this->calbody, $ctype, 'calendar');
555 }
556
557 /**
558 * Creates a new mimePart object, using multipart/mixed as
559 * the initial content-type and returns it during the
560 * build process.
561 *
562 * @param array $params Additional part parameters
563 *
564 * @return Mail_mimePart The multipart/mixed mimePart object
565 */
566 protected function addMixedPart($params = array())
567 {
568 $params['content_type'] = 'multipart/mixed';
569 $params['eol'] = $this->build_params['eol'];
570
571 // Create empty multipart/mixed Mail_mimePart object to return
572 return new Mail_mimePart('', $params);
573 }
574
575 /**
576 * Adds a multipart/alternative part to a mimePart
577 * object (or creates one), and returns it during
578 * the build process.
579 *
580 * @param mixed $obj The object to add the part to, or
581 * anything else if a new object is to be created.
582 *
583 * @return Mail_mimePart The multipart/mixed mimePart object
584 */
585 protected function addAlternativePart($obj = null)
586 {
587 $params['content_type'] = 'multipart/alternative';
588 $params['eol'] = $this->build_params['eol'];
589
590 if (is_object($obj)) {
591 $ret = $obj->addSubpart('', $params);
592 } else {
593 $ret = new Mail_mimePart('', $params);
594 }
595
596 return $ret;
597 }
598
599 /**
600 * Adds a multipart/related part to a mimePart
601 * object (or creates one), and returns it during
602 * the build process.
603 *
604 * @param mixed $obj The object to add the part to, or
605 * anything else if a new object is to be created
606 *
607 * @return Mail_mimePart The multipart/mixed mimePart object
608 */
609 protected function addRelatedPart($obj = null)
610 {
611 $params['content_type'] = 'multipart/related';
612 $params['eol'] = $this->build_params['eol'];
613
614 if (is_object($obj)) {
615 $ret = $obj->addSubpart('', $params);
616 } else {
617 $ret = new Mail_mimePart('', $params);
618 }
619
620 return $ret;
621 }
622
623 /**
624 * Adds an html image subpart to a mimePart object
625 * and returns it during the build process.
626 *
627 * @param object $obj The mimePart to add the image to
628 * @param array $value The image information
629 *
630 * @return Mail_mimePart The image mimePart object
631 */
632 protected function addHtmlImagePart($obj, $value)
633 {
634 $params['content_type'] = $value['c_type'];
635 $params['encoding'] = 'base64';
636 $params['disposition'] = 'inline';
637 $params['filename'] = $value['name'];
638 $params['cid'] = $value['cid'];
639 $params['body_file'] = $value['body_file'];
640 $params['eol'] = $this->build_params['eol'];
641
642 if (!empty($value['name_encoding'])) {
643 $params['name_encoding'] = $value['name_encoding'];
644 }
645 if (!empty($value['filename_encoding'])) {
646 $params['filename_encoding'] = $value['filename_encoding'];
647 }
648
649 return $obj->addSubpart($value['body'], $params);
650 }
651
652 /**
653 * Adds an attachment subpart to a mimePart object
654 * and returns it during the build process.
655 *
656 * @param object $obj The mimePart to add the image to
657 * @param mixed $value The attachment information array or Mail_mimePart object
658 *
659 * @return Mail_mimePart The image mimePart object
660 */
661 protected function addAttachmentPart($obj, $value)
662 {
663 if ($value instanceof Mail_mimePart) {
664 return $obj->addSubpart($value);
665 }
666
667 $params['eol'] = $this->build_params['eol'];
668 $params['filename'] = $value['name'];
669 $params['encoding'] = $value['encoding'];
670 $params['content_type'] = $value['c_type'];
671 $params['body_file'] = $value['body_file'];
672 $params['disposition'] = isset($value['disposition']) ?
673 $value['disposition'] : 'attachment';
674
675 // content charset
676 if (!empty($value['charset'])) {
677 $params['charset'] = $value['charset'];
678 }
679 // headers charset (filename, description)
680 if (!empty($value['headers_charset'])) {
681 $params['headers_charset'] = $value['headers_charset'];
682 }
683 if (!empty($value['language'])) {
684 $params['language'] = $value['language'];
685 }
686 if (!empty($value['location'])) {
687 $params['location'] = $value['location'];
688 }
689 if (!empty($value['name_encoding'])) {
690 $params['name_encoding'] = $value['name_encoding'];
691 }
692 if (!empty($value['filename_encoding'])) {
693 $params['filename_encoding'] = $value['filename_encoding'];
694 }
695 if (!empty($value['description'])) {
696 $params['description'] = $value['description'];
697 }
698 if (is_array($value['add_headers'])) {
699 $params['headers'] = $value['add_headers'];
700 }
701
702 return $obj->addSubpart($value['body'], $params);
703 }
704
705 /**
706 * Returns the complete e-mail, ready to send using an alternative
707 * mail delivery method. Note that only the mailpart that is made
708 * with Mail_Mime is created. This means that,
709 * YOU WILL HAVE NO TO: HEADERS UNLESS YOU SET IT YOURSELF
710 * using the $headers parameter!
711 *
712 * @param string $separation The separation between these two parts.
713 * @param array $params The Build parameters passed to the
714 * get() function. See get() for more info.
715 * @param array $headers The extra headers that should be passed
716 * to the headers() method.
717 * See that function for more info.
718 * @param bool $overwrite Overwrite the existing headers with new.
719 *
720 * @return mixed The complete e-mail or PEAR error object
721 */
722 public function getMessage($separation = null, $params = null, $headers = null,
723 $overwrite = false
724 ) {
725 if ($separation === null) {
726 $separation = $this->build_params['eol'];
727 }
728
729 $body = $this->get($params);
730
731 if (self::isError($body)) {
732 return $body;
733 }
734
735 return $this->txtHeaders($headers, $overwrite) . $separation . $body;
736 }
737
738 /**
739 * Returns the complete e-mail body, ready to send using an alternative
740 * mail delivery method.
741 *
742 * @param array $params The Build parameters passed to the
743 * get() method. See get() for more info.
744 *
745 * @return mixed The e-mail body or PEAR error object
746 * @since 1.6.0
747 */
748 public function getMessageBody($params = null)
749 {
750 return $this->get($params, null, true);
751 }
752
753 /**
754 * Writes (appends) the complete e-mail into file.
755 *
756 * @param string $filename Output file location
757 * @param array $params The Build parameters passed to the
758 * get() method. See get() for more info.
759 * @param array $headers The extra headers that should be passed
760 * to the headers() function.
761 * See that function for more info.
762 * @param bool $overwrite Overwrite the existing headers with new.
763 *
764 * @return mixed True or PEAR error object
765 * @since 1.6.0
766 */
767 public function saveMessage($filename, $params = null, $headers = null, $overwrite = false)
768 {
769 // Check state of file and raise an error properly
770 if (file_exists($filename) && !is_writable($filename)) {
771 return self::raiseError('File is not writable: ' . $filename);
772 }
773
774 // Temporarily reset magic_quotes_runtime and read file contents
775 if (version_compare(PHP_VERSION, '5.4.0', '<')) {
776 $magic_quotes = @ini_set('magic_quotes_runtime', 0);
777 }
778
779 if (!($fh = fopen($filename, 'ab'))) {
780 return self::raiseError('Unable to open file: ' . $filename);
781 }
782
783 // Write message headers into file (skipping Content-* headers)
784 $head = $this->txtHeaders($headers, $overwrite, true);
785 if (fwrite($fh, $head) === false) {
786 return self::raiseError('Error writing to file: ' . $filename);
787 }
788
789 fclose($fh);
790
791 if (isset($magic_quotes)) {
792 @ini_set('magic_quotes_runtime', $magic_quotes);
793 }
794
795 // Write the rest of the message into file
796 $res = $this->get($params, $filename);
797
798 return $res ? $res : true;
799 }
800
801 /**
802 * Writes (appends) the complete e-mail body into file or stream.
803 *
804 * @param mixed $filename Output filename or file pointer where to save
805 * the message instead of returning it
806 * @param array $params The Build parameters passed to the
807 * get() method. See get() for more info.
808 *
809 * @return mixed True or PEAR error object
810 * @since 1.6.0
811 */
812 public function saveMessageBody($filename, $params = null)
813 {
814 if (!is_resource($filename)) {
815 // Check state of file and raise an error properly
816 if (!file_exists($filename) || !is_writable($filename)) {
817 return self::raiseError('File is not writable: ' . $filename);
818 }
819
820 if (!($fh = fopen($filename, 'ab'))) {
821 return self::raiseError('Unable to open file: ' . $filename);
822 }
823 } else {
824 $fh = $filename;
825 }
826
827 // Temporarily reset magic_quotes_runtime and read file contents
828 if (version_compare(PHP_VERSION, '5.4.0', '<')) {
829 $magic_quotes = @ini_set('magic_quotes_runtime', 0);
830 }
831
832 // Write the rest of the message into file
833 $res = $this->get($params, $fh, true);
834
835 if (!is_resource($filename)) {
836 fclose($fh);
837 }
838
839 if (isset($magic_quotes)) {
840 @ini_set('magic_quotes_runtime', $magic_quotes);
841 }
842
843 return $res ? $res : true;
844 }
845
846 /**
847 * Builds the multipart message from the list ($this->parts) and
848 * returns the mime content.
849 *
850 * @param array $params Build parameters that change the way the email
851 * is built. Should be associative. See $_build_params.
852 * @param mixed $filename Output filename or file pointer where to save
853 * the message instead of returning it
854 * @param bool $skip_head True if you want to return/save only the message
855 * without headers
856 *
857 * @return mixed The MIME message content string, null or PEAR error object
858 */
859 public function get($params = null, $filename = null, $skip_head = false)
860 {
861 if (!empty($params) && is_array($params)) {
862 $this->build_params = array_merge($this->build_params, $params);
863 }
864
865 if (isset($this->headers['From'])) {
866 // Bug #11381: Illegal characters in domain ID
867 if (preg_match('#(@[0-9a-zA-Z\-\.]+)#', $this->headers['From'], $matches)) {
868 $domainID = $matches[1];
869 } else {
870 $domainID = '@localhost';
871 }
872
873 foreach ($this->html_images as $i => $img) {
874 $cid = $this->html_images[$i]['cid'];
875 if (!preg_match('#'.preg_quote($domainID).'$#', $cid)) {
876 $this->html_images[$i]['cid'] = $cid . $domainID;
877 }
878 }
879 }
880
881 if (count($this->html_images) && strlen($this->htmlbody) > 0) {
882 foreach ($this->html_images as $key => $value) {
883 $rval = preg_quote($value['name'], '#');
884 $regex = array(
885 '#(\s)((?i)src|background|href(?-i))\s*=\s*(["\']?)' . $rval . '\3#',
886 '#(?i)url(?-i)\(\s*(["\']?)' . $rval . '\1\s*\)#',
887 );
888
889 $rep = array(
890 '\1\2=\3cid:' . $value['cid'] .'\3',
891 'url(\1cid:' . $value['cid'] . '\1)',
892 );
893
894 $this->htmlbody = preg_replace($regex, $rep, $this->htmlbody);
895 $this->html_images[$key]['name']
896 = $this->basename($this->html_images[$key]['name']);
897 }
898 }
899
900 $this->checkParams();
901
902 $message = $this->buildBodyPart();
903
904 if (!isset($message)) {
905 return null;
906 }
907
908 // Use saved boundary
909 if (!empty($this->build_params['boundary'])) {
910 $boundary = $this->build_params['boundary'];
911 } else {
912 $boundary = null;
913 }
914
915 // Write output to file
916 if ($filename) {
917 // Append mimePart message headers and body into file
918 $headers = $message->encodeToFile($filename, $boundary, $skip_head);
919 if (self::isError($headers)) {
920 return $headers;
921 }
922 $this->headers = array_merge($this->headers, $headers);
923 } else {
924 $output = $message->encode($boundary, $skip_head);
925 if (self::isError($output)) {
926 return $output;
927 }
928 $this->headers = array_merge($this->headers, $output['headers']);
929 }
930
931 // remember the boundary used, in case we'd handle headers() call later
932 if (empty($boundary) && !empty($this->headers['Content-Type'])) {
933 if (preg_match('/boundary="([^"]+)/', $this->headers['Content-Type'], $m)) {
934 $this->build_params['boundary'] = $m[1];
935 }
936 }
937
938 return $filename ? null : $output['body'];
939 }
940
941 /**
942 * Builds the main body MIME part for the email body. It will add a mixed part
943 * if attachments are found. If no attachments are found it will return an
944 * alternative part if several body texts are found (text, html, calendar),
945 * or a single part if only one body text is found.
946 *
947 * @return Mail_mimePart|null The corresponding part for the body or null.
948 *
949 * @see buildAlternativeParts
950 * @see buildHtmlParts
951 */
952 protected function buildBodyPart()
953 {
954 $parts_count = count($this->parts);
955 $mixed_params = array('preamble' => $this->build_params['preamble']);
956 $message = null;
957
958 if ($parts_count > 0) {
959 $message = $this->addMixedPart($mixed_params);
960 $this->buildAlternativeParts($message, null);
961 for ($i = 0; $i < $parts_count; $i++) {
962 $this->addAttachmentPart($message, $this->parts[$i]);
963 }
964 } else {
965 $message = $this->buildAlternativeParts(null, $mixed_params);
966 }
967
968 return $message;
969 }
970
971 /**
972 * Builds a single text, html, or calendar part only if one of them is found.
973 * If two or more parts are found, then an alternative part containing them is built.
974 *
975 * @param Mail_mimePart|null $parent_part The parent mime part to add
976 * the part or null
977 * @param array $mixed_params The needed params to create the
978 * part when no parent_part is
979 * received.
980 *
981 * @return null|Mail_mimePart The main part built inside the method. It will be an
982 * alternative part or text, html, or calendar part.
983 * Null if no body texts are found.
984 */
985 protected function buildAlternativeParts($parent_part, $mixed_params = null)
986 {
987 $html = strlen($this->htmlbody) > 0;
988 $calendar = strlen($this->calbody) > 0;
989 $has_text = strlen($this->txtbody) > 0;
990 $alternatives_count = $html + $calendar + $has_text;
991
992 if ($alternatives_count > 1) {
993 $alt_part = $this->addAlternativePart($parent_part ? $parent_part : $mixed_params);
994 } else {
995 $alt_part = null;
996 }
997
998 $dest_part = $alt_part ? $alt_part : $parent_part;
999 $part = null;
1000
1001 if ($has_text) {
1002 $part = $this->addTextPart($dest_part);
1003 }
1004
1005 if ($html) {
1006 $part = $this->buildHtmlParts($dest_part);
1007 }
1008
1009 if ($calendar) {
1010 $part = $this->addCalendarPart($dest_part);
1011 }
1012
1013 return $dest_part ? $dest_part : $part;
1014 }
1015
1016 /**
1017 * Builds html part as a single part or inside a related part with the html
1018 * images thar were found.
1019 *
1020 * @param Mail_mimePart|null $parent_part The object to add the part to,
1021 * or anything else if a new object
1022 * is to be created.
1023 *
1024 * @return Mail_mimePart|null The created part or null if no htmlbody found.
1025 */
1026 protected function buildHtmlParts($parent_part)
1027 {
1028 if (!strlen($this->htmlbody)) {
1029 return null;
1030 }
1031
1032 $count_html_images = count($this->html_images);
1033
1034 if ($count_html_images > 0) {
1035 $part = $this->addRelatedPart($parent_part);
1036 $this->addHtmlPart($part);
1037 } else {
1038 $part = $this->addHtmlPart($parent_part);
1039 }
1040
1041 for ($i = 0; $i < $count_html_images; $i++) {
1042 $this->addHtmlImagePart($part, $this->html_images[$i]);
1043 }
1044
1045 return $part;
1046 }
1047
1048 /**
1049 * Returns an array with the headers needed to prepend to the email
1050 * (MIME-Version and Content-Type). Format of argument is:
1051 * $array['header-name'] = 'header-value';
1052 *
1053 * @param array $xtra_headers Assoc array with any extra headers (optional)
1054 * (Don't set Content-Type for multipart messages here!)
1055 * @param bool $overwrite Overwrite already existing headers.
1056 * @param bool $skip_content Don't return content headers: Content-Type,
1057 * Content-Disposition and Content-Transfer-Encoding
1058 *
1059 * @return array Assoc array with the mime headers
1060 */
1061 public function headers($xtra_headers = null, $overwrite = false, $skip_content = false)
1062 {
1063 // Add mime version header
1064 $headers['MIME-Version'] = '1.0';
1065
1066 if (!empty($xtra_headers)) {
1067 $headers = array_merge($headers, $xtra_headers);
1068 }
1069
1070 if ($overwrite) {
1071 $this->headers = array_merge($this->headers, $headers);
1072 } else {
1073 $this->headers = array_merge($headers, $this->headers);
1074 }
1075
1076 // Always reset Content-Type/Content-Transfer-Encoding headers
1077 // In case the message structure changed in meantime
1078 unset($this->headers['Content-Type']);
1079 unset($this->headers['Content-Transfer-Encoding']);
1080 unset($this->headers['Content-Disposition']);
1081
1082 $this->headers = array_merge($this->headers, $this->contentHeaders());
1083
1084 $headers = $this->headers;
1085
1086 if ($skip_content) {
1087 unset($headers['Content-Type']);
1088 unset($headers['Content-Transfer-Encoding']);
1089 unset($headers['Content-Disposition']);
1090 } else if (!empty($this->build_params['ctype'])) {
1091 $headers['Content-Type'] = $this->build_params['ctype'];
1092 }
1093
1094 return $this->encodeHeaders($headers);
1095 }
1096
1097 /**
1098 * Get the text version of the headers
1099 * (usefull if you want to use the PHP mail() function)
1100 *
1101 * @param array $xtra_headers Assoc array with any extra headers (optional)
1102 * (Don't set Content-Type for multipart messages here!)
1103 * @param bool $overwrite Overwrite the existing headers with new.
1104 * @param bool $skip_content Don't return content headers: Content-Type,
1105 * Content-Disposition and Content-Transfer-Encoding
1106 *
1107 * @return string Plain text headers
1108 */
1109 public function txtHeaders($xtra_headers = null, $overwrite = false, $skip_content = false)
1110 {
1111 $headers = $this->headers($xtra_headers, $overwrite, $skip_content);
1112
1113 // Place Received: headers at the beginning of the message
1114 // Spam detectors often flag messages with it after the Subject: as spam
1115 if (isset($headers['Received'])) {
1116 $received = $headers['Received'];
1117 unset($headers['Received']);
1118 $headers = array('Received' => $received) + $headers;
1119 }
1120
1121 $ret = '';
1122 $eol = $this->build_params['eol'];
1123
1124 foreach ($headers as $key => $val) {
1125 if (is_array($val)) {
1126 foreach ($val as $value) {
1127 $ret .= "$key: $value" . $eol;
1128 }
1129 } else {
1130 $ret .= "$key: $val" . $eol;
1131 }
1132 }
1133
1134 return $ret;
1135 }
1136
1137 /**
1138 * Sets message Content-Type header.
1139 * Use it to build messages with various content-types e.g. miltipart/raport
1140 * not supported by contentHeaders() function.
1141 *
1142 * @param string $type Type name
1143 * @param array $params Hash array of header parameters
1144 *
1145 * @return void
1146 * @since 1.7.0
1147 */
1148 public function setContentType($type, $params = array())
1149 {
1150 $header = $type;
1151
1152 $eol = !empty($this->build_params['eol']) ? $this->build_params['eol'] : "\r\n";
1153
1154 // add parameters
1155 $token_regexp = '#([^\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7E])#';
1156
1157 if (is_array($params)) {
1158 foreach ($params as $name => $value) {
1159 if ($name == 'boundary') {
1160 $this->build_params['boundary'] = $value;
1161 } else if (!preg_match($token_regexp, $value)) {
1162 $header .= ";$eol $name=$value";
1163 } else {
1164 $value = addcslashes($value, '\\"');
1165 $header .= ";$eol $name=\"$value\"";
1166 }
1167 }
1168 }
1169
1170 // add required boundary parameter if not defined
1171 if (stripos($type, 'multipart/') === 0) {
1172 if (empty($this->build_params['boundary'])) {
1173 $this->build_params['boundary'] = '=_' . md5(rand() . microtime());
1174 }
1175
1176 $header .= ";$eol boundary=\"".$this->build_params['boundary']."\"";
1177 }
1178
1179 $this->build_params['ctype'] = $header;
1180 }
1181
1182 /**
1183 * Sets the Subject header
1184 *
1185 * @param string $subject String to set the subject to.
1186 *
1187 * @return void
1188 */
1189 public function setSubject($subject)
1190 {
1191 $this->headers['Subject'] = $subject;
1192 }
1193
1194 /**
1195 * Set an email to the From (the sender) header
1196 *
1197 * @param string $email The email address to use
1198 *
1199 * @return void
1200 */
1201 public function setFrom($email)
1202 {
1203 $this->headers['From'] = $email;
1204 }
1205
1206 /**
1207 * Add an email to the To header
1208 * (multiple calls to this method are allowed)
1209 *
1210 * @param string $email The email direction to add
1211 *
1212 * @return void
1213 */
1214 public function addTo($email)
1215 {
1216 if (isset($this->headers['To'])) {
1217 $this->headers['To'] .= ", $email";
1218 } else {
1219 $this->headers['To'] = $email;
1220 }
1221 }
1222
1223 /**
1224 * Add an email to the Cc (carbon copy) header
1225 * (multiple calls to this method are allowed)
1226 *
1227 * @param string $email The email direction to add
1228 *
1229 * @return void
1230 */
1231 public function addCc($email)
1232 {
1233 if (isset($this->headers['Cc'])) {
1234 $this->headers['Cc'] .= ", $email";
1235 } else {
1236 $this->headers['Cc'] = $email;
1237 }
1238 }
1239
1240 /**
1241 * Add an email to the Bcc (blank carbon copy) header
1242 * (multiple calls to this method are allowed)
1243 *
1244 * @param string $email The email direction to add
1245 *
1246 * @return void
1247 */
1248 public function addBcc($email)
1249 {
1250 if (isset($this->headers['Bcc'])) {
1251 $this->headers['Bcc'] .= ", $email";
1252 } else {
1253 $this->headers['Bcc'] = $email;
1254 }
1255 }
1256
1257 /**
1258 * Since the PHP send function requires you to specify
1259 * recipients (To: header) separately from the other
1260 * headers, the To: header is not properly encoded.
1261 * To fix this, you can use this public method to encode
1262 * your recipients before sending to the send function.
1263 *
1264 * @param string $recipients A comma-delimited list of recipients
1265 *
1266 * @return string Encoded data
1267 */
1268 public function encodeRecipients($recipients)
1269 {
1270 $input = array('To' => $recipients);
1271 $retval = $this->encodeHeaders($input);
1272
1273 return $retval['To'] ;
1274 }
1275
1276 /**
1277 * Encodes headers as per RFC2047
1278 *
1279 * @param array $input The header data to encode
1280 * @param array $params Extra build parameters
1281 *
1282 * @return array Encoded data
1283 */
1284 protected function encodeHeaders($input, $params = array())
1285 {
1286 $build_params = $this->build_params;
1287
1288 if (!empty($params)) {
1289 $build_params = array_merge($build_params, $params);
1290 }
1291
1292 foreach ($input as $hdr_name => $hdr_value) {
1293 if (is_array($hdr_value)) {
1294 foreach ($hdr_value as $idx => $value) {
1295 $input[$hdr_name][$idx] = $this->encodeHeader(
1296 $hdr_name, $value,
1297 $build_params['head_charset'], $build_params['head_encoding']
1298 );
1299 }
1300 } else if ($hdr_value !== null) {
1301 $input[$hdr_name] = $this->encodeHeader(
1302 $hdr_name, $hdr_value,
1303 $build_params['head_charset'], $build_params['head_encoding']
1304 );
1305 } else {
1306 unset($input[$hdr_name]);
1307 }
1308 }
1309
1310 return $input;
1311 }
1312
1313 /**
1314 * Encodes a header as per RFC2047
1315 *
1316 * @param string $name The header name
1317 * @param string $value The header data to encode
1318 * @param string $charset Character set name
1319 * @param string $encoding Encoding name (base64 or quoted-printable)
1320 *
1321 * @return string Encoded header data (without a name)
1322 * @since 1.5.3
1323 */
1324 public function encodeHeader($name, $value, $charset, $encoding)
1325 {
1326 return Mail_mimePart::encodeHeader(
1327 $name, $value, $charset, $encoding, $this->build_params['eol']
1328 );
1329 }
1330
1331 /**
1332 * Get file's basename (locale independent)
1333 *
1334 * @param string $filename Filename
1335 *
1336 * @return string Basename
1337 */
1338 protected function basename($filename)
1339 {
1340 // basename() is not unicode safe and locale dependent
1341 if (stristr(PHP_OS, 'win') || stristr(PHP_OS, 'netware')) {
1342 return preg_replace('/^.*[\\\\\\/]/', '', $filename);
1343 } else {
1344 return preg_replace('/^.*[\/]/', '', $filename);
1345 }
1346 }
1347
1348 /**
1349 * Get Content-Type and Content-Transfer-Encoding headers of the message
1350 *
1351 * @return array Headers array
1352 */
1353 protected function contentHeaders()
1354 {
1355 $attachments = count($this->parts) > 0;
1356 $html_images = count($this->html_images) > 0;
1357 $html = strlen($this->htmlbody) > 0;
1358 $calendar = strlen($this->calbody) > 0;
1359 $has_text = strlen($this->txtbody) > 0;
1360 $has_alternatives = ($html + $calendar + $has_text) > 1;
1361 $headers = array();
1362
1363 // See get()
1364 switch (true) {
1365 case $has_text && !$attachments && !$has_alternatives:
1366 $headers['Content-Type'] = 'text/plain';
1367 break;
1368
1369 case $html && !$html_images && !$attachments && !$has_alternatives:
1370 $headers['Content-Type'] = 'text/html';
1371 break;
1372
1373 case $html && $html_images && !$attachments && !$has_alternatives:
1374 $headers['Content-Type'] = 'multipart/related';
1375 break;
1376
1377 case $calendar && !$attachments && !$has_alternatives:
1378 $headers['Content-Type'] = 'text/calendar';
1379 break;
1380
1381 case $has_alternatives && !$attachments:
1382 $headers['Content-Type'] = 'multipart/alternative';
1383 break;
1384
1385 case $attachments:
1386 $headers['Content-Type'] = 'multipart/mixed';
1387 break;
1388 }
1389
1390 // Note: This is outside of the above switch construct to workaround
1391 // opcache bug: https://bugzilla.opensuse.org/show_bug.cgi?id=1166235
1392 if (empty($headers)) {
1393 return $headers;
1394 }
1395
1396 $this->checkParams();
1397
1398 $eol = !empty($this->build_params['eol'])
1399 ? $this->build_params['eol'] : "\r\n";
1400
1401 if ($headers['Content-Type'] == 'text/plain') {
1402 // single-part message: add charset and encoding
1403 if ($this->build_params['text_charset']) {
1404 $charset = 'charset=' . $this->build_params['text_charset'];
1405 // place charset parameter in the same line, if possible
1406 // 26 = strlen("Content-Type: text/plain; ")
1407 $headers['Content-Type']
1408 .= (strlen($charset) + 26 <= 76) ? "; $charset" : ";$eol $charset";
1409 }
1410
1411 $headers['Content-Transfer-Encoding']
1412 = $this->build_params['text_encoding'];
1413 } else if ($headers['Content-Type'] == 'text/html') {
1414 // single-part message: add charset and encoding
1415 if ($this->build_params['html_charset']) {
1416 $charset = 'charset=' . $this->build_params['html_charset'];
1417 // place charset parameter in the same line, if possible
1418 $headers['Content-Type']
1419 .= (strlen($charset) + 25 <= 76) ? "; $charset" : ";$eol $charset";
1420 }
1421 $headers['Content-Transfer-Encoding']
1422 = $this->build_params['html_encoding'];
1423 } else if ($headers['Content-Type'] == 'text/calendar') {
1424 // single-part message: add charset and encoding
1425 if ($this->build_params['calendar_charset']) {
1426 $charset = 'charset=' . $this->build_params['calendar_charset'];
1427 $headers['Content-Type'] .= "; $charset";
1428 }
1429
1430 if ($this->build_params['calendar_method']) {
1431 $method = 'method=' . $this->build_params['calendar_method'];
1432 $headers['Content-Type'] .= "; $method";
1433 }
1434
1435 $headers['Content-Transfer-Encoding']
1436 = $this->build_params['calendar_encoding'];
1437 } else {
1438 // multipart message: and boundary
1439 if (!empty($this->build_params['boundary'])) {
1440 $boundary = $this->build_params['boundary'];
1441 } else if (!empty($this->headers['Content-Type'])
1442 && preg_match('/boundary="([^"]+)"/', $this->headers['Content-Type'], $m)
1443 ) {
1444 $boundary = $m[1];
1445 } else {
1446 $boundary = '=_' . md5(rand() . microtime());
1447 }
1448
1449 $this->build_params['boundary'] = $boundary;
1450 $headers['Content-Type'] .= ";$eol boundary=\"$boundary\"";
1451 }
1452
1453 return $headers;
1454 }
1455
1456 /**
1457 * Validate and set build parameters
1458 *
1459 * @return void
1460 */
1461 protected function checkParams()
1462 {
1463 $encodings = array('7bit', '8bit', 'base64', 'quoted-printable');
1464
1465 $this->build_params['text_encoding']
1466 = strtolower($this->build_params['text_encoding']);
1467 $this->build_params['html_encoding']
1468 = strtolower($this->build_params['html_encoding']);
1469 $this->build_params['calendar_encoding']
1470 = strtolower($this->build_params['calendar_encoding']);
1471
1472 if (!in_array($this->build_params['text_encoding'], $encodings)) {
1473 $this->build_params['text_encoding'] = '7bit';
1474 }
1475 if (!in_array($this->build_params['html_encoding'], $encodings)) {
1476 $this->build_params['html_encoding'] = '7bit';
1477 }
1478 if (!in_array($this->build_params['calendar_encoding'], $encodings)) {
1479 $this->build_params['calendar_encoding'] = '7bit';
1480 }
1481
1482 // text body
1483 if ($this->build_params['text_encoding'] == '7bit'
1484 && !preg_match('/ascii/i', $this->build_params['text_charset'])
1485 && preg_match('/[^\x00-\x7F]/', $this->txtbody)
1486 ) {
1487 $this->build_params['text_encoding'] = 'quoted-printable';
1488 }
1489 // html body
1490 if ($this->build_params['html_encoding'] == '7bit'
1491 && !preg_match('/ascii/i', $this->build_params['html_charset'])
1492 && preg_match('/[^\x00-\x7F]/', $this->htmlbody)
1493 ) {
1494 $this->build_params['html_encoding'] = 'quoted-printable';
1495 }
1496 // calendar body
1497 if ($this->build_params['calendar_encoding'] == '7bit'
1498 && !preg_match('/ascii/i', $this->build_params['calendar_charset'])
1499 && preg_match('/[^\x00-\x7F]/', $this->calbody)
1500 ) {
1501 $this->build_params['calendar_encoding'] = 'quoted-printable';
1502 }
1503 }
1504
1505 /**
1506 * Set body of specified message part
1507 *
1508 * @param string $type One of: txtbody, calbody, htmlbody
1509 * @param string $data Either a string or the file name with the contents
1510 * @param bool $isfile If true the first param should be treated
1511 * as a file name, else as a string (default)
1512 * @param bool $append If true the text or file is appended to
1513 * the existing body, else the old body is
1514 * overwritten
1515 *
1516 * @return mixed True on success or PEAR_Error object
1517 */
1518 protected function setBody($type, $data, $isfile = false, $append = false)
1519 {
1520 if ($isfile) {
1521 $data = $this->file2str($data);
1522 if (self::isError($data)) {
1523 return $data;
1524 }
1525 }
1526
1527 if (!$append) {
1528 $this->{$type} = $data;
1529 } else {
1530 $this->{$type} .= $data;
1531 }
1532
1533 return true;
1534 }
1535
1536 /**
1537 * Adds a subpart to the mimePart object and
1538 * returns it during the build process.
1539 *
1540 * @param mixed $obj The object to add the part to, or
1541 * anything else if a new object is to be created.
1542 * @param string $body Part body
1543 * @param string $ctype Part content type
1544 * @param string $type Internal part type
1545 *
1546 * @return Mail_mimePart The mimePart object
1547 */
1548 protected function addBodyPart($obj, $body, $ctype, $type)
1549 {
1550 $params['content_type'] = $ctype;
1551 $params['encoding'] = $this->build_params[$type . '_encoding'];
1552 $params['charset'] = $this->build_params[$type . '_charset'];
1553 $params['eol'] = $this->build_params['eol'];
1554
1555 if (is_object($obj)) {
1556 $ret = $obj->addSubpart($body, $params);
1557 } else {
1558 $ret = new Mail_mimePart($body, $params);
1559 }
1560
1561 return $ret;
1562 }
1563
1564 /**
1565 * PEAR::isError implementation
1566 *
1567 * @param mixed $data Object
1568 *
1569 * @return bool True if object is an instance of PEAR_Error
1570 */
1571 public static function isError($data)
1572 {
1573 // PEAR::isError() is not PHP 5.4 compatible (see Bug #19473)
1574 if (is_a($data, 'PEAR_Error')) {
1575 return true;
1576 }
1577
1578 return false;
1579 }
1580
1581 /**
1582 * PEAR::raiseError implementation
1583 *
1584 * @param string $message A text error message
1585 *
1586 * @return PEAR_Error Instance of PEAR_Error
1587 */
1588 public static function raiseError($message)
1589 {
1590 // PEAR::raiseError() is not PHP 5.4 compatible
1591 return new PEAR_Error($message);
1592 }
1593 }