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

Skip to content

[Mime] Fix undefined array key 0 when empty sender #53565

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 1 commit into from
Jan 21, 2024
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
4 changes: 4 additions & 0 deletions src/Symfony/Component/Mime/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ public function addFrom(...$addresses)
*/
public function from(...$addresses)
{
if (!$addresses) {
throw new LogicException('"from()" must be called with at least one address.');
}

return $this->setListAddressHeaderBody('From', $addresses);
}

Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Component/Mime/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ public function generateMessageId(): string
if ($this->headers->has('Sender')) {
$sender = $this->headers->get('Sender')->getAddress();
} elseif ($this->headers->has('From')) {
$sender = $this->headers->get('From')->getAddresses()[0];
if (!$froms = $this->headers->get('From')->getAddresses()) {
throw new LogicException('A "From" header must have at least one email address.');
}
$sender = $froms[0];
} else {
throw new LogicException('An email must have a "From" or a "Sender" header.');
}
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/Mime/Tests/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Exception\LogicException;
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Mime\Part\Multipart\AlternativePart;
use Symfony\Component\Mime\Part\Multipart\MixedPart;
Expand Down Expand Up @@ -62,6 +63,13 @@ public function testSender()
$this->assertSame($fabien, $e->getSender());
}

public function testFromWithNoAddress()
{
$e = new Email();
$this->expectException(LogicException::class);
$e->from();
}

public function testFrom()
{
$e = new Email();
Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/Mime/Tests/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Exception\LogicException;
use Symfony\Component\Mime\Header\Headers;
use Symfony\Component\Mime\Header\MailboxListHeader;
use Symfony\Component\Mime\Header\UnstructuredHeader;
Expand Down Expand Up @@ -125,6 +126,14 @@ public function testGetPreparedHeadersHasSenderWhenNeeded()
$this->assertEquals('[email protected]', $message->getPreparedHeaders()->get('Sender')->getAddress()->getAddress());
}

public function testGenerateMessageIdThrowsWhenHasFromButNoAddresses()
{
$message = new Message();
$message->getHeaders()->addMailboxListHeader('From', []);
$this->expectException(LogicException::class);
$message->generateMessageId();
}

public function testToString()
{
$message = new Message();
Expand Down