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

[9.x ] Updated Mailable to prevent duplicated recipients #45119

Merged
merged 5 commits into from
Nov 28, 2022
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
5 changes: 5 additions & 0 deletions src/Illuminate/Mail/Mailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,11 @@ protected function setAddress($address, $name = null, $property = 'to')
];
}

$this->{$property} = collect($this->{$property})
->unique('address')
->values()
->all();

return $this;
}

Expand Down
26 changes: 16 additions & 10 deletions tests/Mail/MailMailableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ public function testMailableSetsRecipientsCorrectly()
$mailable->assertHasTo('[email protected]');

$mailable = new WelcomeMailableStub;
$mailable->to(collect([new MailableTestUserStub, new MailableTestUserStub]));
$mailable->to(collect([new MailableTestUserStub, new MailableTestUserStub, new MailableTestUserStub2]));
$this->assertEquals([
['name' => 'Taylor Otwell', 'address' => '[email protected]'],
['name' => 'Taylor Otwell', 'address' => 'taylor@laravel.com'],
['name' => 'Laravel Framework', 'address' => 'contact@laravel.com'],
], $mailable->to);
$this->assertTrue($mailable->hasTo(new MailableTestUserStub));
$this->assertTrue($mailable->hasTo('[email protected]'));
Expand Down Expand Up @@ -149,10 +149,10 @@ public function testMailableSetsCcRecipientsCorrectly()
$mailable->assertHasCc('[email protected]');

$mailable = new WelcomeMailableStub;
$mailable->cc(collect([new MailableTestUserStub, new MailableTestUserStub]));
$mailable->cc(collect([new MailableTestUserStub, new MailableTestUserStub, new MailableTestUserStub2]));
$this->assertEquals([
['name' => 'Taylor Otwell', 'address' => '[email protected]'],
['name' => 'Taylor Otwell', 'address' => 'taylor@laravel.com'],
['name' => 'Laravel Framework', 'address' => 'contact@laravel.com'],
], $mailable->cc);
$this->assertTrue($mailable->hasCc(new MailableTestUserStub));
$this->assertTrue($mailable->hasCc('[email protected]'));
Expand Down Expand Up @@ -238,10 +238,10 @@ public function testMailableSetsBccRecipientsCorrectly()
$mailable->assertHasBcc('[email protected]');

$mailable = new WelcomeMailableStub;
$mailable->bcc(collect([new MailableTestUserStub, new MailableTestUserStub]));
$mailable->bcc(collect([new MailableTestUserStub, new MailableTestUserStub, new MailableTestUserStub2]));
$this->assertEquals([
['name' => 'Taylor Otwell', 'address' => '[email protected]'],
['name' => 'Taylor Otwell', 'address' => 'taylor@laravel.com'],
['name' => 'Laravel Framework', 'address' => 'contact@laravel.com'],
], $mailable->bcc);
$this->assertTrue($mailable->hasBcc(new MailableTestUserStub));
$this->assertTrue($mailable->hasBcc('[email protected]'));
Expand Down Expand Up @@ -327,10 +327,10 @@ public function testMailableSetsReplyToCorrectly()
$mailable->assertHasReplyTo('[email protected]');

$mailable = new WelcomeMailableStub;
$mailable->replyTo(collect([new MailableTestUserStub, new MailableTestUserStub]));
$mailable->replyTo(collect([new MailableTestUserStub, new MailableTestUserStub, new MailableTestUserStub2]));
$this->assertEquals([
['name' => 'Taylor Otwell', 'address' => '[email protected]'],
['name' => 'Taylor Otwell', 'address' => 'taylor@laravel.com'],
['name' => 'Laravel Framework', 'address' => 'contact@laravel.com'],
], $mailable->replyTo);
$this->assertTrue($mailable->hasReplyTo(new MailableTestUserStub));
$this->assertTrue($mailable->hasReplyTo('[email protected]'));
Expand Down Expand Up @@ -405,10 +405,10 @@ public function testMailableSetsFromCorrectly()
$mailable->assertFrom('[email protected]');

$mailable = new WelcomeMailableStub;
$mailable->from(collect([new MailableTestUserStub, new MailableTestUserStub]));
$mailable->from(collect([new MailableTestUserStub, new MailableTestUserStub, new MailableTestUserStub2]));
$this->assertEquals([
['name' => 'Taylor Otwell', 'address' => '[email protected]'],
['name' => 'Taylor Otwell', 'address' => 'taylor@laravel.com'],
['name' => 'Laravel Framework', 'address' => 'contact@laravel.com'],
], $mailable->from);
$this->assertTrue($mailable->hasFrom(new MailableTestUserStub));
$this->assertTrue($mailable->hasFrom('[email protected]'));
Expand Down Expand Up @@ -1018,6 +1018,12 @@ class MailableTestUserStub
public $email = '[email protected]';
}

class MailableTestUserStub2
{
public $name = 'Laravel Framework';
public $email = '[email protected]';
}

class MailTestAttachable implements Attachable
{
public function __construct(protected $attachment)
Expand Down