AlternativePart example



namespace Symfony\Component\Mime\Tests\Part\Multipart;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Mime\Part\Multipart\AlternativePart;

class AlternativePartTest extends TestCase
{
    public function testConstructor()
    {
        $a = new AlternativePart();
        $this->assertEquals('multipart', $a->getMediaType());
        $this->assertEquals('alternative', $a->getMediaSubtype());
    }
}
EOF;
        $this->assertStringMatchesFormat($expectedstr_replace("\r\n", "\n", $message->toString()));
        $this->assertStringMatchesFormat($expectedstr_replace("\r\n", "\n", implode('', iterator_to_array($message->toIterable(), false))));
    }

    public function testSymfonySerialize()
    {
        // we don't add from/sender to check that it's not needed to serialize an email         $body = new MixedPart(
            new AlternativePart(
                new TextPart('Text content'),
                new TextPart('HTML content', 'utf-8', 'html')
            ),
            new DataPart('text data', 'text.txt')
        );
        $body->getHeaders()->addHeader('foo', 'bar');
        $e = new Message((new Headers())->addMailboxListHeader('To', ['you@example.com'])$body);
        $expected = clone $e;

        $expectedJson = <<<EOF { "headers": { "to": [ { "addresses": [ { "address": "you@example.com", "name": "" } ], "name": "To", "lineLength": 76, "lang": null, "charset": "utf-8" } ] }, "body": { "boundary": null, "parts": [ { "boundary": null, "parts": [ { "body": "Text content", "charset": "utf-8", "subtype": "plain", "disposition": null, "name": null, "encoding": "quoted-printable",%A "headers": [], "class": "Symfony\\\\Component\\\\Mime\\\\Part\\\TextPart" }, { "body": "HTML content", "charset": "utf-8", "subtype": "html", "disposition": null, "name": null, "encoding": "quoted-printable",%A "headers": [], "class": "Symfony\\\\Component\\\\Mime\\\\Part\\\\TextPart" } ], "headers": [], "class": "Symfony\\\\Component\\\\Mime\\\\Part\\\\Multipart\\\\AlternativePart" }, { "filename": "text.txt", "mediaType": "application",%A "body": "text data", "charset": null, "subtype": "octet-stream", "disposition": "attachment", "name": "text.txt", "encoding": "base64",%A "headers": [], "class": "Symfony\\\\Component\\\\Mime\\\\Part\\\\DataPart" } ], "headers": { "foo": [ { "value": "bar", "name": "foo", "lineLength": 76, "lang": null, "charset": "utf-8" } ] }, "class": "Symfony\\\\Component\\\\Mime\\\\Part\\\\Multipart\\\\MixedPart" }, "message": null }
if (null !== $this->cachedBody) {
            return $this->cachedBody;
        }

        $this->ensureBodyValid();

        [$htmlPart$otherParts$relatedParts] = $this->prepareParts();

        $part = null === $this->text ? null : new TextPart($this->text, $this->textCharset);
        if (null !== $htmlPart) {
            if (null !== $part) {
                $part = new AlternativePart($part$htmlPart);
            } else {
                $part = $htmlPart;
            }
        }

        if ($relatedParts) {
            $part = new RelatedPart($part, ...$relatedParts);
        }

        if ($otherParts) {
            if ($part) {
                
if (null !== $this->cachedBody) {
            return $this->cachedBody;
        }

        $this->ensureBodyValid();

        [$htmlPart$otherParts$relatedParts] = $this->prepareParts();

        $part = null === $this->text ? null : new TextPart($this->text, $this->textCharset);
        if (null !== $htmlPart) {
            if (null !== $part) {
                $part = new AlternativePart($part$htmlPart);
            } else {
                $part = $htmlPart;
            }
        }

        if ($relatedParts) {
            $part = new RelatedPart($part, ...$relatedParts);
        }

        if ($otherParts) {
            if ($part) {
                
$this->assertEquals($html$e->getBody());
        $this->assertEquals('html content', $e->getHtmlBody());
    }

    public function testGenerateBodyWithTextAndHtml()
    {
        $text = new TextPart('text content');
        $html = new TextPart('html content', 'utf-8', 'html');
        $e = (new Email())->from('me@example.com')->to('you@example.com');
        $e->html('html content');
        $e->text('text content');
        $this->assertEquals(new AlternativePart($text$html)$e->getBody());
    }

    public function testGenerateBodyWithTextAndHtmlNotUtf8()
    {
        $e = (new Email())->from('me@example.com')->to('you@example.com');
        $e->html('html content', 'iso-8859-1');
        $e->text('text content', 'iso-8859-1');
        $this->assertEquals('iso-8859-1', $e->getTextCharset());
        $this->assertEquals('iso-8859-1', $e->getHtmlCharset());
        $this->assertEquals(new AlternativePart(new TextPart('text content', 'iso-8859-1')new TextPart('html content', 'iso-8859-1', 'html'))$e->getBody());
    }

    
Home | Imprint | This part of the site doesn't use cookies.