Skip to content

Commit 914c25d

Browse files
authored
Multiple tos (#22)
* Add another assertion to check we can handle multiple tos returned by routeNotificationForMail * Change the way we build $message->to - use a foreach to add each recipient.
1 parent b1c693a commit 914c25d

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/SendGridChannel.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ public function send($notifiable, Notification $notification)
5050

5151
// Handle the case where routeNotificationForMail returns an array (email => name)
5252
if (is_array($to)) {
53-
reset($to);
54-
$message->to(key($to), current($to));
53+
foreach ($to as $email => $name) {
54+
$message->to($email, $name);
55+
}
5556
} else {
5657
$message->to($to);
5758
}

tests/SendGridChannelTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,27 @@ public function routeNotificationForMail($notification)
137137

138138
$this->assertEquals($message->tos[0]->getEmail(), 'john@example.com');
139139
$this->assertEquals($message->tos[0]->getName(), 'John Doe');
140+
141+
//Also support multiple recipients
142+
$notifiableWithEmailAndName = new class {
143+
use Notifiable;
144+
145+
public function routeNotificationForMail($notification)
146+
{
147+
return [
148+
'john@example.com' => 'John Doe',
149+
'jane@example.com' => 'Jane Doe',
150+
];
151+
}
152+
};
153+
154+
$channel->send($notifiableWithEmailAndName, $notification);
155+
$message = $notification->sendgridMessage;
156+
157+
$this->assertEquals($message->tos[0]->getEmail(), 'john@example.com');
158+
$this->assertEquals($message->tos[0]->getName(), 'John Doe');
159+
$this->assertEquals($message->tos[1]->getEmail(), 'jane@example.com');
160+
$this->assertEquals($message->tos[1]->getName(), 'Jane Doe');
140161
}
141162

142163
private function mockSendgrid($statusCode = 200)

0 commit comments

Comments
 (0)