Repository: laravel/framework (php)

Issue:
Email Notification Customizing The Recipient
<!-- DO NOT THROW THIS AWAY -->
<!-- Fill out the FULL versions with patch versions -->

- Laravel Version: 9.0
- PHP Version: 8.1
- Database Driver & Version: MySQL

### Description:
When the email address and name are returned in `routeNotificationForMail` in a modal, the RfcComplianceException is thrown. 

    public function routeNotificationForMail($notification)  
    {  
            \Log::debug([$this->email => $this->name]);  
            return [$this->email => $this->name];
    }



Log
[2022-02-09 16:48:36] local.DEBUG: array (
  'karim.naimy@gmail.com' => 'karim',
)  
[2022-02-09 16:48:36] local.DEBUG: array (
  'karim.naimy@gmail.com' => 'karim',
)  
[2022-02-09 16:48:36] local.ERROR: Email "karim" does not comply with addr-spec of RFC 2822. {"exception":"[object] (Symfony\\Component\\Mime\\Exception\\RfcComplianceException(code: 0): Email \"karim\" does not comply with addr-spec of RFC 2822. at /home/karim/www/example-app/vendor/symfony/mime/Address.php:54)
[stacktrace]
#0 /home/karim/www/example-app/vendor/symfony/mime/Address.php(96): Symfony\\Component\\Mime\\Address->__construct()
#1 /home/karim/www/example-app/vendor/symfony/mime/Address.php(115): Symfony\\Component\\Mime\\Address::create()
#2 /home/karim/www/example-app/vendor/symfony/mime/Email.php(528): Symfony\\Component\\Mime\\Address::createArray()
#3 /home/karim/www/example-app/vendor/symfony/mime/Email.php(169): Symfony\\Component\\Mime\\Email->setListAddressHeaderBody()
#4 /home/karim/www/example-app/vendor/laravel/framework/src/Illuminate/Mail/Message.php(211): Symfony\\Component\\Mime\\Email->to()
#5 /home/karim/www/example-app/vendor/laravel/framework/src/Illuminate/Mail/Message.php(105): Illuminate\\Mail\\Message->addAddresses()
#6 /home/karim/www/example-app/vendor/laravel/framework/src/Illuminate/Notifications/Channels/MailChannel.php(163): Illuminate\\Mail\\Message->to()
#7 /home/karim/www/example-app/vendor/laravel/framework/src/Illuminate/Notifications/Channels/MailChannel.php(135): Illuminate\\Notifications\\Channels\\MailChannel->addressMessage()
#8 /home/karim/www/example-app/vendor/laravel/framework/src/Illuminate/Notifications/Channels/MailChannel.php(80): Illuminate\\Notifications\\Channels\\MailChannel->buildMessage()
#9 /home/karim/www/example-app/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(267): Illuminate\\Notifications\\Channels\\MailChannel->Illuminate\\Notifications\\Channels\\{closure}()
#10 /home/karim/www/example-app/vendor/laravel/framework/src/Illuminate/Notifications/Channels/MailChannel.php(65): Illuminate\\Mail\\Mailer->send()
#11 /home/karim/www/example-app/vendor/laravel/framework/src/Illuminate/Notifications/NotificationSender.php(148): Illuminate\\Notifications\\Channels\\MailChannel->send()
#12 /home/karim/www/example-app/vendor/laravel/framework/src/Illuminate/Notifications/NotificationSender.php(106): Illuminate\\Notifications\\NotificationSender->sendToNotifiable()
#13 /home/karim/www/example-app/vendor/laravel/framework/src/Illuminate/Support/Traits/Localizable.php(19): Illuminate\\Notifications\\NotificationSender->Illuminate\\Notifications\\{closure}()
#14 /home/karim/www/example-app/vendor/laravel/framework/src/Illuminate/Notifications/NotificationSender.php(109): Illuminate\\Notifications\\NotificationSender->withLocale()
#15 /home/karim/www/example-app/vendor/laravel/framework/src/Illuminate/Notifications/NotificationSender.php(79): Illuminate\\Notifications\\NotificationSender->sendNow()
#16 /home/karim/www/example-app/vendor/laravel/framework/src/Illuminate/Notifications/ChannelManager.php(39): Illuminate\\Notifications\\NotificationSender->send()
#17 /home/karim/www/example-app/vendor/laravel/framework/src/Illuminate/Notifications/RoutesNotifications.php(18): Illuminate\\Notifications\\ChannelManager->send()
#18 /home/karim/www/example-app/vendor/laravel/framework/src/Illuminate/Auth/Passwords/CanResetPassword.php(27): App\\Models\\User->notify()
......
#70 {main}
"} 

### Steps To Reproduce:

<!-- If possible, please provide a GitHub repository to demonstrate your issue -->
<!-- laravel new bug-report --github="--public" -->


Pull request:
[9.x] Fix notification email recipient
This PR fixes an issue where the address of a notifiable for email sending is returned as a key/value array. Fixes #40895


Patch:
diff --git a/src/Illuminate/Foundation/Exceptions/Handler.php b/src/Illuminate/Foundation/Exceptions/Handler.php
index cd4fb82f38..56eba6b3c1 100644
--- a/src/Illuminate/Foundation/Exceptions/Handler.php
+++ b/src/Illuminate/Foundation/Exceptions/Handler.php
@@ -692,6 +692,8 @@ protected function convertExceptionToArray(Throwable $e)
      * @param  \Symfony\Component\Console\Output\OutputInterface  $output
      * @param  \Throwable  $e
      * @return void
+     *
+     * @internal This method is not meant to be used or overwritten outside the framework.
      */
     public function renderForConsole($output, Throwable $e)
     {
diff --git a/src/Illuminate/Mail/Message.php b/src/Illuminate/Mail/Message.php
index 67b738a4b7..88a3fa5b05 100755
--- a/src/Illuminate/Mail/Message.php
+++ b/src/Illuminate/Mail/Message.php
@@ -200,7 +200,11 @@ protected function addAddresses($address, $name, $type)
         if (is_array($address)) {
             $type = lcfirst($type);
 
-            $addresses = collect($address)->map(function (string|array $address) {
+            $addresses = collect($address)->map(function (string|array $address, $key) {
+                if (is_string($key) && is_string($address)) {
+                    return new Address($key, $address);
+                }
+
                 if (is_array($address)) {
                     return new Address($address['email'] ?? $address['address'], $address['name'] ?? null);
                 }
diff --git a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php
index 458f91e3d0..dfa74cdfb4 100644
--- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php
+++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php
@@ -7,10 +7,10 @@
 use DateTimeInterface;
 use Egulias\EmailValidator\EmailValidator;
 use Egulias\EmailValidator\Validation\DNSCheckValidation;
+use Egulias\EmailValidator\Validation\Extra\SpoofCheckValidation;
 use Egulias\EmailValidator\Validation\MultipleValidationWithAnd;
 use Egulias\EmailValidator\Validation\NoRFCWarningsValidation;
 use Egulias\EmailValidator\Validation\RFCValidation;
-use Egulias\EmailValidator\Validation\SpoofCheckValidation;
 use Exception;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Support\Arr;


Test patch:
diff --git a/tests/Mail/MailMessageTest.php b/tests/Mail/MailMessageTest.php
index d36812492d..1756059de6 100755
--- a/tests/Mail/MailMessageTest.php
+++ b/tests/Mail/MailMessageTest.php
@@ -41,8 +41,11 @@ public function testReturnPathMethod()
 
     public function testToMethod()
     {
-        $this->assertInstanceOf(Message::class, $message = $this->message->to('foo@bar.baz', 'Foo', false));
+        $this->assertInstanceOf(Message::class, $message = $this->message->to('foo@bar.baz', 'Foo'));
         $this->assertEquals(new Address('foo@bar.baz', 'Foo'), $message->getSymfonyMessage()->getTo()[0]);
+
+        $this->assertInstanceOf(Message::class, $message = $this->message->to(['bar@bar.baz' => 'Bar']));
+        $this->assertEquals(new Address('bar@bar.baz', 'Bar'), $message->getSymfonyMessage()->getTo()[0]);
     }
 
     public function testToMethodWithOverride()