From 376c0a9c1578250803191c72b7fb1c035cea50f5 Mon Sep 17 00:00:00 2001 From: Kuba Szymanowski Date: Wed, 28 Sep 2016 18:53:36 +0200 Subject: [PATCH] Add support for setting mail notification priority. --- .../Notifications/Channels/MailChannel.php | 4 ++ .../Notifications/Messages/MailMessage.php | 22 ++++++++++ .../NotificationMailChannelTest.php | 40 +++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/src/Illuminate/Notifications/Channels/MailChannel.php b/src/Illuminate/Notifications/Channels/MailChannel.php index 2604c3fb2c02..71e91a2c126f 100644 --- a/src/Illuminate/Notifications/Channels/MailChannel.php +++ b/src/Illuminate/Notifications/Channels/MailChannel.php @@ -70,6 +70,10 @@ public function send($notifiable, Notification $notification) foreach ($message->rawAttachments as $attachment) { $m->attachData($attachment['data'], $attachment['name'], $attachment['options']); } + + if($message->priority !== null) { + $m->setPriority($message->priority); + } }); } } diff --git a/src/Illuminate/Notifications/Messages/MailMessage.php b/src/Illuminate/Notifications/Messages/MailMessage.php index e9ce0a66d1ab..365e302c9b72 100644 --- a/src/Illuminate/Notifications/Messages/MailMessage.php +++ b/src/Illuminate/Notifications/Messages/MailMessage.php @@ -49,6 +49,13 @@ class MailMessage extends SimpleMessage */ public $rawAttachments = []; + /** + * Priority level of the message. + * + * @var int + */ + public $priority = null; + /** * Set the view for the mail message. * @@ -129,4 +136,19 @@ public function data() { return array_merge($this->toArray(), $this->viewData); } + + /** + * Set the priority of this message. + * + * The value is an integer where 1 is the highest priority and 5 is the lowest. + * + * @param int $level + * @return $this + */ + public function priority($level) + { + $this->priority = $level; + + return $this; + } } diff --git a/tests/Notifications/NotificationMailChannelTest.php b/tests/Notifications/NotificationMailChannelTest.php index 79436c3fabf6..33dba51235fe 100644 --- a/tests/Notifications/NotificationMailChannelTest.php +++ b/tests/Notifications/NotificationMailChannelTest.php @@ -211,6 +211,37 @@ public function testMessageWithToAddress() $channel->send($notifiable, $notification); } + public function testMessageWithPriority() + { + $notification = new NotificationMailChannelTestNotificationWithPriority; + $notifiable = new NotificationMailChannelTestNotifiable; + + $message = $notification->toMail($notifiable); + $data = $message->toArray(); + + $channel = new Illuminate\Notifications\Channels\MailChannel( + $mailer = Mockery::mock(Illuminate\Contracts\Mail\Mailer::class) + ); + + $views = ['notifications::email', 'notifications::email-plain']; + + $mailer->shouldReceive('send')->with($views, $data, Mockery::on(function ($closure) { + $mock = Mockery::mock('Illuminate\Mailer\Message'); + + $mock->shouldReceive('subject')->once(); + + $mock->shouldReceive('to')->once()->with('taylor@laravel.com'); + + $mock->shouldReceive('setPriority')->once()->with(1); + + $closure($mock); + + return true; + })); + + $channel->send($notifiable, $notification); + } + public function testMessageWithMailableContract() { $notification = new NotificationMailChannelTestNotificationWithMailableContract; @@ -287,6 +318,15 @@ public function toMail($notifiable) } } +class NotificationMailChannelTestNotificationWithPriority extends Notification +{ + public function toMail($notifiable) + { + return (new MailMessage) + ->priority(1); + } +} + class NotificationMailChannelTestNotificationWithMailableContract extends Notification { public function toMail($notifiable)