Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.3] Support custom recipient(s) with MailMessage #15100

Merged
merged 1 commit into from
Aug 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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('jeffrey@laracasts.com');

$closure($mock);

return true;
}));

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

class NotificationMailChannelTestNotifiable
Expand Down Expand Up @@ -234,3 +263,12 @@ public function toMail($notifiable)
->from('test@mail.com');
}
}

class NotificationMailChannelTestNotificationWithToAddress extends Notification
{
public function toMail($notifiable)
{
return (new MailMessage)
->to('jeffrey@laracasts.com');
}
}