31 if ($message instanceof
Email) {
38 return self::createEmailFromTextPart($message, $body);
42 return self::createEmailFromAlternativePart($message, $body);
46 return self::createEmailFromRelatedPart($message, $body);
50 $parts = $body->getParts();
52 $email = self::createEmailFromRelatedPart($message, $parts[0]);
54 $email = self::createEmailFromAlternativePart($message, $parts[0]);
55 } elseif ($parts[0] instanceof
TextPart) {
56 $email = self::createEmailFromTextPart($message, $parts[0]);
58 throw new RuntimeException(sprintf(
'Unable to create an Email from an instance of "%s" as the body is too complex.', \get_class($message)));
61 return self::attachParts($email, \array_slice($parts, 1));
64 throw new RuntimeException(sprintf(
'Unable to create an Email from an instance of "%s" as the body is too complex.', \get_class($message)));
76 throw new RuntimeException(sprintf(
'Unable to create an Email from an instance of "%s" as the body is too complex.', \get_class($message)));
79 private static function createEmailFromAlternativePart(Message $message, AlternativePart $part): Email
81 $parts = $part->getParts();
83 2 === \count($parts) &&
84 $parts[0] instanceof TextPart &&
'text' === $parts[0]->getMediaType() &&
'plain' === $parts[0]->getMediaSubtype() &&
85 $parts[1] instanceof TextPart &&
'text' === $parts[1]->getMediaType() &&
'html' === $parts[1]->getMediaSubtype()
87 return (
new Email(clone $message->getHeaders()))
88 ->text($parts[0]->getBody(), $parts[0]->getPreparedHeaders()->getHeaderParameter(
'Content-Type',
'charset') ?:
'utf-8')
89 ->html($parts[1]->getBody(), $parts[1]->getPreparedHeaders()->getHeaderParameter(
'Content-Type',
'charset') ?:
'utf-8')
93 throw new RuntimeException(sprintf(
'Unable to create an Email from an instance of "%s" as the body is too complex.', \get_class($message)));
96 private static function createEmailFromRelatedPart(Message $message, RelatedPart $part): Email
98 $parts = $part->getParts();
99 if ($parts[0] instanceof AlternativePart) {
100 $email = self::createEmailFromAlternativePart($message, $parts[0]);
101 } elseif ($parts[0] instanceof TextPart) {
102 $email = self::createEmailFromTextPart($message, $parts[0]);
104 throw new RuntimeException(sprintf(
'Unable to create an Email from an instance of "%s" as the body is too complex.', \get_class($message)));
107 return self::attachParts($email, \array_slice($parts, 1));
110 private static function attachParts(Email $email, array $parts): Email
112 foreach ($parts as $part) {
113 if (!$part instanceof DataPart) {
114 throw new RuntimeException(sprintf(
'Unable to create an Email from an instance of "%s" as the body is too complex.', \get_class($email)));
117 $headers = $part->getPreparedHeaders();
118 $method =
'inline' === $headers->getHeaderBody(
'Content-Disposition') ?
'embed' :
'attach';
119 $name = $headers->getHeaderParameter(
'Content-Disposition',
'filename');
120 $email->$method($part->getBody(), $name, $part->getMediaType().
'/'.$part->getMediaSubtype());