From d4a87d26c7002e778fd5b4b55468ce246b12b7a0 Mon Sep 17 00:00:00 2001 From: flack Date: Wed, 9 Feb 2022 14:20:26 +0100 Subject: [PATCH] [Mime] Fix embed logic for background attributes --- src/Symfony/Component/Mime/Email.php | 13 +++++++++++-- src/Symfony/Component/Mime/Tests/EmailTest.php | 8 ++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Mime/Email.php b/src/Symfony/Component/Mime/Email.php index 952c7b5c151ca..bfc7c37f1b043 100644 --- a/src/Symfony/Component/Mime/Email.php +++ b/src/Symfony/Component/Mime/Email.php @@ -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('(]*src\s*=\s*(?:([\'"])cid:([^"]+)\\1|cid:([^>\s]+)))i', $html, $names); - $names = array_filter(array_unique(array_merge($names[2], $names[3]))); + + $regexes = [ + ']*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 = []; diff --git a/src/Symfony/Component/Mime/Tests/EmailTest.php b/src/Symfony/Component/Mime/Tests/EmailTest.php index e5b48fe6cabdc..ea06c7f6c12f5 100644 --- a/src/Symfony/Component/Mime/Tests/EmailTest.php +++ b/src/Symfony/Component/Mime/Tests/EmailTest.php @@ -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 ', $parts[0]->bodyToString()); + + $e = (new Email())->from('me@example.com')->to('you@example.com'); + $e->html('
'); + $e->embed($image, 'test.gif'); + $body = $e->getBody(); + $this->assertInstanceOf(RelatedPart::class, $body); + $this->assertCount(2, $parts = $body->getParts()); + $this->assertStringMatchesFormat('
', $parts[0]->bodyToString()); } public function testAttachments()