diff --git a/Part/TextPart.php b/Part/TextPart.php index a133009..1054c25 100644 --- a/Part/TextPart.php +++ b/Part/TextPart.php @@ -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) { diff --git a/Tests/Part/TextPartTest.php b/Tests/Part/TextPartTest.php index 905349e..ae1a592 100644 --- a/Tests/Part/TextPartTest.php +++ b/Tests/Part/TextPartTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Mime\Tests\Part; use PHPUnit\Framework\TestCase; +use Symfony\Component\Mime\Exception\InvalidArgumentException; use Symfony\Component\Mime\Header\Headers; use Symfony\Component\Mime\Header\ParameterizedHeader; use Symfony\Component\Mime\Header\UnstructuredHeader; @@ -55,6 +56,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);