From adea0c612005361fb58ea800bb9eee835d95a4ab Mon Sep 17 00:00:00 2001 From: Arron King Date: Tue, 21 May 2024 16:26:44 +0100 Subject: [PATCH 1/2] Add another assertion to check we can handle multiple tos returned by routeNotificationForMail --- tests/SendGridChannelTest.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/SendGridChannelTest.php b/tests/SendGridChannelTest.php index b12089b..ed41696 100644 --- a/tests/SendGridChannelTest.php +++ b/tests/SendGridChannelTest.php @@ -137,6 +137,27 @@ public function routeNotificationForMail($notification) $this->assertEquals($message->tos[0]->getEmail(), 'john@example.com'); $this->assertEquals($message->tos[0]->getName(), 'John Doe'); + + //Also support multiple recipients + $notifiableWithEmailAndName = new class { + use Notifiable; + + public function routeNotificationForMail($notification) + { + return [ + 'john@example.com' => 'John Doe', + 'jane@example.com' => 'Jane Doe', + ]; + } + }; + + $channel->send($notifiableWithEmailAndName, $notification); + $message = $notification->sendgridMessage; + + $this->assertEquals($message->tos[0]->getEmail(), 'john@example.com'); + $this->assertEquals($message->tos[0]->getName(), 'John Doe'); + $this->assertEquals($message->tos[1]->getEmail(), 'jane@example.com'); + $this->assertEquals($message->tos[1]->getName(), 'Jane Doe'); } private function mockSendgrid($statusCode = 200) From 9c6adf629e691dc10a903db4291105d3793bf457 Mon Sep 17 00:00:00 2001 From: Arron King Date: Tue, 21 May 2024 16:27:12 +0100 Subject: [PATCH 2/2] Change the way we build $message->to - use a foreach to add each recipient. --- src/SendGridChannel.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/SendGridChannel.php b/src/SendGridChannel.php index cef541b..e1f245c 100644 --- a/src/SendGridChannel.php +++ b/src/SendGridChannel.php @@ -50,8 +50,9 @@ public function send($notifiable, Notification $notification) // Handle the case where routeNotificationForMail returns an array (email => name) if (is_array($to)) { - reset($to); - $message->to(key($to), current($to)); + foreach ($to as $email => $name) { + $message->to($email, $name); + } } else { $message->to($to); }