Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Mime] Throw exception when body in Email attach method is not ok #46367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions 20 src/Symfony/Component/Mime/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,16 @@ public function getPriority(): int
}

/**
* @param resource|string $body
* @param resource|string|null $body
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was not documented before but null car be pass

*
* @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;

Expand All @@ -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;

Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
62 changes: 62 additions & 0 deletions 62 src/Symfony/Component/Mime/Tests/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.