From 8561c4e91f45da45bbd5f4a5d2eb6cc5820dcc68 Mon Sep 17 00:00:00 2001 From: Antoine Lamirault Date: Mon, 16 May 2022 23:52:41 +0200 Subject: [PATCH] [Mime] Throw exception when body in Email attach method is not ok --- src/Symfony/Component/Mime/Email.php | 20 +++++- .../Component/Mime/Tests/EmailTest.php | 62 +++++++++++++++++++ 2 files changed, 80 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Mime/Email.php b/src/Symfony/Component/Mime/Email.php index 7a7a0db32e167..5e31cb92a85f2 100644 --- a/src/Symfony/Component/Mime/Email.php +++ b/src/Symfony/Component/Mime/Email.php @@ -272,12 +272,16 @@ public function getPriority(): int } /** - * @param resource|string $body + * @param resource|string|null $body * * @return $this */ public function text($body, string $charset = 'utf-8') { + if (null !== $body && !\is_string($body) && !\is_resource($body)) { + throw new \TypeError(sprintf('The body must be a string, a resource or null (got "%s").', get_debug_type($body))); + } + $this->text = $body; $this->textCharset = $charset; @@ -304,6 +308,10 @@ public function getTextCharset(): ?string */ public function html($body, string $charset = 'utf-8') { + if (null !== $body && !\is_string($body) && !\is_resource($body)) { + throw new \TypeError(sprintf('The body must be a string, a resource or null (got "%s").', get_debug_type($body))); + } + $this->html = $body; $this->htmlCharset = $charset; @@ -330,6 +338,10 @@ public function getHtmlCharset(): ?string */ public function attach($body, string $name = null, string $contentType = null) { + if (!\is_string($body) && !\is_resource($body)) { + throw new \TypeError(sprintf('The body must be a string or a resource (got "%s").', get_debug_type($body))); + } + $this->attachments[] = ['body' => $body, 'name' => $name, 'content-type' => $contentType, 'inline' => false]; return $this; @@ -352,6 +364,10 @@ public function attachFromPath(string $path, string $name = null, string $conten */ public function embed($body, string $name = null, string $contentType = null) { + if (!\is_string($body) && !\is_resource($body)) { + throw new \TypeError(sprintf('The body must be a string or a resource (got "%s").', get_debug_type($body))); + } + $this->attachments[] = ['body' => $body, 'name' => $name, 'content-type' => $contentType, 'inline' => true]; return $this; @@ -463,7 +479,7 @@ private function prepareParts(): ?array $names = []; $htmlPart = null; $html = $this->html; - if (null !== $this->html) { + if (null !== $html) { if (\is_resource($html)) { if (stream_get_meta_data($html)['seekable'] ?? false) { rewind($html); diff --git a/src/Symfony/Component/Mime/Tests/EmailTest.php b/src/Symfony/Component/Mime/Tests/EmailTest.php index baffa4a4e0655..a12ca180e611c 100644 --- a/src/Symfony/Component/Mime/Tests/EmailTest.php +++ b/src/Symfony/Component/Mime/Tests/EmailTest.php @@ -396,4 +396,66 @@ public function testMissingHeaderDoesNotThrowError() $emailHeaderSame = new EmailHeaderSame('foo', 'bar'); $emailHeaderSame->evaluate($e); } + + public function testAttachBodyExpectStringOrResource() + { + $this->expectException(\TypeError::class); + $this->expectExceptionMessage('The body must be a string or a resource (got "bool").'); + + (new Email())->attach(false); + } + + public function testEmbedBodyExpectStringOrResource() + { + $this->expectException(\TypeError::class); + $this->expectExceptionMessage('The body must be a string or a resource (got "bool").'); + + (new Email())->embed(false); + } + + public function testHtmlBodyExpectStringOrResourceOrNull() + { + $this->expectException(\TypeError::class); + $this->expectExceptionMessage('The body must be a string, a resource or null (got "bool").'); + + (new Email())->html(false); + } + + public function testHtmlBodyAcceptedTypes() + { + $email = new Email(); + + $email->html('foo'); + $this->assertSame('foo', $email->getHtmlBody()); + + $email->html(null); + $this->assertNull($email->getHtmlBody()); + + $contents = file_get_contents(__DIR__.'/Fixtures/mimetypes/test', 'r'); + $email->html($contents); + $this->assertSame($contents, $email->getHtmlBody()); + } + + public function testTextBodyExpectStringOrResourceOrNull() + { + $this->expectException(\TypeError::class); + $this->expectExceptionMessage('The body must be a string, a resource or null (got "bool").'); + + (new Email())->text(false); + } + + public function testTextBodyAcceptedTypes() + { + $email = new Email(); + + $email->text('foo'); + $this->assertSame('foo', $email->getTextBody()); + + $email->text(null); + $this->assertNull($email->getTextBody()); + + $contents = file_get_contents(__DIR__.'/Fixtures/mimetypes/test', 'r'); + $email->text($contents); + $this->assertSame($contents, $email->getTextBody()); + } }