Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 402997f

Browse files
committed
[Mime] Throw exception when body in Email attach method is not ok
1 parent c3db5c1 commit 402997f

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

src/Symfony/Component/Mime/Email.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Mime;
1313

1414
use Symfony\Component\Mime\Exception\LogicException;
15+
use Symfony\Component\Mime\Exception\UnexpectedTypeException;
1516
use Symfony\Component\Mime\Part\AbstractPart;
1617
use Symfony\Component\Mime\Part\DataPart;
1718
use Symfony\Component\Mime\Part\Multipart\AlternativePart;
@@ -330,6 +331,10 @@ public function getHtmlCharset(): ?string
330331
*/
331332
public function attach($body, string $name = null, string $contentType = null)
332333
{
334+
if(!\is_string($body) && !\is_resource($body)){
335+
throw new UnexpectedTypeException($body, 'resource or string');
336+
}
337+
333338
$this->attachments[] = ['body' => $body, 'name' => $name, 'content-type' => $contentType, 'inline' => false];
334339

335340
return $this;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mime\Exception;
13+
14+
class UnexpectedTypeException extends InvalidArgumentException
15+
{
16+
public function __construct($value, string $expectedType)
17+
{
18+
parent::__construct(sprintf('Expected argument of type "%s", "%s" given', $expectedType, \is_object($value) ? \get_class($value) : \gettype($value)));
19+
}
20+
}

src/Symfony/Component/Mime/Tests/EmailTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Mime\Address;
1616
use Symfony\Component\Mime\Email;
17+
use Symfony\Component\Mime\Exception\UnexpectedTypeException;
1718
use Symfony\Component\Mime\Part\DataPart;
1819
use Symfony\Component\Mime\Part\Multipart\AlternativePart;
1920
use Symfony\Component\Mime\Part\Multipart\MixedPart;
@@ -384,4 +385,12 @@ public function testSerialize()
384385
$this->assertEquals($expected->getHeaders(), $n->getHeaders());
385386
$this->assertEquals($e->getBody(), $n->getBody());
386387
}
388+
389+
public function testAttachBodyExpectStringOrResource()
390+
{
391+
$this->expectException(UnexpectedTypeException::class);
392+
$this->expectExceptionMessage('Expected argument of type "resource or string", "boolean" given');
393+
394+
(new Email())->attach(false);
395+
}
387396
}

0 commit comments

Comments
 (0)