8000 bug #46367 [Mime] Throw exception when body in Email attach method is… · symfony/symfony@f3df3d0 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit f3df3d0

Browse files
bug #46367 [Mime] Throw exception when body in Email attach method is not ok (alamirault)
This PR was squashed before being merged into the 4.4 branch. Discussion ---------- [Mime] Throw exception when body in Email attach method is not ok | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #45350 | License | MIT | Doc PR | symfony/symfony-docs#... As proposed in #45350, throw exception when calling `(new Email())->attach($badBodyType);` when type is not supported Commits ------- 8561c4e [Mime] Throw exception when body in Email attach method is not ok
2 parents a6a5076 + 8561c4e commit f3df3d0

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

src/Symfony/Component/Mime/Email.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,16 @@ public function getPriority(): int
272272
}
273273

274274
/**
275-
* @param resource|string $body
275+
* @param resource|string|null $body
276276
*
277277
* @return $this
278278
*/
279279
public function text($body, string $charset = 'utf-8')
280280
{
281+
if (null !== $body && !\is_string($body) && !\is_resource($body)) {
282+
throw new \TypeError(sprintf('The body must be a string, a resource or null (got "%s").', get_debug_type($body)));
283+
}
284+
281285
$this->text = $body;
282286
$this->textCharset = $charset;
283287

@@ -304,6 +308,10 @@ public function getTextCharset(): ?string
304308
*/
305309
public function html($body, string $charset = 'utf-8')
306310
{
311+
if (null !== $body && !\is_string($body) && !\is_resource($body)) {
312+
throw new \TypeError(sprintf('The body must be a string, a resource or null (got "%s").', get_debug_type($body)));
313+
}
314+
307315
10000 $this->html = $body;
308316
$this->htmlCharset = $charset;
309317

@@ -330,6 +338,10 @@ public function getHtmlCharset(): ?string
330338
*/
331339
public function attach($body, string $name = null, string $contentType = null)
332340
{
341+
if (!\is_string($body) && !\is_resource($body)) {
342+
throw new \TypeError(sprintf('The body must be a string or a resource (got "%s").', get_debug_type($body)));
343+
}
344+
333345
$this->attachments[] = ['body' => $body, 'name' => $name, 'content-type' => $contentType, 'inline' => false];
334346

335347
return $this;
@@ -352,6 +364,10 @@ public function attachFromPath(string $path, string $name = null, string $conten
352364
*/
353365
public function embed($body, string $name = null, string $contentType = null)
354366
{
367+
if (!\is_string($body) && !\is_resource($body)) {
368+
throw new \TypeError(sprintf('The body must be a string or a resource (got "%s").', get_debug_type($body)));
369+
}
370+
355371
$this->attachments[] = ['body' => $body, 'name' => $name, 'content-type' => $contentType, 'inline' => true];
356372

357373
return $this;
@@ -463,7 +479,7 @@ private function prepareParts(): ?array
463479
$names = [];
464480
$htmlPart = null;
465481
$html = $this->html;
466-
if (null !== $this->html) {
482+
if (null !== $html) {
467483
if (\is_resource($html)) {
468484
if (stream_get_meta_data($html)['seekable'] ?? false) {
469485
rewind($html);

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,4 +396,66 @@ public function testMissingHeaderDoesNotThrowError()
396396
$emailHeaderSame = new EmailHeaderSame('foo', 'bar');
397397
$emailHeaderSame->evaluate($e);
398398
}
399+
400+
public function testAttachBodyExpectStringOrResource()
401+
{
402+
$this->expectException(\TypeError::class);
403+
$this->expectExceptionMessage('The body must be a string or a resource (got "bool").');
404+
405+
(new Email())->attach(false);
406+
}
407+
408+
public function testEmbedBodyExpectStringOrResource()
409+
{
410+
$this->expectException(\TypeError::class);
411+
$this->expectExceptionMessage('The body must be a string or a resource (got "bool").');
412+
413+
(new Email())->embed(false);
414+
}
415+
416+
public function testHtmlBodyExpectStringOrResourceOrNull()
417+
{
418+
$this->expectException(\TypeError::class);
419+
$this->expectExceptionMessage('The body must be a string, a resource or null (got "bool").');
420+
421+
(new Email())->html(false);
422+
}
423+
424+
public function testHtmlBodyAcceptedTypes()
425+
{
426+
$email = new Email();
427+
428+
$email->html('foo');
429+
$this->assertSame('foo', $email->getHtmlBody());
430+
431+
$email->html(null);
432+
$this->assertNull($email->getHtmlBody());
433+
434+
$contents = file_get_contents(__DIR__.'/Fixtures/mimetypes/test', 'r');
435+
$email->html($contents);
436+
$this->assertSame($contents, $email->getHtmlBody());
437+
}
438+
439+
public function testTextBodyExpectStringOrResourceOrNull()
440+
{
441+
$this->expectException(\TypeError::class);
442+
$this->expectExceptionMessage('The body must be a string, a resource or null (got "bool").');
443+
444+
(new Email())->text(false);
445+
}
446+
447+
public function testTextBodyAcceptedTypes()
448+
{
449+
$email = new Email();
450+
451+
$email->text('foo');
452+
$this->assertSame('foo', $email->getTextBody());
453+
454+
$email->text(null);
455+
$this->assertNull($email->getTextBody());
456+
457+
$contents = file_get_contents(__DIR__.'/Fixtures/mimetypes/test', 'r');
458+
$email->text($contents);
459+
$this->assertSame($contents, $email->getTextBody());
460+
}
399461
}

0 commit comments

Comments
 (0)
0