Dashboard sipadu mbip
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

MessageConverter.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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;
  11. use Symfony\Component\Mime\Exception\RuntimeException;
  12. use Symfony\Component\Mime\Part\DataPart;
  13. use Symfony\Component\Mime\Part\Multipart\AlternativePart;
  14. use Symfony\Component\Mime\Part\Multipart\MixedPart;
  15. use Symfony\Component\Mime\Part\Multipart\RelatedPart;
  16. use Symfony\Component\Mime\Part\TextPart;
  17. /**
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. *
  20. * @experimental in 4.3
  21. */
  22. final class MessageConverter
  23. {
  24. /**
  25. * @throws RuntimeException when unable to convert the message to an email
  26. */
  27. public static function toEmail(Message $message): Email
  28. {
  29. if ($message instanceof Email) {
  30. return $message;
  31. }
  32. // try to convert to a "simple" Email instance
  33. $body = $message->getBody();
  34. if ($body instanceof TextPart) {
  35. return self::createEmailFromTextPart($message, $body);
  36. }
  37. if ($body instanceof AlternativePart) {
  38. return self::createEmailFromAlternativePart($message, $body);
  39. }
  40. if ($body instanceof RelatedPart) {
  41. return self::createEmailFromRelatedPart($message, $body);
  42. }
  43. if ($body instanceof MixedPart) {
  44. $parts = $body->getParts();
  45. if ($parts[0] instanceof RelatedPart) {
  46. $email = self::createEmailFromRelatedPart($message, $parts[0]);
  47. } elseif ($parts[0] instanceof AlternativePart) {
  48. $email = self::createEmailFromAlternativePart($message, $parts[0]);
  49. } elseif ($parts[0] instanceof TextPart) {
  50. $email = self::createEmailFromTextPart($message, $parts[0]);
  51. } else {
  52. throw new RuntimeException(sprintf('Unable to create an Email from an instance of "%s" as the body is too complex.', \get_class($message)));
  53. }
  54. return self::attachParts($email, \array_slice($parts, 1));
  55. }
  56. throw new RuntimeException(sprintf('Unable to create an Email from an instance of "%s" as the body is too complex.', \get_class($message)));
  57. }
  58. private static function createEmailFromTextPart(Message $message, TextPart $part): Email
  59. {
  60. if ('text' === $part->getMediaType() && 'plain' === $part->getMediaSubtype()) {
  61. return (new Email(clone $message->getHeaders()))->text($part->getBody(), $part->getPreparedHeaders()->getHeaderParameter('Content-Type', 'charset') ?: 'utf-8');
  62. }
  63. if ('text' === $part->getMediaType() && 'html' === $part->getMediaSubtype()) {
  64. return (new Email(clone $message->getHeaders()))->html($part->getBody(), $part->getPreparedHeaders()->getHeaderParameter('Content-Type', 'charset') ?: 'utf-8');
  65. }
  66. throw new RuntimeException(sprintf('Unable to create an Email from an instance of "%s" as the body is too complex.', \get_class($message)));
  67. }
  68. private static function createEmailFromAlternativePart(Message $message, AlternativePart $part): Email
  69. {
  70. $parts = $part->getParts();
  71. if (
  72. 2 === \count($parts) &&
  73. $parts[0] instanceof TextPart && 'text' === $parts[0]->getMediaType() && 'plain' === $parts[0]->getMediaSubtype() &&
  74. $parts[1] instanceof TextPart && 'text' === $parts[1]->getMediaType() && 'html' === $parts[1]->getMediaSubtype()
  75. ) {
  76. return (new Email(clone $message->getHeaders()))
  77. ->text($parts[0]->getBody(), $parts[0]->getPreparedHeaders()->getHeaderParameter('Content-Type', 'charset') ?: 'utf-8')
  78. ->html($parts[1]->getBody(), $parts[1]->getPreparedHeaders()->getHeaderParameter('Content-Type', 'charset') ?: 'utf-8')
  79. ;
  80. }
  81. throw new RuntimeException(sprintf('Unable to create an Email from an instance of "%s" as the body is too complex.', \get_class($message)));
  82. }
  83. private static function createEmailFromRelatedPart(Message $message, RelatedPart $part): Email
  84. {
  85. $parts = $part->getParts();
  86. if ($parts[0] instanceof AlternativePart) {
  87. $email = self::createEmailFromAlternativePart($message, $parts[0]);
  88. } elseif ($parts[0] instanceof TextPart) {
  89. $email = self::createEmailFromTextPart($message, $parts[0]);
  90. } else {
  91. throw new RuntimeException(sprintf('Unable to create an Email from an instance of "%s" as the body is too complex.', \get_class($message)));
  92. }
  93. return self::attachParts($email, \array_slice($parts, 1));
  94. }
  95. private static function attachParts(Email $email, array $parts): Email
  96. {
  97. foreach ($parts as $part) {
  98. if (!$part instanceof DataPart) {
  99. throw new RuntimeException(sprintf('Unable to create an Email from an instance of "%s" as the body is too complex.', \get_class($email)));
  100. }
  101. $headers = $part->getPreparedHeaders();
  102. $method = 'inline' === $headers->getHeaderBody('Content-Disposition') ? 'embed' : 'attach';
  103. $name = $headers->getHeaderParameter('Content-Disposition', 'filename');
  104. $email->$method($part->getBody(), $name, $part->getMediaType().'/'.$part->getMediaSubtype());
  105. }
  106. return $email;
  107. }
  108. }