Skip to content

Commit

Permalink
Multiple tos (#22)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
ArronKing authored May 23, 2024
1 parent b1c693a commit 914c25d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/SendGridChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
21 changes: 21 additions & 0 deletions tests/SendGridChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,27 @@ public function routeNotificationForMail($notification)

$this->assertEquals($message->tos[0]->getEmail(), '[email protected]');
$this->assertEquals($message->tos[0]->getName(), 'John Doe');

//Also support multiple recipients
$notifiableWithEmailAndName = new class {
use Notifiable;

public function routeNotificationForMail($notification)
{
return [
'[email protected]' => 'John Doe',
'[email protected]' => 'Jane Doe',
];
}
};

$channel->send($notifiableWithEmailAndName, $notification);
$message = $notification->sendgridMessage;

$this->assertEquals($message->tos[0]->getEmail(), '[email protected]');
$this->assertEquals($message->tos[0]->getName(), 'John Doe');
$this->assertEquals($message->tos[1]->getEmail(), '[email protected]');
$this->assertEquals($message->tos[1]->getName(), 'Jane Doe');
}

private function mockSendgrid($statusCode = 200)
Expand Down

0 comments on commit 914c25d

Please sign in to comment.