Skip to content

Commit

Permalink
[5.3] Add cc emails in mail notifications (laravel#16152)
Browse files Browse the repository at this point in the history
* Add cc emails in mail notifications

* Apply StyleCI formatting
  • Loading branch information
shadoWalker89 authored and taylorotwell committed Oct 28, 2016
1 parent e5697d5 commit 266a986
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Illuminate/Notifications/Channels/MailChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public function send($notifiable, Notification $notification)
$m->to($recipients);
}

if ($message->cc) {
$m->cc($message->cc);
}

$m->subject($message->subject ?: Str::title(
Str::snake(class_basename($notification), ' ')
));
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 @@ -35,6 +35,13 @@ class MailMessage extends SimpleMessage
*/
public $to = [];

/**
* The "cc" recipients of the message.
*
* @var array
*/
public $cc = [];

/**
* The attachments for the message.
*
Expand Down Expand Up @@ -98,6 +105,19 @@ public function to($address)
return $this;
}

/**
* Set the recipients of the message.
*
* @param string|array $address
* @return $this
*/
public function cc($address)
{
$this->cc = $address;

return $this;
}

/**
* Attach a file to the message.
*
Expand Down
40 changes: 40 additions & 0 deletions tests/Notifications/NotificationMailChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,37 @@ public function testMessageWithToAddress()
$channel->send($notifiable, $notification);
}

public function testMessageWithToCcEmails()
{
$notification = new NotificationMailChannelTestNotificationWithCcEmails;
$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]');

$mock->shouldReceive('cc')->once()->with(['[email protected]', '[email protected]']);

$closure($mock);

return true;
}));

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

public function testMessageWithPriority()
{
$notification = new NotificationMailChannelTestNotificationWithPriority;
Expand Down Expand Up @@ -318,6 +349,15 @@ public function toMail($notifiable)
}
}

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

class NotificationMailChannelTestNotificationWithPriority extends Notification
{
public function toMail($notifiable)
Expand Down

0 comments on commit 266a986

Please sign in to comment.