diff --git a/src/Illuminate/Notifications/Channels/MailChannel.php b/src/Illuminate/Notifications/Channels/MailChannel.php index 0e1fb25c056b..dd934defa101 100644 --- a/src/Illuminate/Notifications/Channels/MailChannel.php +++ b/src/Illuminate/Notifications/Channels/MailChannel.php @@ -50,6 +50,10 @@ public function send($notifiable, Notification $notification) $m->to($recipients); } + if (! empty($message->from)) { + $m->from($message->from[0], isset($message->from[1]) ? $message->from[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 ff45a67791a8..d8d07b689a51 100644 --- a/src/Illuminate/Notifications/Messages/MailMessage.php +++ b/src/Illuminate/Notifications/Messages/MailMessage.php @@ -11,6 +11,13 @@ class MailMessage extends SimpleMessage */ public $view = 'notifications::email'; + /** + * The from address for the message. + * + * @var array + */ + public $from = []; + /** * The view data for the message. * @@ -47,6 +54,19 @@ public function view($view, array $data = []) return $this; } + /** + * Set the from address for the mail message. + * + * @param string $address + * @return $this + */ + public function from($address, $name = null) + { + $this->from = [$address, $name]; + + return $this; + } + /** * Attach a file to the message. * diff --git a/tests/Notifications/NotificationMailChannelTest.php b/tests/Notifications/NotificationMailChannelTest.php index d36ea8923585..7e18db7c0ef0 100644 --- a/tests/Notifications/NotificationMailChannelTest.php +++ b/tests/Notifications/NotificationMailChannelTest.php @@ -26,6 +26,149 @@ public function testMailIsSentByChannel() $channel->send($notifiable, $notification); } + + public function testMessageWithSubject() + { + $notification = new NotificationMailChannelTestNotification; + $notifiable = new NotificationMailChannelTestNotifiable; + + $message = $notification->toMail($notifiable); + $data = $message->toArray(); + + $channel = new Illuminate\Notifications\Channels\MailChannel( + $mailer = Mockery::mock(Illuminate\Contracts\Mail\Mailer::class) + ); + + $mailer->shouldReceive('send')->with('notifications::email', $data, Mockery::on(function ($closure) { + $mock = Mockery::mock('Illuminate\Mailer\Message'); + + $mock->shouldReceive('subject')->once()->with('test subject'); + + $mock->shouldReceive('to')->once()->with('taylor@laravel.com'); + + $mock->shouldReceive('from')->never(); + + $closure($mock); + + return true; + })); + + $channel->send($notifiable, $notification); + } + + public function testMessageWithoutSubjectAutogeneratesSubjectFromClassName() + { + $notification = new NotificationMailChannelTestNotificationNoSubject; + $notifiable = new NotificationMailChannelTestNotifiable; + + $message = $notification->toMail($notifiable); + $data = $message->toArray(); + + $channel = new Illuminate\Notifications\Channels\MailChannel( + $mailer = Mockery::mock(Illuminate\Contracts\Mail\Mailer::class) + ); + + $mailer->shouldReceive('send')->with('notifications::email', $data, Mockery::on(function ($closure) { + $mock = Mockery::mock('Illuminate\Mailer\Message'); + + $mock->shouldReceive('subject')->once()->with('Notification Mail Channel Test Notification No Subject'); + + $mock->shouldReceive('to')->once()->with('taylor@laravel.com'); + + $closure($mock); + + return true; + })); + + $channel->send($notifiable, $notification); + } + + public function testMessageWithMultipleSenders() + { + $notification = new NotificationMailChannelTestNotification; + $notifiable = new NotificationMailChannelTestNotifiableMultipleEmails; + + $message = $notification->toMail($notifiable); + $data = $message->toArray(); + + $channel = new Illuminate\Notifications\Channels\MailChannel( + $mailer = Mockery::mock(Illuminate\Contracts\Mail\Mailer::class) + ); + + $mailer->shouldReceive('send')->with('notifications::email', $data, Mockery::on(function ($closure) { + $mock = Mockery::mock('Illuminate\Mailer\Message'); + + $mock->shouldReceive('subject')->once(); + + $mock->shouldReceive('to')->never(); + + $mock->shouldReceive('bcc')->with(['taylor@laravel.com', 'jeffrey@laracasts.com']); + + $closure($mock); + + return true; + })); + + $channel->send($notifiable, $notification); + } + + public function testMessageWithFromAddress() + { + $notification = new NotificationMailChannelTestNotificationWithFromAddress; + $notifiable = new NotificationMailChannelTestNotifiable; + + $message = $notification->toMail($notifiable); + $data = $message->toArray(); + + $channel = new Illuminate\Notifications\Channels\MailChannel( + $mailer = Mockery::mock(Illuminate\Contracts\Mail\Mailer::class) + ); + + $mailer->shouldReceive('send')->with('notifications::email', $data, Mockery::on(function ($closure) { + $mock = Mockery::mock('Illuminate\Mailer\Message'); + + $mock->shouldReceive('subject')->once(); + + $mock->shouldReceive('to')->once(); + + $mock->shouldReceive('from')->with('test@mail.com', 'Test Man'); + + $closure($mock); + + return true; + })); + + $channel->send($notifiable, $notification); + } + + public function testMessageWithFromAddressAndNoName() + { + $notification = new NotificationMailChannelTestNotificationWithFromAddressNoName; + $notifiable = new NotificationMailChannelTestNotifiable; + + $message = $notification->toMail($notifiable); + $data = $message->toArray(); + + $channel = new Illuminate\Notifications\Channels\MailChannel( + $mailer = Mockery::mock(Illuminate\Contracts\Mail\Mailer::class) + ); + + $mailer->shouldReceive('send')->with('notifications::email', $data, Mockery::on(function ($closure) { + $mock = Mockery::mock('Illuminate\Mailer\Message'); + + $mock->shouldReceive('subject')->once(); + + $mock->shouldReceive('to')->once(); + + $mock->shouldReceive('from')->with('test@mail.com', null); + + $closure($mock); + + return true; + })); + + $channel->send($notifiable, $notification); + } } class NotificationMailChannelTestNotifiable @@ -35,10 +178,47 @@ class NotificationMailChannelTestNotifiable public $email = 'taylor@laravel.com'; } +class NotificationMailChannelTestNotifiableMultipleEmails +{ + use Illuminate\Notifications\Notifiable; + + public function routeNotificationForMail() + { + return ['taylor@laravel.com', 'jeffrey@laracasts.com']; + } +} + class NotificationMailChannelTestNotification extends Notification +{ + public function toMail($notifiable) + { + return (new MailMessage) + ->subject('test subject'); + } +} + +class NotificationMailChannelTestNotificationNoSubject extends Notification { public function toMail($notifiable) { return new MailMessage; } } + +class NotificationMailChannelTestNotificationWithFromAddress extends Notification +{ + public function toMail($notifiable) + { + return (new MailMessage) + ->from('test@mail.com', 'Test Man'); + } +} + +class NotificationMailChannelTestNotificationWithFromAddressNoName extends Notification +{ + public function toMail($notifiable) + { + return (new MailMessage) + ->from('test@mail.com'); + } +}