Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a5e4fc2

Browse files
committed
Allow Usage of ContentId in html
Detect usage of Content-Id in html and mark part as related, just as it would happen with a `cid:<name>` reference.
1 parent f4a9a5d commit a5e4fc2

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/Symfony/Component/Mime/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.3
5+
---
6+
7+
* Support detection of related parts if `Content-Id` is used instead of the name
8+
49
6.2
510
---
611

src/Symfony/Component/Mime/Email.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,14 +492,16 @@ private function prepareParts(): ?array
492492
$otherParts = $relatedParts = [];
493493
foreach ($this->attachments as $part) {
494494
foreach ($names as $name) {
495-
if ($name !== $part->getName()) {
495+
if ($name !== $part->getName() && (!$part->hasContentId() || $name !== $part->getContentId())) {
496496
continue;
497497
}
498498
if (isset($relatedParts[$name])) {
499499
continue 2;
500500
}
501501

502-
$html = str_replace('cid:'.$name, 'cid:'.$part->getContentId(), $html, $count);
502+
if ($name !== $part->getContentId()) {
503+
$html = str_replace('cid:'.$name, 'cid:'.$part->getContentId(), $html, $count);
504+
}
503505
$relatedParts[$name] = $part;
504506
$part->setName($part->getContentId())->asInline();
505507

src/Symfony/Component/Mime/Tests/EmailTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,24 @@ public function testGenerateBodyWithTextAndHtmlAndAttachedFileAndAttachedImageRe
409409
$this->assertStringContainsString('cid:'.$parts[1]->getContentId(), $generatedHtml->getBody());
410410
}
411411

412+
public function testGenerateBodyWithTextAndHtmlAndAttachedFileAndAttachedImageReferencedViaCidAndContentId()
413+
{
414+
[$text, $html, $filePart, $file, $imagePart, $image] = $this->generateSomeParts();
415+
$e = (new Email())->from('[email protected]')->to('[email protected]');
416+
$e->text('text content');
417+
$e->addPart(new DataPart($file));
418+
$img = new DataPart($image, 'test.gif');
419+
$e->addPart($img);
420+
$e->html($content = 'html content <img src="cid:'.$img->getContentId().'">');
421+
$body = $e->getBody();
422+
$this->assertInstanceOf(MixedPart::class, $body);
423+
$this->assertCount(2, $related = $body->getParts());
424+
$this->assertInstanceOf(RelatedPart::class, $related[0]);
425+
$this->assertEquals($filePart, $related[1]);
426+
$this->assertCount(2, $parts = $related[0]->getParts());
427+
$this->assertInstanceOf(AlternativePart::class, $parts[0]);
428+
}
429+
412430
public function testGenerateBodyWithHtmlAndInlinedImageTwiceReferencedViaCid()
413431
{
414432
// inline image (twice) referenced in the HTML content

0 commit comments

Comments
 (0)