diff --git a/src/Symfony/Component/Mime/Email.php b/src/Symfony/Component/Mime/Email.php index bd0a476c4f778..dee0b71f84e9f 100644 --- a/src/Symfony/Component/Mime/Email.php +++ b/src/Symfony/Component/Mime/Email.php @@ -513,7 +513,7 @@ private function prepareParts(): ?array $related = false; foreach ($names as $name) { - if ($name !== $attachment['name']) { + if ($name !== $attachment['name'] && (!$part->hasContentId() || $name !== $part->getContentId())) { continue; } if (isset($relatedParts[$name])) { diff --git a/src/Symfony/Component/Mime/Tests/EmailTest.php b/src/Symfony/Component/Mime/Tests/EmailTest.php index 516a589180823..79eb228cfd281 100644 --- a/src/Symfony/Component/Mime/Tests/EmailTest.php +++ b/src/Symfony/Component/Mime/Tests/EmailTest.php @@ -427,6 +427,24 @@ public function testGenerateBodyWithTextAndHtmlAndAttachedFileAndAttachedImagePa $this->assertStringContainsString('cid:'.$parts[1]->getContentId(), $generatedHtml->getBody()); } + public function testGenerateBodyWithTextAndHtmlAndAttachedFileAndAttachedImageReferencedViaCidAndContentId() + { + [$text, $html, $filePart, $file, $imagePart, $image] = $this->generateSomeParts(); + $e = (new Email())->from('me@example.com')->to('you@example.com'); + $e->text('text content'); + $e->attach($file); + $img = new DataPart($image, 'test.gif'); + $e->attachPart($img->asInline()); + $e->html($content = 'html content '); + $body = $e->getBody(); + $this->assertInstanceOf(MixedPart::class, $body); + $this->assertCount(2, $related = $body->getParts()); + $this->assertInstanceOf(RelatedPart::class, $related[0]); + $this->assertEquals($filePart, $related[1]); + $this->assertCount(2, $parts = $related[0]->getParts()); + $this->assertInstanceOf(AlternativePart::class, $parts[0]); + } + public function testGenerateBodyWithHtmlAndInlinedImageTwiceReferencedViaCid() { // inline image (twice) referenced in the HTML content