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.6] Allow passing of recipient name in Mail notifications #24606

Merged
merged 3 commits into from
Jun 15, 2018
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
8 changes: 6 additions & 2 deletions src/Illuminate/Notifications/Channels/MailChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,12 @@ protected function getRecipients($notifiable, $notification, $message)
$recipients = [$recipients];
}

return collect($recipients)->map(function ($recipient) {
return is_string($recipient) ? $recipient : $recipient->email;
return collect($recipients)->mapWithKeys(function ($recipient, $email) {
if (! is_numeric($email)) {
return [$email => $recipient];
}

return [(is_string($recipient) ? $recipient : $recipient->email)];
})->all();
}

Expand Down
53 changes: 53 additions & 0 deletions tests/Integration/Notifications/SendingMailNotificationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function setUp()
Schema::create('users', function ($table) {
$table->increments('id');
$table->string('email');
$table->string('name')->nullable();
});
}

Expand Down Expand Up @@ -102,6 +103,47 @@ public function test_mail_is_sent()
$user->notify($notification);
}

public function test_mail_is_sent_to_named_address()
{
$notification = new TestMailNotification;

$user = NotifiableUserWithNamedAddress::forceCreate([
'email' => 'taylor@laravel.com',
'name' => 'Taylor Otwell',
]);

$this->markdown->shouldReceive('render')->once()->andReturn('htmlContent');
$this->markdown->shouldReceive('renderText')->once()->andReturn('textContent');

$this->mailer->shouldReceive('send')->once()->with(
['html' => 'htmlContent', 'text' => 'textContent'],
$notification->toMail($user)->toArray(),
Mockery::on(function ($closure) {
$message = Mockery::mock(\Illuminate\Mail\Message::class);

$message->shouldReceive('to')->once()->with(['taylor@laravel.com' => 'Taylor Otwell', 'foo_taylor@laravel.com']);

$message->shouldReceive('cc')->once()->with('cc@deepblue.com', 'cc');

$message->shouldReceive('bcc')->once()->with('bcc@deepblue.com', 'bcc');

$message->shouldReceive('from')->once()->with('jack@deepblue.com', 'Jacques Mayol');

$message->shouldReceive('replyTo')->once()->with('jack@deepblue.com', 'Jacques Mayol');

$message->shouldReceive('subject')->once()->with('Test Mail Notification');

$message->shouldReceive('setPriority')->once()->with(1);

$closure($message);

return true;
})
);

$user->notify($notification);
}

public function test_mail_is_sent_with_subject()
{
$notification = new TestMailNotificationWithSubject;
Expand Down Expand Up @@ -152,6 +194,17 @@ class NotifiableUser extends Model
public $timestamps = false;
}

class NotifiableUserWithNamedAddress extends NotifiableUser
{
public function routeNotificationForMail($notification)
{
return [
$this->email => $this->name,
'foo_'.$this->email,
];
}
}

class TestMailNotification extends Notification
{
public function via($notifiable)
Expand Down