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

Skip to content

Commit 0aa12cd

Browse files
committed
feature #48901 Allow Usage of ContentId in html (m42e)
This PR was merged into the 6.3 branch. Discussion ---------- 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. | Q | A | ------------- | --- | Branch? | 6.3 <!-- see below --> | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | | License | MIT | Doc PR | - <!-- Replace this notice by a short README for your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the latest branch. - For new features, provide some code snippets to help understand usage. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> Assumed you have a defined `Content-Id` for your attachment, as you modify and forward an id. The HTML part will already contain the reference to that id in the `cid:` reference. Which means the name is no longer part of the html body. In this case it is currently not detected that the parts are related to each other. This will be improved with this PR. I do not consider it a bug fix. The feature has a very special use case. If you consider otherwise, let me know and I will create a PR against the maintenance branch. (I already have one prepared: https://github.com/m42e/symfony/tree/allow-usage-of-content-id-in-html-5.4 Which means the feature/fix would reach more people. Please let me know what you think.) Commits ------- a5e4fc2 Allow Usage of ContentId in html
2 parents 8fee664 + a5e4fc2 commit 0aa12cd

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)