10000 Allow Usage of ContentId in html by m42e · Pull Request #48901 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Allow Usage of ContentId in html #48901

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
Jan 9, 2023
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
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.
  • Loading branch information
m42e committed Jan 9, 2023
commit a5e4fc25289f67045c4a7cc64d3c8974e27ec5ce
5 changes: 5 additions & 0 deletions src/Symfony/Component/Mime/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.3
---

* Support detection of related parts if `Content-Id` is used instead of the name

6.2
---

Expand Down
6 changes: 4 additions & 2 deletions src/Symfony/Component/Mime/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,14 +492,16 @@ private function prepareParts(): ?array
$otherParts = $relatedParts = [];
foreach ($this->attachments as $part) {
foreach ($names as $name) {
if ($name !== $part->getName()) {
if ($name !== $part->getName() && (!$part->hasContentId() || $name !== $part->getContentId())) {
continue;
}
if (isset($relatedParts[$name])) {
continue 2;
}

$html = str_replace('cid:'.$name, 'cid:'.$part->getContentId(), $html, $count);
if ($name !== $part->getContentId()) {
$html = str_replace('cid:'.$name, 'cid:'.$part->getContentId(), $html, $count);
}
$relatedParts[$name] = $part;
$part->setName($part->getContentId())->asInline();

Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/Mime/Tests/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,24 @@ public function testGenerateBodyWithTextAndHtmlAndAttachedFileAndAttachedImageRe
$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->addPart(new DataPart($file));
$img = new DataPart($image, 'test.gif');
$e->addPart($img);
$e->html($content = 'html content <img src="cid:'.$img->getContentId().'">');
$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
Expand Down
0