Debian Codegraph @

lib/3rdParty/composer/webklex/php-imap/src/Message.php

raw symbols SCIP
1 <?php
2 /*
3 * File: Message.php
4 * Category: -
5 * Author: M. Goldenbaum
6 * Created: 19.01.17 22:21
7 * Updated: -
8 *
9 * Description:
10 * -
11 */
12
13 namespace Webklex\PHPIMAP;
14
15 use Exception;
16 use Illuminate\Support\Str;
17 use ReflectionClass;
18 use ReflectionException;
19 use Webklex\PHPIMAP\Decoder\Decoder;
20 use Webklex\PHPIMAP\Decoder\DecoderInterface;
21 use Webklex\PHPIMAP\Exceptions\AuthFailedException;
22 use Webklex\PHPIMAP\Exceptions\ConnectionFailedException;
23 use Webklex\PHPIMAP\Exceptions\DecoderNotFoundException;
24 use Webklex\PHPIMAP\Exceptions\EventNotFoundException;
25 use Webklex\PHPIMAP\Exceptions\FolderFetchingException;
26 use Webklex\PHPIMAP\Exceptions\GetMessagesFailedException;
27 use Webklex\PHPIMAP\Exceptions\ImapBadRequestException;
28 use Webklex\PHPIMAP\Exceptions\ImapServerErrorException;
29 use Webklex\PHPIMAP\Exceptions\InvalidMessageDateException;
30 use Webklex\PHPIMAP\Exceptions\MaskNotFoundException;
31 use Webklex\PHPIMAP\Exceptions\MessageContentFetchingException;
32 use Webklex\PHPIMAP\Exceptions\MessageFlagException;
33 use Webklex\PHPIMAP\Exceptions\MessageHeaderFetchingException;
34 use Webklex\PHPIMAP\Exceptions\MessageNotFoundException;
35 use Webklex\PHPIMAP\Exceptions\MessageSizeFetchingException;
36 use Webklex\PHPIMAP\Exceptions\MethodNotFoundException;
37 use Webklex\PHPIMAP\Exceptions\ResponseException;
38 use Webklex\PHPIMAP\Exceptions\RuntimeException;
39 use Webklex\PHPIMAP\Support\AttachmentCollection;
40 use Webklex\PHPIMAP\Support\FlagCollection;
41 use Webklex\PHPIMAP\Support\Masks\MessageMask;
42 use Webklex\PHPIMAP\Support\MessageCollection;
43 use Webklex\PHPIMAP\Traits\HasEvents;
44
45 /**
46 * Class Message
47 *
48 * @package Webklex\PHPIMAP
49 *
50 * @property integer $msglist
51 * @property integer $uid
52 * @property integer $msgn
53 * @property integer $size
54 * @property Attribute $subject
55 * @property Attribute $message_id
56 * @property Attribute $message_no
57 * @property Attribute $references
58 * @property Attribute $date
59 * @property Attribute $from
60 * @property Attribute $to
61 * @property Attribute $cc
62 * @property Attribute $bcc
63 * @property Attribute $reply_to
64 * @property Attribute $in_reply_to
65 * @property Attribute $sender
66 *
67 * @method integer getMsglist()
68 * @method integer setMsglist($msglist)
69 * @method integer getUid()
70 * @method integer getMsgn()
71 * @method integer getSize()
72 * @method Attribute getPriority()
73 * @method Attribute getSubject()
74 * @method Attribute getMessageId()
75 * @method Attribute getMessageNo()
76 * @method Attribute getReferences()
77 * @method Attribute getDate()
78 * @method Attribute getFrom()
79 * @method Attribute getTo()
80 * @method Attribute getCc()
81 * @method Attribute getBcc()
82 * @method Attribute getReplyTo()
83 * @method Attribute getInReplyTo()
84 * @method Attribute getSender()
85 */
86 class Message {
87 use HasEvents;
88
89 /**
90 * Client instance
91 *
92 * @var ?Client
93 */
94 private ?Client $client;
95
96 /**
97 * Default mask
98 *
99 * @var string $mask
100 */
101 protected string $mask = MessageMask::class;
102
103 /**
104 * Used options
105 *
106 * @var array $options
107 */
108 protected array $options = [];
109
110 /**
111 * All library configs
112 *
113 * @var Config $config
114 */
115 protected Config $config;
116
117 /**
118 * Decoder instance
119 *
120 * @var DecoderInterface $decoder
121 */
122 protected DecoderInterface $decoder;
123
124 /**
125 * Attribute holder
126 *
127 * @var Attribute[]|array $attributes
128 */
129 protected array $attributes = [];
130
131 /**
132 * The message folder path
133 *
134 * @var string $folder_path
135 */
136 protected string $folder_path;
137
138 /**
139 * Fetch body options
140 *
141 * @var ?integer
142 */
143 public ?int $fetch_options = null;
144
145 /**
146 * @var integer
147 */
148 protected int $sequence = IMAP::NIL;
149
150 /**
151 * Fetch body options
152 *
153 * @var bool
154 */
155 public bool $fetch_body = true;
156
157 /**
158 * Fetch flags options
159 *
160 * @var bool
161 */
162 public bool $fetch_flags = true;
163
164 /**
165 * @var ?Header $header
166 */
167 public ?Header $header = null;
168
169 /**
170 * Raw message body
171 *
172 * @var string $raw_body
173 */
174 protected string $raw_body = "";
175
176 /**
177 * Message structure
178 *
179 * @var ?Structure $structure
180 */
181 protected ?Structure $structure = null;
182
183 /**
184 * Message body components
185 *
186 * @var array $bodies
187 */
188 public array $bodies = [];
189
190 /** @var AttachmentCollection $attachments */
191 public AttachmentCollection $attachments;
192
193 /** @var FlagCollection $flags */
194 public FlagCollection $flags;
195
196 /**
197 * A list of all available and supported flags
198 *
199 * @var ?array $available_flags
200 */
201 private ?array $available_flags = null;
202
203 /**
204 * Message constructor.
205 * @param integer $uid
206 * @param integer|null $msglist
207 * @param Client $client
208 * @param integer|null $fetch_options
209 * @param boolean $fetch_body
210 * @param boolean $fetch_flags
211 * @param integer|null $sequence
212 *
213 * @throws AuthFailedException
214 * @throws ConnectionFailedException
215 * @throws EventNotFoundException
216 * @throws ImapBadRequestException
217 * @throws ImapServerErrorException
218 * @throws InvalidMessageDateException
219 * @throws MessageContentFetchingException
220 * @throws MessageFlagException
221 * @throws MessageHeaderFetchingException
222 * @throws RuntimeException
223 * @throws ResponseException
224 */
225 public function __construct(int $uid, ?int $msglist, Client $client, ?int $fetch_options = null, bool $fetch_body = false, bool $fetch_flags = false, ?int $sequence = null) {
226 $this->boot($client->getConfig());
227
228 $default_mask = $client->getDefaultMessageMask();
229 if ($default_mask != null) {
230 $this->mask = $default_mask;
231 }
232 $this->events["message"] = $client->getDefaultEvents("message");
233 $this->events["flag"] = $client->getDefaultEvents("flag");
234
235 $this->folder_path = $client->getFolderPath();
236
237 $this->setSequence($sequence);
238 $this->setFetchOption($fetch_options);
239 $this->setFetchBodyOption($fetch_body);
240 $this->setFetchFlagsOption($fetch_flags);
241
242 $this->client = $client;
243 $this->client->openFolder($this->folder_path);
244
245 $this->setSequenceId($uid, $msglist);
246
247 if ($this->fetch_options == IMAP::FT_PEEK) {
248 $this->parseFlags();
249 }
250
251 $this->parseHeader();
252
253 if ($this->getFetchBodyOption() === true) {
254 $this->parseBody();
255 }
256
257 if ($this->getFetchFlagsOption() === true && $this->fetch_options !== IMAP::FT_PEEK) {
258 $this->parseFlags();
259 }
260 }
261
262 /**
263 * Create a new instance without fetching the message header and providing them raw instead
264 * @param int $uid
265 * @param int|null $msglist
266 * @param Client $client
267 * @param string $raw_header
268 * @param string $raw_body
269 * @param array $raw_flags
270 * @param null $fetch_options
271 * @param null $sequence
272 *
273 * @return Message
274 * @throws AuthFailedException
275 * @throws ConnectionFailedException
276 * @throws EventNotFoundException
277 * @throws ImapBadRequestException
278 * @throws ImapServerErrorException
279 * @throws InvalidMessageDateException
280 * @throws MessageContentFetchingException
281 * @throws MessageFlagException
282 * @throws ReflectionException
283 * @throws RuntimeException
284 * @throws ResponseException
285 */
286 public static function make(int $uid, ?int $msglist, Client $client, string $raw_header, string $raw_body, array $raw_flags, $fetch_options = null, $sequence = null): Message {
287 $reflection = new ReflectionClass(self::class);
288 /** @var Message $instance */
289 $instance = $reflection->newInstanceWithoutConstructor();
290 $instance->boot($client->getConfig());
291
292 $default_mask = $client->getDefaultMessageMask();
293 if ($default_mask != null) {
294 $instance->setMask($default_mask);
295 }
296 $instance->setEvents([
297 "message" => $client->getDefaultEvents("message"),
298 "flag" => $client->getDefaultEvents("flag"),
299 ]);
300 $instance->setFolderPath($client->getFolderPath());
301 $instance->setSequence($sequence);
302 $instance->setFetchOption($fetch_options);
303
304 $instance->setClient($client);
305 $instance->setSequenceId($uid, $msglist);
306
307 $instance->parseRawHeader($raw_header);
308 $instance->parseRawFlags($raw_flags);
309 $instance->parseRawBody($raw_body);
310 $instance->peek();
311
312 return $instance;
313 }
314
315 /**
316 * Create a new message instance by reading and loading a file or remote location
317 * @param string $filename
318 * @param ?Config $config
319 *
320 * @return Message
321 * @throws AuthFailedException
322 * @throws ConnectionFailedException
323 * @throws ImapBadRequestException
324 * @throws ImapServerErrorException
325 * @throws InvalidMessageDateException
326 * @throws MaskNotFoundException
327 * @throws MessageContentFetchingException
328 * @throws ReflectionException
329 * @throws ResponseException
330 * @throws RuntimeException
331 */
332 public static function fromFile(string $filename, ?Config $config = null): Message {
333 $blob = file_get_contents($filename);
334 if ($blob === false) {
335 throw new RuntimeException("Unable to read file");
336 }
337 return self::fromString($blob, $config);
338 }
339
340 /**
341 * Create a new message instance by reading and loading a string
342 * @param string $blob
343 * @param ?Config $config
344 *
345 * @return Message
346 * @throws AuthFailedException
347 * @throws ConnectionFailedException
348 * @throws ImapBadRequestException
349 * @throws ImapServerErrorException
350 * @throws InvalidMessageDateException
351 * @throws MaskNotFoundException
352 * @throws MessageContentFetchingException
353 * @throws ReflectionException
354 * @throws ResponseException
355 * @throws RuntimeException
356 */
357 public static function fromString(string $blob, ?Config $config = null): Message {
358 $reflection = new ReflectionClass(self::class);
359 /** @var Message $instance */
360 $instance = $reflection->newInstanceWithoutConstructor();
361 $instance->boot($config);
362
363 $default_mask = $instance->getConfig()->getMask("message");
364 if($default_mask != ""){
365 $instance->setMask($default_mask);
366 }else{
367 throw new MaskNotFoundException("Unknown message mask provided");
368 }
369
370 if(!str_contains($blob, "\r\n")){
371 $blob = str_replace("\n", "\r\n", $blob);
372 }
373 $raw_header = substr($blob, 0, strpos($blob, "\r\n\r\n"));
374 $raw_body = substr($blob, strlen($raw_header)+4);
375
376 $instance->parseRawHeader($raw_header);
377 $instance->parseRawBody($raw_body);
378
379 $instance->setUid(0);
380
381 return $instance;
382 }
383
384 /**
385 * Boot a new instance
386 * @param ?Config $config
387 * @throws DecoderNotFoundException
388 */
389 public function boot(?Config $config = null): void {
390 $this->attributes = [];
391 $this->client = null;
392 $this->config = $config ?? Config::make();
393 $this->decoder = $this->config->getDecoder("message");
394
395 $this->options = $this->config->get('options');
396 $this->available_flags = $this->config->get('flags');
397
398 $this->attachments = AttachmentCollection::make();
399 $this->flags = FlagCollection::make();
400 }
401
402 /**
403 * Call dynamic attribute setter and getter methods
404 * @param string $method
405 * @param array $arguments
406 *
407 * @return mixed
408 * @throws AuthFailedException
409 * @throws ConnectionFailedException
410 * @throws ImapBadRequestException
411 * @throws ImapServerErrorException
412 * @throws MessageNotFoundException
413 * @throws MethodNotFoundException
414 * @throws MessageSizeFetchingException
415 * @throws RuntimeException
416 * @throws ResponseException
417 */
418 public function __call(string $method, array $arguments) {
419 if (strtolower(substr($method, 0, 3)) === 'get') {
420 $name = Str::snake(substr($method, 3));
421 return $this->get($name);
422 } elseif (strtolower(substr($method, 0, 3)) === 'set') {
423 $name = Str::snake(substr($method, 3));
424
425 if (in_array($name, array_keys($this->attributes))) {
426 return $this->__set($name, array_pop($arguments));
427 }
428
429 }
430
431 throw new MethodNotFoundException("Method " . self::class . '::' . $method . '() is not supported');
432 }
433
434 /**
435 * Magic setter
436 * @param $name
437 * @param $value
438 *
439 * @return mixed
440 */
441 public function __set($name, $value) {
442 $this->attributes[$name] = $value;
443
444 return $this->attributes[$name];
445 }
446
447 /**
448 * Magic getter
449 * @param $name
450 *
451 * @return Attribute|mixed|null
452 * @throws AuthFailedException
453 * @throws ConnectionFailedException
454 * @throws ImapBadRequestException
455 * @throws ImapServerErrorException
456 * @throws MessageNotFoundException
457 * @throws MessageSizeFetchingException
458 * @throws RuntimeException
459 * @throws ResponseException
460 */
461 public function __get($name) {
462 return $this->get($name);
463 }
464
465 /**
466 * Get an available message or message header attribute
467 * @param $name
468 *
469 * @return Attribute|mixed|null
470 * @throws AuthFailedException
471 * @throws ConnectionFailedException
472 * @throws ImapBadRequestException
473 * @throws ImapServerErrorException
474 * @throws MessageNotFoundException
475 * @throws RuntimeException
476 * @throws ResponseException
477 * @throws MessageSizeFetchingException
478 */
479 public function get($name): mixed {
480 if (isset($this->attributes[$name]) && $this->attributes[$name] !== null) {
481 return $this->attributes[$name];
482 }
483
484 switch ($name){
485 case "uid":
486 $this->attributes[$name] = $this->client->getConnection()->getUid($this->msgn)->validate()->integer();
487 return $this->attributes[$name];
488 case "msgn":
489 $this->attributes[$name] = $this->client->getConnection()->getMessageNumber($this->uid)->validate()->integer();
490 return $this->attributes[$name];
491 case "size":
492 if (!isset($this->attributes[$name])) {
493 $this->fetchSize();
494 }
495 return $this->attributes[$name];
496 }
497
498 return $this->header->get($name);
499 }
500
501 /**
502 * Check if the Message has a text body
503 *
504 * @return bool
505 */
506 public function hasTextBody(): bool {
507 return isset($this->bodies['text']) && $this->bodies['text'] !== "";
508 }
509
510 /**
511 * Get the Message text body
512 *
513 * @return string
514 */
515 public function getTextBody(): string {
516 if (!isset($this->bodies['text'])) {
517 return "";
518 }
519
520 return $this->bodies['text'];
521 }
522
523 /**
524 * Check if the Message has a html body
525 *
526 * @return bool
527 */
528 public function hasHTMLBody(): bool {
529 return isset($this->bodies['html']) && $this->bodies['html'] !== "";
530 }
531
532 /**
533 * Get the Message html body
534 *
535 * @return string
536 */
537 public function getHTMLBody(): string {
538 if (!isset($this->bodies['html'])) {
539 return "";
540 }
541
542 return $this->bodies['html'];
543 }
544
545 /**
546 * Parse all defined headers
547 *
548 * @throws AuthFailedException
549 * @throws ConnectionFailedException
550 * @throws ImapBadRequestException
551 * @throws ImapServerErrorException
552 * @throws RuntimeException
553 * @throws InvalidMessageDateException
554 * @throws MessageHeaderFetchingException
555 * @throws ResponseException
556 */
557 private function parseHeader(): void {
558 $sequence_id = $this->getSequenceId();
559 $headers = $this->client->getConnection()->headers([$sequence_id], "RFC822", $this->sequence)->setCanBeEmpty(true)->validatedData();
560 if (!isset($headers[$sequence_id])) {
561 throw new MessageHeaderFetchingException("no headers found", 0);
562 }
563
564 $this->parseRawHeader($headers[$sequence_id]);
565 }
566
567 /**
568 * @param string $raw_header
569 *
570 * @throws InvalidMessageDateException
571 */
572 public function parseRawHeader(string $raw_header): void {
573 $this->header = new Header($raw_header, $this->getConfig());
574 }
575
576 /**
577 * Parse additional raw flags
578 * @param array $raw_flags
579 */
580 public function parseRawFlags(array $raw_flags): void {
581 $this->flags = FlagCollection::make();
582
583 foreach ($raw_flags as $flag) {
584 if (str_starts_with($flag, "\\")) {
585 $flag = substr($flag, 1);
586 }
587 $flag_key = strtolower($flag);
588 if ($this->available_flags === null || in_array($flag_key, $this->available_flags)) {
589 $this->flags->put($flag_key, $flag);
590 }
591 }
592 }
593
594 /**
595 * Parse additional flags
596 *
597 * @return void
598 * @throws AuthFailedException
599 * @throws ConnectionFailedException
600 * @throws ImapBadRequestException
601 * @throws ImapServerErrorException
602 * @throws MessageFlagException
603 * @throws RuntimeException
604 * @throws ResponseException
605 */
606 private function parseFlags(): void {
607 $this->client->openFolder($this->folder_path);
608 $this->flags = FlagCollection::make();
609
610 $sequence_id = $this->getSequenceId();
611 try {
612 $flags = $this->client->getConnection()->flags([$sequence_id], $this->sequence)->setCanBeEmpty(true)->validatedData();
613 } catch (Exceptions\RuntimeException $e) {
614 throw new MessageFlagException("flag could not be fetched", 0, $e);
615 }
616
617 if (isset($flags[$sequence_id])) {
618 $this->parseRawFlags($flags[$sequence_id]);
619 }
620 }
621
622 /**
623 * Parse the Message body
624 *
625 * @return Message
626 * @throws AuthFailedException
627 * @throws ConnectionFailedException
628 * @throws EventNotFoundException
629 * @throws ImapBadRequestException
630 * @throws ImapServerErrorException
631 * @throws InvalidMessageDateException
632 * @throws MessageContentFetchingException
633 * @throws MessageFlagException
634 * @throws RuntimeException
635 * @throws ResponseException
636 */
637 public function parseBody(): Message {
638 $this->client->openFolder($this->folder_path);
639
640 $sequence_id = $this->getSequenceId();
641 try {
642 $contents = $this->client->getConnection()->content([$sequence_id], $this->client->rfc, $this->sequence)->validatedData();
643 } catch (Exceptions\RuntimeException $e) {
644 throw new MessageContentFetchingException("failed to fetch content", 0, $e);
645 }
646 if (!isset($contents[$sequence_id])) {
647 throw new MessageContentFetchingException("no content found", 0);
648 }
649 $content = $contents[$sequence_id];
650
651 $body = $this->parseRawBody($content);
652 $this->peek();
653
654 return $body;
655 }
656
657 /**
658 * Fetches the size for this message.
659 *
660 * @throws AuthFailedException
661 * @throws ConnectionFailedException
662 * @throws ImapBadRequestException
663 * @throws ImapServerErrorException
664 * @throws MessageSizeFetchingException
665 * @throws ResponseException
666 * @throws RuntimeException
667 */
668 private function fetchSize(): void {
669 $sequence_id = $this->getSequenceId();
670 $sizes = $this->client->getConnection()->sizes([$sequence_id], $this->sequence)->validatedData();
671 if (!isset($sizes[$sequence_id])) {
672 throw new MessageSizeFetchingException("sizes did not set an array entry for the supplied sequence_id", 0);
673 }
674 $this->attributes["size"] = $sizes[$sequence_id];
675 }
676
677 /**
678 * Handle auto "Seen" flag handling
679 *
680 * @throws AuthFailedException
681 * @throws ConnectionFailedException
682 * @throws EventNotFoundException
683 * @throws ImapBadRequestException
684 * @throws ImapServerErrorException
685 * @throws MessageFlagException
686 * @throws RuntimeException
687 * @throws ResponseException
688 */
689 public function peek(): void {
690 if ($this->fetch_options == IMAP::FT_PEEK) {
691 if ($this->getFlags()->get("seen") == null) {
692 $this->unsetFlag("Seen");
693 }
694 } elseif ($this->getFlags()->get("seen") == null) {
695 $this->setFlag("Seen");
696 }
697 }
698
699 /**
700 * Parse a given message body
701 * @param string $raw_body
702 *
703 * @return Message
704 * @throws AuthFailedException
705 * @throws ConnectionFailedException
706 * @throws ImapBadRequestException
707 * @throws ImapServerErrorException
708 * @throws InvalidMessageDateException
709 * @throws MessageContentFetchingException
710 * @throws RuntimeException
711 * @throws ResponseException
712 */
713 public function parseRawBody(string $raw_body): Message {
714 $this->structure = new Structure($raw_body, $this->header);
715 $this->fetchStructure($this->structure);
716
717 return $this;
718 }
719
720 /**
721 * Fetch the Message structure
722 * @param Structure $structure
723 *
724 * @throws AuthFailedException
725 * @throws ConnectionFailedException
726 * @throws ImapBadRequestException
727 * @throws ImapServerErrorException
728 * @throws RuntimeException
729 * @throws ResponseException
730 */
731 private function fetchStructure(Structure $structure): void {
732 $this->client?->openFolder($this->folder_path);
733
734 foreach ($structure->parts as $part) {
735 $this->fetchPart($part);
736 }
737 }
738
739 /**
740 * Fetch a given part
741 * @param Part $part
742 */
743 private function fetchPart(Part $part): void {
744 if ($part->isAttachment()) {
745 $this->fetchAttachment($part);
746 } else {
747 $encoding = $this->decoder->getEncoding($part);
748
749 $content = $this->decoder->decode($part->content, $part->encoding);
750
751 // We don't need to do convertEncoding() if charset is ASCII (us-ascii):
752 // ASCII is a subset of UTF-8, so all ASCII files are already UTF-8 encoded
753 // https://stackoverflow.com/a/11303410
754 //
755 // us-ascii is the same as ASCII:
756 // ASCII is the traditional name for the encoding system; the Internet Assigned Numbers Authority (IANA)
757 // prefers the updated name US-ASCII, which clarifies that this system was developed in the US and
758 // based on the typographical symbols predominantly in use there.
759 // https://en.wikipedia.org/wiki/ASCII
760 //
761 // convertEncoding() function basically means convertToUtf8(), so when we convert ASCII string into UTF-8 it gets broken.
762 if ($encoding != 'us-ascii') {
763 $content = $this->decoder->convertEncoding($content, $encoding);
764 }
765
766 $this->addBody($part->subtype ?? '', $content);
767 }
768 }
769
770 /**
771 * Add a body to the message
772 * @param string $subtype
773 * @param string $content
774 *
775 * @return void
776 */
777 protected function addBody(string $subtype, string $content): void {
778 $subtype = strtolower($subtype);
779 $subtype = $subtype == "plain" || $subtype == "" ? "text" : $subtype;
780
781 if (isset($this->bodies[$subtype]) && $this->bodies[$subtype] !== null && $this->bodies[$subtype] !== "") {
782 if ($content !== "") {
783 $this->bodies[$subtype] .= "\n".$content;
784 }
785 } else {
786 $this->bodies[$subtype] = $content;
787 }
788 }
789
790 /**
791 * Fetch the Message attachment
792 * @param Part $part
793 */
794 protected function fetchAttachment(Part $part): void {
795 $oAttachment = new Attachment($this, $part);
796
797 if ($oAttachment->getSize() > 0) {
798 if ($oAttachment->getId() !== null && $this->attachments->offsetExists($oAttachment->getId())) {
799 $this->attachments->put($oAttachment->getId(), $oAttachment);
800 } else {
801 $this->attachments->push($oAttachment);
802 }
803 }
804 }
805
806 /**
807 * Fail proof setter for $fetch_option
808 * @param $option
809 *
810 * @return Message
811 */
812 public function setFetchOption($option): Message {
813 if (is_long($option) === true) {
814 $this->fetch_options = $option;
815 } elseif (is_null($option) === true) {
816 $config = $this->config->get('options.fetch', IMAP::FT_UID);
817 $this->fetch_options = is_long($config) ? $config : 1;
818 }
819
820 return $this;
821 }
822
823 /**
824 * Set the sequence type
825 * @param int|null $sequence
826 *
827 * @return Message
828 */
829 public function setSequence(?int $sequence): Message {
830 if (is_long($sequence)) {
831 $this->sequence = $sequence;
832 } elseif (is_null($sequence)) {
833 $config = $this->config->get('options.sequence', IMAP::ST_MSGN);
834 $this->sequence = is_long($config) ? $config : IMAP::ST_MSGN;
835 }
836
837 return $this;
838 }
839
840 /**
841 * Fail proof setter for $fetch_body
842 * @param $option
843 *
844 * @return Message
845 */
846 public function setFetchBodyOption($option): Message {
847 if (is_bool($option)) {
848 $this->fetch_body = $option;
849 } elseif (is_null($option)) {
850 $config = $this->config->get('options.fetch_body', true);
851 $this->fetch_body = is_bool($config) ? $config : true;
852 }
853
854 return $this;
855 }
856
857 /**
858 * Fail proof setter for $fetch_flags
859 * @param $option
860 *
861 * @return Message
862 */
863 public function setFetchFlagsOption($option): Message {
864 if (is_bool($option)) {
865 $this->fetch_flags = $option;
866 } elseif (is_null($option)) {
867 $config = $this->config->get('options.fetch_flags', true);
868 $this->fetch_flags = is_bool($config) ? $config : true;
869 }
870
871 return $this;
872 }
873
874 /**
875 * Get the messages folder
876 *
877 * @return ?Folder
878 * @throws AuthFailedException
879 * @throws ConnectionFailedException
880 * @throws FolderFetchingException
881 * @throws ImapBadRequestException
882 * @throws ImapServerErrorException
883 * @throws RuntimeException
884 * @throws ResponseException
885 */
886 public function getFolder(): ?Folder {
887 return $this->client->getFolderByPath($this->folder_path);
888 }
889
890 /**
891 * Create a message thread based on the current message
892 * @param Folder|null $sent_folder
893 * @param MessageCollection|null $thread
894 * @param Folder|null $folder
895 *
896 * @return MessageCollection
897 * @throws AuthFailedException
898 * @throws ConnectionFailedException
899 * @throws FolderFetchingException
900 * @throws GetMessagesFailedException
901 * @throws ImapBadRequestException
902 * @throws ImapServerErrorException
903 * @throws RuntimeException
904 * @throws ResponseException
905 */
906 public function thread(?Folder $sent_folder = null, ?MessageCollection &$thread = null, ?Folder $folder = null): MessageCollection {
907 $thread = $thread ?: MessageCollection::make();
908 $folder = $folder ?: $this->getFolder();
909 $sent_folder = $sent_folder ?: $this->client->getFolderByPath($this->config->get("options.common_folders.sent", "INBOX/Sent"));
910
911 /** @var Message $message */
912 foreach ($thread as $message) {
913 if ($message->message_id->first() == $this->message_id->first()) {
914 return $thread;
915 }
916 }
917 $thread->push($this);
918
919 $this->fetchThreadByInReplyTo($thread, $this->message_id, $folder, $folder, $sent_folder);
920 $this->fetchThreadByInReplyTo($thread, $this->message_id, $sent_folder, $folder, $sent_folder);
921
922 foreach ($this->in_reply_to->all() as $in_reply_to) {
923 $this->fetchThreadByMessageId($thread, $in_reply_to, $folder, $folder, $sent_folder);
924 $this->fetchThreadByMessageId($thread, $in_reply_to, $sent_folder, $folder, $sent_folder);
925 }
926
927 return $thread;
928 }
929
930 /**
931 * Fetch a partial thread by message id
932 * @param MessageCollection $thread
933 * @param string $in_reply_to
934 * @param Folder $primary_folder
935 * @param Folder $secondary_folder
936 * @param Folder $sent_folder
937 *
938 * @throws AuthFailedException
939 * @throws ConnectionFailedException
940 * @throws FolderFetchingException
941 * @throws GetMessagesFailedException
942 * @throws ImapBadRequestException
943 * @throws ImapServerErrorException
944 * @throws RuntimeException
945 * @throws ResponseException
946 */
947 protected function fetchThreadByInReplyTo(MessageCollection &$thread, string $in_reply_to, Folder $primary_folder, Folder $secondary_folder, Folder $sent_folder): void {
948 $primary_folder->query()->inReplyTo($in_reply_to)
949 ->setFetchBody($this->getFetchBodyOption())
950 ->leaveUnread()->get()->each(function($message) use (&$thread, $secondary_folder, $sent_folder) {
951 /** @var Message $message */
952 $message->thread($sent_folder, $thread, $secondary_folder);
953 });
954 }
955
956 /**
957 * Fetch a partial thread by message id
958 * @param MessageCollection $thread
959 * @param string $message_id
960 * @param Folder $primary_folder
961 * @param Folder $secondary_folder
962 * @param Folder $sent_folder
963 *
964 * @throws AuthFailedException
965 * @throws ConnectionFailedException
966 * @throws GetMessagesFailedException
967 * @throws FolderFetchingException
968 * @throws ImapBadRequestException
969 * @throws ImapServerErrorException
970 * @throws RuntimeException
971 * @throws ResponseException
972 */
973 protected function fetchThreadByMessageId(MessageCollection &$thread, string $message_id, Folder $primary_folder, Folder $secondary_folder, Folder $sent_folder): void {
974 $primary_folder->query()->messageId($message_id)
975 ->setFetchBody($this->getFetchBodyOption())
976 ->leaveUnread()->get()->each(function($message) use (&$thread, $secondary_folder, $sent_folder) {
977 /** @var Message $message */
978 $message->thread($sent_folder, $thread, $secondary_folder);
979 });
980 }
981
982 /**
983 * Copy the current Messages to a mailbox
984 * @param string $folder_path
985 * @param boolean $expunge
986 * @param bool $utf7
987 *
988 * @return null|Message
989 * @throws AuthFailedException
990 * @throws ConnectionFailedException
991 * @throws EventNotFoundException
992 * @throws FolderFetchingException
993 * @throws ImapBadRequestException
994 * @throws ImapServerErrorException
995 * @throws InvalidMessageDateException
996 * @throws MessageContentFetchingException
997 * @throws MessageFlagException
998 * @throws MessageHeaderFetchingException
999 * @throws MessageNotFoundException
1000 * @throws RuntimeException
1001 * @throws ResponseException
1002 */
1003 public function copy(string $folder_path, bool $expunge = false, bool $utf7 = false): ?Message {
1004 $this->client->openFolder($folder_path);
1005 $status = $this->client->getConnection()->examineFolder($folder_path)->validatedData();
1006
1007 if (isset($status["uidnext"])) {
1008 $next_uid = $status["uidnext"];
1009 if ((int)$next_uid <= 0) {
1010 return null;
1011 }
1012
1013 /** @var Folder $folder */
1014 $folder = $this->client->getFolderByPath($folder_path, $utf7);
1015
1016 $this->client->openFolder($this->folder_path);
1017 if ($this->client->getConnection()->copyMessage($folder->path, $this->getSequenceId(), null, $this->sequence)->validatedData()) {
1018 return $this->fetchNewMail($folder, $next_uid, "copied", $expunge);
1019 }
1020 }
1021
1022 return null;
1023 }
1024
1025 /**
1026 * Move the current Messages to a mailbox
1027 * @param string $folder_path
1028 * @param boolean $expunge
1029 * @param bool $utf7
1030 *
1031 * @return Message|null
1032 * @throws AuthFailedException
1033 * @throws ConnectionFailedException
1034 * @throws EventNotFoundException
1035 * @throws FolderFetchingException
1036 * @throws ImapBadRequestException
1037 * @throws ImapServerErrorException
1038 * @throws InvalidMessageDateException
1039 * @throws MessageContentFetchingException
1040 * @throws MessageFlagException
1041 * @throws MessageHeaderFetchingException
1042 * @throws MessageNotFoundException
1043 * @throws RuntimeException
1044 * @throws ResponseException
1045 */
1046 public function move(string $folder_path, bool $expunge = false, bool $utf7 = false): ?Message {
1047 $this->client->openFolder($folder_path);
1048 $status = $this->client->getConnection()->examineFolder($folder_path)->validatedData();
1049
1050 if (isset($status["uidnext"])) {
1051 $next_uid = $status["uidnext"];
1052 if ((int)$next_uid <= 0) {
1053 return null;
1054 }
1055
1056 /** @var Folder $folder */
1057 $folder = $this->client->getFolderByPath($folder_path, $utf7);
1058
1059 $this->client->openFolder($this->folder_path);
1060 if ($this->client->getConnection()->moveMessage($folder->path, $this->getSequenceId(), null, $this->sequence)->validatedData()) {
1061 return $this->fetchNewMail($folder, $next_uid, "moved", $expunge);
1062 }
1063 }
1064
1065 return null;
1066 }
1067
1068 /**
1069 * Fetch a new message and fire a given event
1070 * @param Folder $folder
1071 * @param int $next_uid
1072 * @param string $event
1073 * @param boolean $expunge
1074 *
1075 * @return Message
1076 * @throws AuthFailedException
1077 * @throws ConnectionFailedException
1078 * @throws EventNotFoundException
1079 * @throws ImapBadRequestException
1080 * @throws ImapServerErrorException
1081 * @throws InvalidMessageDateException
1082 * @throws MessageContentFetchingException
1083 * @throws MessageFlagException
1084 * @throws MessageHeaderFetchingException
1085 * @throws MessageNotFoundException
1086 * @throws RuntimeException
1087 * @throws ResponseException
1088 */
1089 protected function fetchNewMail(Folder $folder, int $next_uid, string $event, bool $expunge): Message {
1090 if ($expunge) $this->client->expunge();
1091
1092 $this->client->openFolder($folder->path);
1093
1094 if ($this->sequence === IMAP::ST_UID) {
1095 $sequence_id = $next_uid;
1096 } else {
1097 $sequence_id = $this->client->getConnection()->getMessageNumber($next_uid)->validatedData();
1098 }
1099
1100 $message = $folder->query()->getMessage($sequence_id, null, $this->sequence);
1101 $this->dispatch("message", $event, $this, $message);
1102
1103 return $message;
1104 }
1105
1106 /**
1107 * Delete the current Message
1108 * @param bool $expunge
1109 * @param string|null $trash_path
1110 * @param boolean $force_move
1111 *
1112 * @return bool
1113 * @throws AuthFailedException
1114 * @throws ConnectionFailedException
1115 * @throws EventNotFoundException
1116 * @throws FolderFetchingException
1117 * @throws ImapBadRequestException
1118 * @throws ImapServerErrorException
1119 * @throws InvalidMessageDateException
1120 * @throws MessageContentFetchingException
1121 * @throws MessageFlagException
1122 * @throws MessageHeaderFetchingException
1123 * @throws MessageNotFoundException
1124 * @throws RuntimeException
1125 * @throws ResponseException
1126 */
1127 public function delete(bool $expunge = true, ?string $trash_path = null, bool $force_move = false): bool {
1128 $status = $this->setFlag("Deleted");
1129 if ($force_move) {
1130 $trash_path = $trash_path === null ? $this->config["common_folders"]["trash"] : $trash_path;
1131 $this->move($trash_path);
1132 }
1133 if ($expunge) $this->client->expunge();
1134
1135 $this->dispatch("message", "deleted", $this);
1136
1137 return $status;
1138 }
1139
1140 /**
1141 * Restore a deleted Message
1142 * @param boolean $expunge
1143 *
1144 * @return bool
1145 * @throws AuthFailedException
1146 * @throws ConnectionFailedException
1147 * @throws EventNotFoundException
1148 * @throws ImapBadRequestException
1149 * @throws ImapServerErrorException
1150 * @throws MessageFlagException
1151 * @throws RuntimeException
1152 * @throws ResponseException
1153 */
1154 public function restore(bool $expunge = true): bool {
1155 $status = $this->unsetFlag("Deleted");
1156 if ($expunge) $this->client->expunge();
1157
1158 $this->dispatch("message", "restored", $this);
1159
1160 return $status;
1161 }
1162
1163 /**
1164 * Set a given flag
1165 * @param array|string $flag
1166 *
1167 * @return bool
1168 * @throws AuthFailedException
1169 * @throws ConnectionFailedException
1170 * @throws EventNotFoundException
1171 * @throws ImapBadRequestException
1172 * @throws ImapServerErrorException
1173 * @throws MessageFlagException
1174 * @throws RuntimeException
1175 * @throws ResponseException
1176 */
1177 public function setFlag(array|string $flag): bool {
1178 $this->client->openFolder($this->folder_path);
1179 $flag = "\\" . trim(is_array($flag) ? implode(" \\", $flag) : $flag);
1180 $sequence_id = $this->getSequenceId();
1181 try {
1182 $status = $this->client->getConnection()->store([$flag], $sequence_id, $sequence_id, "+", true, $this->sequence)->validatedData();
1183 } catch (Exceptions\RuntimeException $e) {
1184 throw new MessageFlagException("flag could not be set", 0, $e);
1185 }
1186 $this->parseFlags();
1187
1188 $this->dispatch("flag", "new", $this, $flag);
1189
1190 return (bool)$status;
1191 }
1192
1193 /**
1194 * Unset a given flag
1195 * @param array|string $flag
1196 *
1197 * @return bool
1198 * @throws AuthFailedException
1199 * @throws ConnectionFailedException
1200 * @throws EventNotFoundException
1201 * @throws ImapBadRequestException
1202 * @throws ImapServerErrorException
1203 * @throws MessageFlagException
1204 * @throws RuntimeException
1205 * @throws ResponseException
1206 */
1207 public function unsetFlag(array|string $flag): bool {
1208 $this->client->openFolder($this->folder_path);
1209
1210 $flag = "\\" . trim(is_array($flag) ? implode(" \\", $flag) : $flag);
1211 $sequence_id = $this->getSequenceId();
1212 try {
1213 $status = $this->client->getConnection()->store([$flag], $sequence_id, $sequence_id, "-", true, $this->sequence)->validatedData();
1214 } catch (Exceptions\RuntimeException $e) {
1215 throw new MessageFlagException("flag could not be removed", 0, $e);
1216 }
1217 $this->parseFlags();
1218
1219 $this->dispatch("flag", "deleted", $this, $flag);
1220
1221 return (bool)$status;
1222 }
1223
1224 /**
1225 * Set a given flag
1226 * @param array|string $flag
1227 *
1228 * @return bool
1229 * @throws AuthFailedException
1230 * @throws ConnectionFailedException
1231 * @throws EventNotFoundException
1232 * @throws ImapBadRequestException
1233 * @throws ImapServerErrorException
1234 * @throws MessageFlagException
1235 * @throws RuntimeException
1236 * @throws ResponseException
1237 */
1238 public function addFlag(array|string $flag): bool {
1239 return $this->setFlag($flag);
1240 }
1241
1242 /**
1243 * Unset a given flag
1244 * @param array|string $flag
1245 *
1246 * @return bool
1247 * @throws AuthFailedException
1248 * @throws ConnectionFailedException
1249 * @throws EventNotFoundException
1250 * @throws ImapBadRequestException
1251 * @throws ImapServerErrorException
1252 * @throws MessageFlagException
1253 * @throws RuntimeException
1254 * @throws ResponseException
1255 */
1256 public function removeFlag(array|string $flag): bool {
1257 return $this->unsetFlag($flag);
1258 }
1259
1260 /**
1261 * Get all message attachments.
1262 *
1263 * @return AttachmentCollection
1264 */
1265 public function getAttachments(): AttachmentCollection {
1266 return $this->attachments;
1267 }
1268
1269 /**
1270 * Get all message attachments.
1271 *
1272 * @return AttachmentCollection
1273 */
1274 public function attachments(): AttachmentCollection {
1275 return $this->getAttachments();
1276 }
1277
1278 /**
1279 * Checks if there are any attachments present
1280 *
1281 * @return boolean
1282 */
1283 public function hasAttachments(): bool {
1284 return $this->attachments->isEmpty() === false;
1285 }
1286
1287 /**
1288 * Get the raw body
1289 *
1290 * @return string
1291 */
1292 public function getRawBody(): string {
1293 if ($this->raw_body === "") {
1294 $this->raw_body = $this->structure->raw;
1295 }
1296
1297 return $this->raw_body;
1298 }
1299
1300 /**
1301 * Get the message header
1302 *
1303 * @return ?Header
1304 */
1305 public function getHeader(): ?Header {
1306 return $this->header;
1307 }
1308
1309 /**
1310 * Get the current client
1311 *
1312 * @return ?Client
1313 */
1314 public function getClient(): ?Client {
1315 return $this->client;
1316 }
1317
1318 /**
1319 * Get the used fetch option
1320 *
1321 * @return ?integer
1322 */
1323 public function getFetchOptions(): ?int {
1324 return $this->fetch_options;
1325 }
1326
1327 /**
1328 * Get the used fetch body option
1329 *
1330 * @return boolean
1331 */
1332 public function getFetchBodyOption(): bool {
1333 return $this->fetch_body;
1334 }
1335
1336 /**
1337 * Get the used fetch flags option
1338 *
1339 * @return boolean
1340 */
1341 public function getFetchFlagsOption(): bool {
1342 return $this->fetch_flags;
1343 }
1344
1345 /**
1346 * Get all available bodies
1347 *
1348 * @return array
1349 */
1350 public function getBodies(): array {
1351 return $this->bodies;
1352 }
1353
1354 /**
1355 * Get all set flags
1356 *
1357 * @return FlagCollection
1358 */
1359 public function getFlags(): FlagCollection {
1360 return $this->flags;
1361 }
1362
1363 /**
1364 * Get all set flags
1365 *
1366 * @return FlagCollection
1367 */
1368 public function flags(): FlagCollection {
1369 return $this->getFlags();
1370 }
1371
1372 /**
1373 * Check if a flag is set
1374 *
1375 * @param string $flag
1376 * @return boolean
1377 */
1378 public function hasFlag(string $flag): bool {
1379 $flag = str_replace("\\", "", strtolower($flag));
1380 return $this->getFlags()->has($flag);
1381 }
1382
1383 /**
1384 * Get the fetched structure
1385 *
1386 * @return Structure|null
1387 */
1388 public function getStructure(): ?Structure {
1389 return $this->structure;
1390 }
1391
1392 /**
1393 * Check if a message matches another by comparing basic attributes
1394 *
1395 * @param null|Message $message
1396 * @return boolean
1397 */
1398 public function is(?Message $message = null): bool {
1399 if (is_null($message)) {
1400 return false;
1401 }
1402
1403 return $this->uid == $message->uid
1404 && $this->message_id->first() == $message->message_id->first()
1405 && $this->subject->first() == $message->subject->first()
1406 && $this->date->toDate()->eq($message->date->toDate());
1407 }
1408
1409 /**
1410 * Get all message attributes
1411 *
1412 * @return array
1413 */
1414 public function getAttributes(): array {
1415 return array_merge($this->attributes, $this->header->getAttributes());
1416 }
1417
1418 /**
1419 * Set the message mask
1420 * @param $mask
1421 *
1422 * @return Message
1423 */
1424 public function setMask($mask): Message {
1425 if (class_exists($mask)) {
1426 $this->mask = $mask;
1427 }
1428
1429 return $this;
1430 }
1431
1432 /**
1433 * Get the used message mask
1434 *
1435 * @return string
1436 */
1437 public function getMask(): string {
1438 return $this->mask;
1439 }
1440
1441 /**
1442 * Get a masked instance by providing a mask name
1443 * @param mixed|null $mask
1444 *
1445 * @return mixed
1446 * @throws MaskNotFoundException
1447 */
1448 public function mask(mixed $mask = null): mixed {
1449 $mask = $mask !== null ? $mask : $this->mask;
1450 if (class_exists($mask)) {
1451 return new $mask($this);
1452 }
1453
1454 throw new MaskNotFoundException("Unknown mask provided: " . $mask);
1455 }
1456
1457 /**
1458 * Get the message path aka folder path
1459 *
1460 * @return string
1461 */
1462 public function getFolderPath(): string {
1463 return $this->folder_path;
1464 }
1465
1466 /**
1467 * Set the message path aka folder path
1468 * @param $folder_path
1469 *
1470 * @return Message
1471 */
1472 public function setFolderPath($folder_path): Message {
1473 $this->folder_path = $folder_path;
1474
1475 return $this;
1476 }
1477
1478 /**
1479 * Set the config
1480 * @param Config $config
1481 *
1482 * @return Message
1483 */
1484 public function setConfig(Config $config): Message {
1485 $this->config = $config;
1486
1487 return $this;
1488 }
1489
1490 /**
1491 * Get the config
1492 *
1493 * @return Config
1494 */
1495 public function getConfig(): Config {
1496 return $this->config;
1497 }
1498
1499 /**
1500 * Set the options
1501 * @param array $options
1502 *
1503 * @return Message
1504 */
1505 public function setOptions(array $options): Message {
1506 $this->options = $options;
1507
1508 return $this;
1509 }
1510
1511 /**
1512 * Get the options
1513 *
1514 * @return array
1515 */
1516 public function getOptions(): array {
1517 return $this->options;
1518 }
1519
1520 /**
1521 * Set the available flags
1522 * @param $available_flags
1523 *
1524 * @return Message
1525 */
1526 public function setAvailableFlags($available_flags): Message {
1527 $this->available_flags = $available_flags;
1528
1529 return $this;
1530 }
1531
1532 /**
1533 * Get the available flags
1534 *
1535 * @return array
1536 */
1537 public function getAvailableFlags(): array {
1538 return $this->available_flags;
1539 }
1540
1541 /**
1542 * Set the attachment collection
1543 * @param $attachments
1544 *
1545 * @return Message
1546 */
1547 public function setAttachments($attachments): Message {
1548 $this->attachments = $attachments;
1549
1550 return $this;
1551 }
1552
1553 /**
1554 * Set the flag collection
1555 * @param $flags
1556 *
1557 * @return Message
1558 */
1559 public function setFlags($flags): Message {
1560 $this->flags = $flags;
1561
1562 return $this;
1563 }
1564
1565 /**
1566 * Set the client
1567 * @param $client
1568 *
1569 * @return Message
1570 * @throws AuthFailedException
1571 * @throws ConnectionFailedException
1572 * @throws ImapBadRequestException
1573 * @throws ImapServerErrorException
1574 * @throws RuntimeException
1575 * @throws ResponseException
1576 */
1577 public function setClient($client): Message {
1578 $this->client = $client;
1579 $this->client?->openFolder($this->folder_path);
1580
1581 return $this;
1582 }
1583
1584 /**
1585 * Set the message number
1586 * @param int $uid
1587 *
1588 * @return Message
1589 */
1590 public function setUid(int $uid): Message {
1591 $this->uid = $uid;
1592 $this->msgn = null;
1593 $this->msglist = null;
1594
1595 return $this;
1596 }
1597
1598 /**
1599 * Set the message number
1600 * @param int $msgn
1601 * @param int|null $msglist
1602 *
1603 * @return Message
1604 */
1605 public function setMsgn(int $msgn, ?int $msglist = null): Message {
1606 $this->msgn = $msgn;
1607 $this->msglist = $msglist;
1608 $this->uid = null;
1609
1610 return $this;
1611 }
1612
1613 /**
1614 * Get the current sequence type
1615 *
1616 * @return int
1617 */
1618 public function getSequence(): int {
1619 return $this->sequence;
1620 }
1621
1622 /**
1623 * Get the current sequence id (either a UID or a message number!)
1624 *
1625 * @return int
1626 */
1627 public function getSequenceId(): int {
1628 return $this->sequence === IMAP::ST_UID ? $this->uid : $this->msgn;
1629 }
1630
1631 /**
1632 * Set the sequence id
1633 * @param $uid
1634 * @param int|null $msglist
1635 */
1636 public function setSequenceId($uid, ?int $msglist = null): void {
1637 if ($this->getSequence() === IMAP::ST_UID) {
1638 $this->setUid($uid);
1639 $this->setMsglist($msglist);
1640 } else {
1641 $this->setMsgn($uid, $msglist);
1642 }
1643 }
1644
1645 /**
1646 * Get the decoder instance
1647 *
1648 * @return DecoderInterface
1649 */
1650 public function getDecoder(): DecoderInterface {
1651 return $this->decoder;
1652 }
1653
1654 /**
1655 * Set the decoder instance
1656 * @param DecoderInterface $decoder
1657 *
1658 * @return $this
1659 */
1660 public function setDecoder(DecoderInterface $decoder): static {
1661 $this->decoder = $decoder;
1662 return $this;
1663 }
1664
1665 /**
1666 * Safe the entire message in a file
1667 * @param $filename
1668 *
1669 * @return bool|int
1670 */
1671 public function save($filename): bool|int {
1672 return file_put_contents($filename, $this->header->raw."\r\n\r\n".$this->structure->raw);
1673 }
1674 }