Skip to content

Commit

Permalink
[Mime] Fix email (de)serialization issues
Browse files Browse the repository at this point in the history
  • Loading branch information
X-Coder264 authored and fabpot committed Sep 29, 2023
1 parent 9a0cbd5 commit d5179ee
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion 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 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 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());
}
}
1 change: 1 addition & 0 deletions Tests/Fixtures/foo_attachment.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo_bar_xyz_123

0 comments on commit d5179ee

Please sign in to comment.