PluginProbe
WPIDE – File Manager & Code Editor / 3.5.9
WPIDE – File Manager & Code Editor v3.5.9
3.5.9 3.5.8 3.5.7 2.0.14 2.0.15 2.0.16 2.0.2 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1 2.2 2.3 2.3.1 2.3.2 2.4.0 2.5 2.6 3.0 3.1 3.2 3.3 All 55 releases
wpide / vendor / symfony / mime / MessageConverter.php

MessageConverter.php in WPIDE – File Manager & Code Editor 3.5.9, at vendor/symfony/mime/MessageConverter.php

126 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Mime;
13
14 use Symfony\Component\Mime\Exception\RuntimeException;
15 use Symfony\Component\Mime\Part\DataPart;
16 use Symfony\Component\Mime\Part\Multipart\AlternativePart;
17 use Symfony\Component\Mime\Part\Multipart\MixedPart;
18 use Symfony\Component\Mime\Part\Multipart\RelatedPart;
19 use Symfony\Component\Mime\Part\TextPart;
20
21 /**
22 * @author Fabien Potencier <fabien@symfony.com>
23 */
24 final class MessageConverter
25 {
26 /**
27 * @throws RuntimeException when unable to convert the message to an email
28 */
29 public static function toEmail(Message $message): Email
30 {
31 if ($message instanceof Email) {
32 return $message;
33 }
34
35 // try to convert to a "simple" Email instance
36 $body = $message->getBody();
37 if ($body instanceof TextPart) {
38 return self::createEmailFromTextPart($message, $body);
39 }
40
41 if ($body instanceof AlternativePart) {
42 return self::createEmailFromAlternativePart($message, $body);
43 }
44
45 if ($body instanceof RelatedPart) {
46 return self::createEmailFromRelatedPart($message, $body);
47 }
48
49 if ($body instanceof MixedPart) {
50 $parts = $body->getParts();
51 if ($parts[0] instanceof RelatedPart) {
52 $email = self::createEmailFromRelatedPart($message, $parts[0]);
53 } elseif ($parts[0] instanceof AlternativePart) {
54 $email = self::createEmailFromAlternativePart($message, $parts[0]);
55 } elseif ($parts[0] instanceof TextPart) {
56 $email = self::createEmailFromTextPart($message, $parts[0]);
57 } else {
58 throw new RuntimeException(sprintf('Unable to create an Email from an instance of "%s" as the body is too complex.', get_debug_type($message)));
59 }
60
61 return self::attachParts($email, \array_slice($parts, 1));
62 }
63
64 throw new RuntimeException(sprintf('Unable to create an Email from an instance of "%s" as the body is too complex.', get_debug_type($message)));
65 }
66
67 private static function createEmailFromTextPart(Message $message, TextPart $part): Email
68 {
69 if ('text' === $part->getMediaType() && 'plain' === $part->getMediaSubtype()) {
70 return (new Email(clone $message->getHeaders()))->text($part->getBody(), $part->getPreparedHeaders()->getHeaderParameter('Content-Type', 'charset') ?: 'utf-8');
71 }
72 if ('text' === $part->getMediaType() && 'html' === $part->getMediaSubtype()) {
73 return (new Email(clone $message->getHeaders()))->html($part->getBody(), $part->getPreparedHeaders()->getHeaderParameter('Content-Type', 'charset') ?: 'utf-8');
74 }
75
76 throw new RuntimeException(sprintf('Unable to create an Email from an instance of "%s" as the body is too complex.', get_debug_type($message)));
77 }
78
79 private static function createEmailFromAlternativePart(Message $message, AlternativePart $part): Email
80 {
81 $parts = $part->getParts();
82 if (
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()
86 ) {
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')
90 ;
91 }
92
93 throw new RuntimeException(sprintf('Unable to create an Email from an instance of "%s" as the body is too complex.', get_debug_type($message)));
94 }
95
96 private static function createEmailFromRelatedPart(Message $message, RelatedPart $part): Email
97 {
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]);
103 } else {
104 throw new RuntimeException(sprintf('Unable to create an Email from an instance of "%s" as the body is too complex.', get_debug_type($message)));
105 }
106
107 return self::attachParts($email, \array_slice($parts, 1));
108 }
109
110 private static function attachParts(Email $email, array $parts): Email
111 {
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_debug_type($email)));
115 }
116
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());
121 }
122
123 return $email;
124 }
125 }
126