Skip to content

[Mime] Fix email (de)serialization issues #51489

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
Sep 29, 2023
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
2 changes: 1 addition & 1 deletion src/Symfony/Component/Mime/Part/DataPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Mime/Part/TextPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
30 changes: 30 additions & 0 deletions src/Symfony/Component/Mime/Tests/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo_bar_xyz_123