Skip to content

Commit 6f20a87

Browse files
committed
bug #51489 [Mime] Fix email (de)serialization issues (X-Coder264)
This PR was squashed before being merged into the 6.3 branch. Discussion ---------- [Mime] Fix email (de)serialization issues | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #47991 | License | MIT | Doc PR | - #48156 fixed #47991 while introducing a big breaking change (the `File` lazy load feature is broken and that was the whole point of that class when it was introduced in #47462 as that feature existed even prior to that PR) on a minor Symfony version (updating from 6.1 to 6.2 broke our application). More context can be found here: #48156 (comment) This PR aims to revert back the `attachFromPath` behavior to what it was before #48156 while still fixing the deserialization issue reported in #47991 The first commit fixes the serialization logic to work the same way it had worked on both 5.4 and 6.1 (which means we are reverting #48156), while the second commit fixes the deserialization issue reported in #47991. I've also added tests to prevent serialization/deserialization regressions in the future. Commits ------- 32836b9 [Mime] Fix email (de)serialization issues
2 parents 21a4345 + 32836b9 commit 6f20a87

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

src/Symfony/Component/Mime/Part/DataPart.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public function __wakeup()
156156
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
157157
}
158158
foreach (['body', 'charset', 'subtype', 'disposition', 'name', 'encoding'] as $name) {
159-
if (null !== $this->_parent[$name] && !\is_string($this->_parent[$name])) {
159+
if (null !== $this->_parent[$name] && !\is_string($this->_parent[$name]) && !$this->_parent[$name] instanceof File) {
160160
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
161161
}
162162
$r = new \ReflectionProperty(TextPart::class, $name);

src/Symfony/Component/Mime/Part/TextPart.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ private function chooseEncoding(): string
226226
public function __sleep(): array
227227
{
228228
// convert resources to strings for serialization
229-
if (null !== $this->seekable || $this->body instanceof File) {
229+
if (null !== $this->seekable) {
230230
$this->body = $this->getBody();
231231
$this->seekable = null;
232232
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,4 +658,34 @@ public function testBodyCache()
658658
$body2 = $email->getBody();
659659
$this->assertNotSame($body1, $body2, 'The two bodies must not reference the same object, so the body cache does not ensure that the hash for the DKIM signature is unique.');
660660
}
661+
662+
public function testAttachmentBodyIsPartOfTheSerializationEmailPayloadWhenUsingAttachMethod()
663+
{
664+
$email = new Email();
665+
$email->attach(file_get_contents(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'foo_attachment.txt') ?: '');
666+
667+
$this->assertTrue(str_contains(serialize($email), 'foo_bar_xyz_123'));
668+
}
669+
670+
public function testAttachmentBodyIsNotPartOfTheSerializationEmailPayloadWhenUsingAttachFromPathMethod()
671+
{
672+
$email = new Email();
673+
$email->attachFromPath(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'foo_attachment.txt');
674+
675+
$this->assertFalse(str_contains(serialize($email), 'foo_bar_xyz_123'));
676+
}
677+
678+
public function testEmailsWithAttachmentsWhichAreAFileInstanceCanBeUnserialized()
679+
{
680+
$email = new Email();
681+
$email->attachFromPath(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'foo_attachment.txt');
682+
683+
$email = unserialize(serialize($email));
684+
$this->assertInstanceOf(Email::class, $email);
685+
686+
$attachments = $email->getAttachments();
687+
688+
$this->assertCount(1, $attachments);
689+
$this->assertStringContainsString('foo_bar_xyz_123', $attachments[0]->getBody());
690+
}
661691
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo_bar_xyz_123

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy