MessagePart.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Mime\Part;
  11. use Symfony\Component\Mime\Message;
  12. use Symfony\Component\Mime\RawMessage;
  13. /**
  14. * @final
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class MessagePart extends DataPart
  19. {
  20. public function __construct(
  21. private RawMessage $message,
  22. ) {
  23. if ($message instanceof Message) {
  24. $name = $message->getHeaders()->getHeaderBody('Subject').'.eml';
  25. } else {
  26. $name = 'email.eml';
  27. }
  28. parent::__construct('', $name);
  29. }
  30. public function getMediaType(): string
  31. {
  32. return 'message';
  33. }
  34. public function getMediaSubtype(): string
  35. {
  36. return 'rfc822';
  37. }
  38. public function getBody(): string
  39. {
  40. return $this->message->toString();
  41. }
  42. public function bodyToString(): string
  43. {
  44. return $this->getBody();
  45. }
  46. public function bodyToIterable(): iterable
  47. {
  48. return $this->message->toIterable();
  49. }
  50. public function __sleep(): array
  51. {
  52. return ['message'];
  53. }
  54. public function __wakeup(): void
  55. {
  56. $this->__construct($this->message);
  57. }
  58. }