MessagePart.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. private $message;
  21. public function __construct(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. $this->message = $message;
  30. }
  31. public function getMediaType(): string
  32. {
  33. return 'message';
  34. }
  35. public function getMediaSubtype(): string
  36. {
  37. return 'rfc822';
  38. }
  39. public function getBody(): string
  40. {
  41. return $this->message->toString();
  42. }
  43. public function bodyToString(): string
  44. {
  45. return $this->getBody();
  46. }
  47. public function bodyToIterable(): iterable
  48. {
  49. return $this->message->toIterable();
  50. }
  51. /**
  52. * @return array
  53. */
  54. public function __sleep()
  55. {
  56. return ['message'];
  57. }
  58. public function __wakeup()
  59. {
  60. $this->__construct($this->message);
  61. }
  62. }