Skip to content

Commit

Permalink
Merge branch '6.3' into 6.4
Browse files Browse the repository at this point in the history
* 6.3:
  [AssetMapper] Handle assets with non-ascii characters in dev server
  [Translation] Fix `TranslationNodeVisitor` with constant domain
  [Messenger] [AMQP] Throw exception on `nack` callback
  [Validator] revise Latvian translations
  [ErrorHandler] Fix `RecursiveDirectoryIterator` exception with wrong composer autoload
  [HttpFoundation] Request without content-type or content-length header should result in null values, not empty strings
  [Cache] Fix possible infinite loop in `CachePoolPass`
  grab a service from the container only if it exists
  [Mime] Fix undefined array key 0 when empty sender
  [Console] Allow '0' as a $shortcut in InputOption.php
  fix multi-byte code area to convert
  [Validator] Make it explicit when English translation differs from its resource name
  • Loading branch information
nicolas-grekas committed Jan 23, 2024
2 parents ca4f58b + cacb478 commit c2904c5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ public function addFrom(Address|string ...$addresses): static
*/
public function from(Address|string ...$addresses): static
{
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 Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,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 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\File;
use Symfony\Component\Mime\Part\Multipart\AlternativePart;
Expand Down Expand Up @@ -63,6 +64,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 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('thomas@symfony.com', $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

0 comments on commit c2904c5

Please sign in to comment.