8000 [Mime] Fix embed logic for background attributes by flack · Pull Request #45376 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Mime] Fix embed logic for background attributes #45376

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
Feb 10, 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
13 changes: 11 additions & 2 deletions src/Symfony/Component/Mime/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,17 @@ private function prepareParts(): ?array
if (null !== $this->html) {
$htmlPart = new TextPart($html, $this->htmlCharset, 'html');
$html = $htmlPart->getBody();
preg_match_all('(<img\s+[^>]*src\s*=\s*(?:([\'"])cid:([^"]+)\\1|cid:([^>\s]+)))i', $html, $names);
$names = array_filter(array_unique(array_merge($names[2], $names[3])));

$regexes = [
'<img\s+[^>]*src\s*=\s*(?:([\'"])cid:([^"]+)\\1|cid:([^>\s]+))',
'<\w+\s+[^>]*background\s*=\s*(?:([\'"])cid:([^"]+)\\1|cid:([^>\s]+))',
];
$tmpMatches = [];
foreach ($regexes as $regex) {
preg_match_all('/'.$regex.'/i', $html, $tmpMatches);
$names = array_merge($names, $tmpMatches[2], $tmpMatches[3]);
}
$names = array_filter(array_unique($names));
}

$attachmentParts = $inlineParts = [];
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/Mime/Tests/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,14 @@ public function testGenerateBody()
// 2 parts only, not 3 (text + embedded image once)
$this->assertCount(2, $parts = $body->getParts());
$this->assertStringMatchesFormat('html content <img src=3D"cid:%s@symfony">', $parts[0]->bodyToString());

$e = (new Email())->from('me@example.com')->to('you@example.com');
$e->html('<div background="cid:test.gif"></div>');
$e->embed($image, 'test.gif');
$body = $e->getBody();
$this->assertInstanceOf(RelatedPart::class, $body);
$this->assertCount(2, $parts = $body->getParts());
$this->assertStringMatchesFormat('<div background=3D"cid:%s@symfony"></div>', $parts[0]->bodyToString());
}

public function testAttachments()
Expand Down
0