Skip to content

[Mime] Fix email rendering when having inlined parts that are not related to the content #47437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions src/Symfony/Component/Mime/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ private function generateBody(): AbstractPart

$this->ensureValidity();

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

$part = null === $this->text ? null : new TextPart($this->text, $this->textCharset);
if (null !== $htmlPart) {
Expand All @@ -474,15 +474,15 @@ private function generateBody(): AbstractPart
}
}

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

if ($attachmentParts) {
if ($otherParts) {
if ($part) {
$part = new MixedPart($part, ...$attachmentParts);
$part = new MixedPart($part, ...$otherParts);
} else {
$part = new MixedPart(...$attachmentParts);
$part = new MixedPart(...$otherParts);
}
}

Expand All @@ -508,42 +508,44 @@ private function prepareParts(): ?array
}

// usage of reflection is a temporary workaround for missing getters that will be added in 6.2
$dispositionRef = new \ReflectionProperty(TextPart::class, 'disposition');
$dispositionRef->setAccessible(true);
$nameRef = new \ReflectionProperty(TextPart::class, 'name');
$nameRef->setAccessible(true);
$attachmentParts = $inlineParts = [];
$otherParts = $relatedParts = [];
foreach ($this->attachments as $attachment) {
$part = $this->createDataPart($attachment);
if (isset($attachment['part'])) {
$attachment['name'] = $nameRef->getValue($part);
}

$related = false;
foreach ($names as $name) {
if ($name !== $attachment['name']) {
continue;
}
if (isset($inlineParts[$name])) {
if (isset($relatedParts[$name])) {
continue 2;
}
$part->setDisposition('inline');
$html = str_replace('cid:'.$name, 'cid:'.$part->getContentId(), $html);
$html = str_replace('cid:'.$name, 'cid:'.$part->getContentId(), $html, $count);
if ($count) {
$related = true;
}
$part->setName($part->getContentId());

break;
}

if ('inline' === $dispositionRef->getValue($part)) {
$inlineParts[$attachment['name']] = $part;
if ($related) {
$relatedParts[$attachment['name']] = $part;
} else {
$attachmentParts[] = $part;
$otherParts[] = $part;
}
}
if (null !== $htmlPart) {
$htmlPart = new TextPart($html, $this->htmlCharset, 'html');
}

return [$htmlPart, $attachmentParts, array_values($inlineParts)];
return [$htmlPart, $otherParts, array_values($relatedParts)];
}

private function createDataPart(array $attachment): DataPart
Expand Down
29 changes: 29 additions & 0 deletions src/Symfony/Component/Mime/Tests/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,17 @@ public function testGenerateBodyWithHtmlContentAndAttachedFile()
$this->assertEquals(new MixedPart($html, $filePart), $e->getBody());
}

public function testGenerateBodyWithHtmlContentAndInlineImageNotreferenced()
{
[$text, $html, $filePart, $file, $imagePart, $image] = $this->generateSomeParts();
$imagePart = new DataPart($image = fopen(__DIR__.'/Fixtures/mimetypes/test.gif', 'r'));
$imagePart->asInline();
$e = (new Email())->from('me@example.com')->to('you@example.com');
$e->embed($image);
$e->html('html content');
$this->assertEquals(new MixedPart($html, $imagePart), $e->getBody());
}

public function testGenerateBodyWithAttachedFileOnly()
{
[$text, $html, $filePart, $file, $imagePart, $image] = $this->generateSomeParts();
Expand All @@ -310,6 +321,24 @@ public function testGenerateBodyWithAttachedFileOnly()
$this->assertEquals(new MixedPart($filePart), $e->getBody());
}

public function testGenerateBodyWithInlineImageOnly()
{
$imagePart = new DataPart($image = fopen(__DIR__.'/Fixtures/mimetypes/test.gif', 'r'));
$imagePart->asInline();
$e = (new Email())->from('me@example.com')->to('you@example.com');
$e->embed($image);
$this->assertEquals(new MixedPart($imagePart), $e->getBody());
}

public function testGenerateBodyWithEmbeddedImageOnly()
{
$imagePart = new DataPart($image = fopen(__DIR__.'/Fixtures/mimetypes/test.gif', 'r'));
$e = (new Email())->from('me@example.com')->to('you@example.com');
$e->embed($image);
$imagePart->asInline();
$this->assertEquals(new MixedPart($imagePart), $e->getBody());
}

public function testGenerateBodyWithTextAndHtmlContentAndAttachedFile()
{
[$text, $html, $filePart, $file, $imagePart, $image] = $this->generateSomeParts();
Expand Down