Skip to content

Commit

Permalink
Support custom to address on MailMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
tillkruss committed Aug 27, 2016
1 parent 3f535b9 commit e6063d7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Notifications/Channels/MailChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function send($notifiable, Notification $notification)
$message = $notification->toMail($notifiable);

$this->mailer->send($message->view, $message->data(), function ($m) use ($notifiable, $notification, $message) {
$recipients = $notifiable->routeNotificationFor('mail');
$recipients = empty($message->to) ? $notifiable->routeNotificationFor('mail') : $message->to;

if (! empty($message->from)) {
$m->from($message->from[0], isset($message->from[1]) ? $message->from[1] : null);
Expand Down
20 changes: 20 additions & 0 deletions src/Illuminate/Notifications/Messages/MailMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ class MailMessage extends SimpleMessage
*/
public $from = [];

/**
* The recipient information for the message.
*
* @var array
*/
public $to = [];

/**
* The attachments for the message.
*
Expand Down Expand Up @@ -71,6 +78,19 @@ public function from($address, $name = null)
return $this;
}

/**
* Set the recipient address for the mail message.
*
* @param string|array $address
* @return $this
*/
public function to($address)
{
$this->to = $address;

return $this;
}

/**
* Attach a file to the message.
*
Expand Down
38 changes: 38 additions & 0 deletions tests/Notifications/NotificationMailChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,35 @@ public function testMessageWithFromAddressAndNoName()

$channel->send($notifiable, $notification);
}

public function testMessageWithToAddress()
{
$notification = new NotificationMailChannelTestNotificationWithToAddress;
$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('[email protected]');

$closure($mock);

return true;
}));

$channel->send($notifiable, $notification);
}
}

class NotificationMailChannelTestNotifiable
Expand Down Expand Up @@ -234,3 +263,12 @@ public function toMail($notifiable)
->from('[email protected]');
}
}

class NotificationMailChannelTestNotificationWithToAddress extends Notification
{
public function toMail($notifiable)
{
return (new MailMessage)
->to('[email protected]');
}
}

0 comments on commit e6063d7

Please sign in to comment.