From 484cfcbdb381e8f36510030b086884e49b898e87 Mon Sep 17 00:00:00 2001 From: sald19 Date: Fri, 4 Nov 2016 23:32:04 -0600 Subject: [PATCH] add ability to set replyTo address Mail channel, add test replyto --- .../Notifications/Channels/MailChannel.php | 4 + .../Notifications/Messages/MailMessage.php | 21 +++++ .../NotificationMailChannelTest.php | 80 +++++++++++++++++++ 3 files changed, 105 insertions(+) diff --git a/src/Illuminate/Notifications/Channels/MailChannel.php b/src/Illuminate/Notifications/Channels/MailChannel.php index 5b324c0b13e7..558c183fae7a 100644 --- a/src/Illuminate/Notifications/Channels/MailChannel.php +++ b/src/Illuminate/Notifications/Channels/MailChannel.php @@ -63,6 +63,10 @@ public function send($notifiable, Notification $notification) $m->cc($message->cc); } + if (! empty($message->replyTo)) { + $m->replyTo($message->replyTo[0], isset($message->replyTo[1]) ? $message->replyTo[1] : null); + } + $m->subject($message->subject ?: Str::title( Str::snake(class_basename($notification), ' ') )); diff --git a/src/Illuminate/Notifications/Messages/MailMessage.php b/src/Illuminate/Notifications/Messages/MailMessage.php index b76e295d8f8c..2c51596c180b 100644 --- a/src/Illuminate/Notifications/Messages/MailMessage.php +++ b/src/Illuminate/Notifications/Messages/MailMessage.php @@ -42,6 +42,13 @@ class MailMessage extends SimpleMessage */ public $cc = []; + /** + * The "reply to" recipients of the message. + * + * @var array + */ + public $replyTo= []; + /** * The attachments for the message. * @@ -118,6 +125,20 @@ public function cc($address) return $this; } + /** + * Set the "reply to" address of the message. + * + * @param array|string $address + * @param null $name + * @return $this + */ + public function replyTo($address, $name = null) + { + $this->replyTo = [$address, $name]; + + return $this; + } + /** * Attach a file to the message. * diff --git a/tests/Notifications/NotificationMailChannelTest.php b/tests/Notifications/NotificationMailChannelTest.php index a7f5c6d1a9ae..4e6995692e17 100644 --- a/tests/Notifications/NotificationMailChannelTest.php +++ b/tests/Notifications/NotificationMailChannelTest.php @@ -211,6 +211,68 @@ public function testMessageWithToAddress() $channel->send($notifiable, $notification); } + public function testMessageWithReplyToAddress() + { + $notification = new NotificationMailChannelTestNotificationWithReplyToAddress; + $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(); + + $mock->shouldReceive('replyTo')->with('test@mail.com', 'Test Man'); + + $closure($mock); + + return true; + })); + + $channel->send($notifiable, $notification); + } + + public function testMessageWithReplyToAddressAndNoName() + { + $notification = new NotificationMailChannelTestNotificationWithReplyToAddressNoName; + $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(); + + $mock->shouldReceive('replyTo')->with('test@mail.com', null); + + $closure($mock); + + return true; + })); + + $channel->send($notifiable, $notification); + } + public function testMessageWithToCcEmails() { $notification = new NotificationMailChannelTestNotificationWithCcEmails; @@ -358,6 +420,24 @@ public function toMail($notifiable) } } +class NotificationMailChannelTestNotificationWithReplyToAddress extends Notification +{ + public function toMail($notifiable) + { + return (new MailMessage) + ->replyTo('test@mail.com', 'Test Man'); + } +} + +class NotificationMailChannelTestNotificationWithReplyToAddressNoName extends Notification +{ + public function toMail($notifiable) + { + return (new MailMessage) + ->replyTo('test@mail.com'); + } +} + class NotificationMailChannelTestNotificationWithPriority extends Notification { public function toMail($notifiable)