Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Mime] Fix TextPart using an unknown File
Browse files Browse the repository at this point in the history
fabpot committed Jun 1, 2024
1 parent d9367f9 commit 68afdb8
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Symfony/Component/Mime/Part/TextPart.php
Original file line number Diff line number Diff line change
@@ -123,7 +123,11 @@ public function getName(): ?string
public function getBody(): string
{
if ($this->body instanceof File) {
return file_get_contents($this->body->getPath());
if (false === $ret = @file_get_contents($this->body->getPath())) {
throw new InvalidArgumentException(error_get_last()['message']);
}

return $ret;
}

if (null === $this->seekable) {
10 changes: 10 additions & 0 deletions src/Symfony/Component/Mime/Tests/Part/TextPartTest.php
Original file line number Diff line number Diff line change
@@ -55,6 +55,16 @@ public function testConstructorWithFile()
$this->assertSame('content', implode('', iterator_to_array($p->bodyToIterable())));
}

public function testConstructorWithUnknownFile()
{
$p = new TextPart(new File(\dirname(__DIR__).'/Fixtures/unknown.txt'));

// Exception should be thrown only when the body is accessed
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessageMatches('{Failed to open stream}');
$p->getBody();
}

public function testConstructorWithNonStringOrResource()
{
$this->expectException(\TypeError::class);

0 comments on commit 68afdb8

Please sign in to comment.