<?php /* * This file is part of the Symfony package. * * (c) Fabien Potencier <fabien@symfony.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Mime; use Symfony\Component\Mime\Exception\RuntimeException; use Symfony\Component\Mime\Part\DataPart; use Symfony\Component\Mime\Part\Multipart\AlternativePart; use Symfony\Component\Mime\Part\Multipart\MixedPart; use Symfony\Component\Mime\Part\Multipart\RelatedPart; use Symfony\Component\Mime\Part\TextPart; /** * @author Fabien Potencier <fabien@symfony.com> */ final class MessageConverter { /** * @throws RuntimeException when unable to convert the message to an email */ public static function toEmail(Message $message): Email { if ($message instanceof Email) { return $message; } // try to convert to a "simple" Email instance $body = $message->getBody(); if ($body instanceof TextPart) { return self::createEmailFromTextPart($message, $body); } if ($body instanceof AlternativePart) { return self::createEmailFromAlternativePart($message, $body); } if ($body instanceof RelatedPart) { return self::createEmailFromRelatedPart($message, $body); } if ($body instanceof MixedPart) { $parts = $body->getParts(); if ($parts[0] instanceof RelatedPart) { $email = self::createEmailFromRelatedPart($message, $parts[0]); } elseif ($parts[0] instanceof AlternativePart) { $email = self::createEmailFromAlternativePart($message, $parts[0]); } elseif ($parts[0] instanceof TextPart) { $email = self::createEmailFromTextPart($message, $parts[0]); } else { throw new RuntimeException(sprintf('Unable to create an Email from an instance of "%s" as the body is too complex.', get_debug_type($message))); } return self::addParts($email, \array_slice($parts, 1)); } throw new RuntimeException(sprintf('Unable to create an Email from an instance of "%s" as the body is too complex.', get_debug_type($message))); } private static function createEmailFromTextPart(Message $message, TextPart $part): Email { if ('text' === $part->getMediaType() && 'plain' === $part->getMediaSubtype()) { return (new Email(clone $message->getHeaders()))->text($part->getBody(), $part->getPreparedHeaders()->getHeaderParameter('Content-Type', 'charset') ?: 'utf-8'); } if ('text' === $part->getMediaType() && 'html' === $part->getMediaSubtype()) { return (new Email(clone $message->getHeaders()))->html($part->getBody(), $part->getPreparedHeaders()->getHeaderParameter('Content-Type', 'charset') ?: 'utf-8'); } throw new RuntimeException(sprintf('Unable to create an Email from an instance of "%s" as the body is too complex.', get_debug_type($message))); } private static function createEmailFromAlternativePart(Message $message, AlternativePart $part): Email { $parts = $part->getParts(); if ( 2 === \count($parts) && $parts[0] instanceof TextPart && 'text' === $parts[0]->getMediaType() && 'plain' === $parts[0]->getMediaSubtype() && $parts[1] instanceof TextPart && 'text' === $parts[1]->getMediaType() && 'html' === $parts[1]->getMediaSubtype() ) { return (new Email(clone $message->getHeaders())) ->text($parts[0]->getBody(), $parts[0]->getPreparedHeaders()->getHeaderParameter('Content-Type', 'charset') ?: 'utf-8') ->html($parts[1]->getBody(), $parts[1]->getPreparedHeaders()->getHeaderParameter('Content-Type', 'charset') ?: 'utf-8') ; } throw new RuntimeException(sprintf('Unable to create an Email from an instance of "%s" as the body is too complex.', get_debug_type($message))); } private static function createEmailFromRelatedPart(Message $message, RelatedPart $part): Email { $parts = $part->getParts(); if ($parts[0] instanceof AlternativePart) { $email = self::createEmailFromAlternativePart($message, $parts[0]); } elseif ($parts[0] instanceof TextPart) { $email = self::createEmailFromTextPart($message, $parts[0]); } else { throw new RuntimeException(sprintf('Unable to create an Email from an instance of "%s" as the body is too complex.', get_debug_type($message))); } return self::addParts($email, \array_slice($parts, 1)); } private static function addParts(Email $email, array $parts): Email { foreach ($parts as $part) { if (!$part instanceof DataPart) { throw new RuntimeException(sprintf('Unable to create an Email from an instance of "%s" as the body is too complex.', get_debug_type($email))); } $email->addPart($part); } return $email; } }
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
.git | Folder | 0755 |
|
|
Crypto | Folder | 0755 |
|
|
DependencyInjection | Folder | 0755 |
|
|
Encoder | Folder | 0755 |
|
|
Exception | Folder | 0755 |
|
|
Header | Folder | 0755 |
|
|
HtmlToTextConverter | Folder | 0755 |
|
|
Part | Folder | 0755 |
|
|
Resources | Folder | 0755 |
|
|
Test | Folder | 0755 |
|
|
Tests | Folder | 0755 |
|
|
.gitattributes | File | 113 B | 0644 |
|
.gitignore | File | 37 B | 0644 |
|
Address.php | File | 3.75 KB | 0644 |
|
BodyRendererInterface.php | File | 438 B | 0644 |
|
CHANGELOG.md | File | 851 B | 0644 |
|
CharacterStream.php | File | 9.36 KB | 0644 |
|
DraftEmail.php | File | 1.14 KB | 0644 |
|
Email.php | File | 15.52 KB | 0644 |
|
FileBinaryMimeTypeGuesser.php | File | 2.49 KB | 0644 |
|
FileinfoMimeTypeGuesser.php | File | 1.84 KB | 0644 |
|
LICENSE | File | 1.06 KB | 0644 |
|
Message.php | File | 4.51 KB | 0644 |
|
MessageConverter.php | File | 5.15 KB | 0644 |
|
MimeTypeGuesserInterface.php | File | 859 B | 0644 |
|
MimeTypes.php | File | 179.96 KB | 0644 |
|
MimeTypesInterface.php | File | 809 B | 0644 |
|
README.md | File | 472 B | 0644 |
|
RawMessage.php | File | 1.55 KB | 0644 |
|
composer.json | File | 1.4 KB | 0644 |
|
phpunit.xml.dist | File | 893 B | 0644 |
|