diff --git a/src/Illuminate/Notifications/Channels/MailChannel.php b/src/Illuminate/Notifications/Channels/MailChannel.php index 80e974403d79..c6215956fe12 100644 --- a/src/Illuminate/Notifications/Channels/MailChannel.php +++ b/src/Illuminate/Notifications/Channels/MailChannel.php @@ -4,6 +4,7 @@ use Illuminate\Support\Str; use Illuminate\Contracts\Mail\Mailer; +use Illuminate\Contracts\Mail\Mailable; use Illuminate\Notifications\Notification; class MailChannel @@ -41,6 +42,12 @@ public function send($notifiable, Notification $notification) $message = $notification->toMail($notifiable); + if ($message instanceof Mailable) { + $message->send($this->mailer); + + return; + } + $this->mailer->send($message->view, $message->data(), function ($m) use ($notifiable, $notification, $message) { $recipients = empty($message->to) ? $notifiable->routeNotificationFor('mail') : $message->to; diff --git a/tests/Notifications/NotificationMailChannelTest.php b/tests/Notifications/NotificationMailChannelTest.php index 0a96dead30e1..79436c3fabf6 100644 --- a/tests/Notifications/NotificationMailChannelTest.php +++ b/tests/Notifications/NotificationMailChannelTest.php @@ -210,6 +210,20 @@ public function testMessageWithToAddress() $channel->send($notifiable, $notification); } + + public function testMessageWithMailableContract() + { + $notification = new NotificationMailChannelTestNotificationWithMailableContract; + $notifiable = new NotificationMailChannelTestNotifiable; + + $channel = new Illuminate\Notifications\Channels\MailChannel( + $mailer = Mockery::mock(Illuminate\Contracts\Mail\Mailer::class) + ); + + $mailer->shouldReceive('send')->once(); + + $channel->send($notifiable, $notification); + } } class NotificationMailChannelTestNotifiable @@ -272,3 +286,23 @@ public function toMail($notifiable) ->to('jeffrey@laracasts.com'); } } + +class NotificationMailChannelTestNotificationWithMailableContract extends Notification +{ + public function toMail($notifiable) + { + $mock = Mockery::mock(Illuminate\Contracts\Mail\Mailable::class); + + $mock->shouldReceive('send')->once()->with(Mockery::on(function ($mailer) { + if (! $mailer instanceof Illuminate\Contracts\Mail\Mailer) { + return false; + } + + $mailer->send('notifications::email-plain'); + + return true; + })); + + return $mock; + } +}