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

Skip to content

Commit d4a87d2

Browse files
flackfabpot
authored andcommitted
[Mime] Fix embed logic for background attributes
1 parent 6e3d5c1 commit d4a87d2

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/Symfony/Component/Mime/Email.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,17 @@ private function prepareParts(): ?array
460460
if (null !== $this->html) {
461461
$htmlPart = new TextPart($html, $this->htmlCharset, 'html');
462462
$html = $htmlPart->getBody();
463-
preg_match_all('(<img\s+[^>]*src\s*=\s*(?:([\'"])cid:([^"]+)\\1|cid:([^>\s]+)))i', $html, $names);
464-
$names = array_filter(array_unique(array_merge($names[2], $names[3])));
463+
464+
$regexes = [
465+
'<img\s+[^>]*src\s*=\s*(?:([\'"])cid:([^"]+)\\1|cid:([^>\s]+))',
466+
'<\w+\s+[^>]*background\s*=\s*(?:([\'"])cid:([^"]+)\\1|cid:([^>\s]+))',
467+
];
468+
$tmpMatches = [];
469+
foreach ($regexes as $regex) {
470+
preg_match_all('/'.$regex.'/i', $html, $tmpMatches);
471+
$names = array_merge($names, $tmpMatches[2], $tmpMatches[3]);
472+
}
473+
$names = array_filter(array_unique($names));
465474
}
466475

467476
$attachmentParts = $inlineParts = [];

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,14 @@ public function testGenerateBody()
351351
// 2 parts only, not 3 (text + embedded image once)
352352
$this->assertCount(2, $parts = $body->getParts());
353353
$this->assertStringMatchesFormat('html content <img src=3D"cid:%s@symfony">', $parts[0]->bodyToString());
354+
355+
$e = (new Email())->from('[email protected]')->to('[email protected]');
356+
$e->html('<div background="cid:test.gif"></div>');
357+
$e->embed($image, 'test.gif');
358+
$body = $e->getBody();
359+
$this->assertInstanceOf(RelatedPart::class, $body);
360+
$this->assertCount(2, $parts = $body->getParts());
361+
$this->assertStringMatchesFormat('<div background=3D"cid:%s@symfony"></div>', $parts[0]->bodyToString());
354362
}
355363

356364
public function testAttachments()

0 commit comments

Comments
 (0)