diff --git a/src/Symfony/Component/Mime/Part/DataPart.php b/src/Symfony/Component/Mime/Part/DataPart.php index b42ecb4da102e..2e6fc7e3a67b1 100644 --- a/src/Symfony/Component/Mime/Part/DataPart.php +++ b/src/Symfony/Component/Mime/Part/DataPart.php @@ -156,7 +156,7 @@ public function __wakeup() throw new \BadMethodCallException('Cannot unserialize '.__CLASS__); } foreach (['body', 'charset', 'subtype', 'disposition', 'name', 'encoding'] as $name) { - if (null !== $this->_parent[$name] && !\is_string($this->_parent[$name])) { + if (null !== $this->_parent[$name] && !\is_string($this->_parent[$name]) && !$this->_parent[$name] instanceof File) { throw new \BadMethodCallException('Cannot unserialize '.__CLASS__); } $r = new \ReflectionProperty(TextPart::class, $name); diff --git a/src/Symfony/Component/Mime/Part/TextPart.php b/src/Symfony/Component/Mime/Part/TextPart.php index 3b1eb8d82204a..4bd24d612fa28 100644 --- a/src/Symfony/Component/Mime/Part/TextPart.php +++ b/src/Symfony/Component/Mime/Part/TextPart.php @@ -226,7 +226,7 @@ private function chooseEncoding(): string public function __sleep(): array { // convert resources to strings for serialization - if (null !== $this->seekable || $this->body instanceof File) { + if (null !== $this->seekable) { $this->body = $this->getBody(); $this->seekable = null; } diff --git a/src/Symfony/Component/Mime/Tests/EmailTest.php b/src/Symfony/Component/Mime/Tests/EmailTest.php index c4f829d7d12de..b8cc7368d4a30 100644 --- a/src/Symfony/Component/Mime/Tests/EmailTest.php +++ b/src/Symfony/Component/Mime/Tests/EmailTest.php @@ -658,4 +658,34 @@ public function testBodyCache() $body2 = $email->getBody(); $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.'); } + + public function testAttachmentBodyIsPartOfTheSerializationEmailPayloadWhenUsingAttachMethod() + { + $email = new Email(); + $email->attach(file_get_contents(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'foo_attachment.txt') ?: ''); + + $this->assertTrue(str_contains(serialize($email), 'foo_bar_xyz_123')); + } + + public function testAttachmentBodyIsNotPartOfTheSerializationEmailPayloadWhenUsingAttachFromPathMethod() + { + $email = new Email(); + $email->attachFromPath(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'foo_attachment.txt'); + + $this->assertFalse(str_contains(serialize($email), 'foo_bar_xyz_123')); + } + + public function testEmailsWithAttachmentsWhichAreAFileInstanceCanBeUnserialized() + { + $email = new Email(); + $email->attachFromPath(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'foo_attachment.txt'); + + $email = unserialize(serialize($email)); + $this->assertInstanceOf(Email::class, $email); + + $attachments = $email->getAttachments(); + + $this->assertCount(1, $attachments); + $this->assertStringContainsString('foo_bar_xyz_123', $attachments[0]->getBody()); + } } diff --git a/src/Symfony/Component/Mime/Tests/Fixtures/foo_attachment.txt b/src/Symfony/Component/Mime/Tests/Fixtures/foo_attachment.txt new file mode 100644 index 0000000000000..291b582ce207c --- /dev/null +++ b/src/Symfony/Component/Mime/Tests/Fixtures/foo_attachment.txt @@ -0,0 +1 @@ +foo_bar_xyz_123