Skip to content

Commit fb29b38

Browse files
committed
Make has recipient checking more robust.
1 parent 3fc36e2 commit fb29b38

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

src/Illuminate/Mail/Mailable.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,18 @@ public function to($address, $name = null)
340340
return $this->setAddress($address, $name, 'to');
341341
}
342342

343+
/**
344+
* Determine if the given recipient is set on the mailable.
345+
*
346+
* @param object|array|string $address
347+
* @param string|null $name
348+
* @return bool
349+
*/
350+
public function hasTo($address, $name = null)
351+
{
352+
return $this->hasRecipient($address, $name, 'to');
353+
}
354+
343355
/**
344356
* Set the recipients of the message.
345357
*
@@ -352,6 +364,18 @@ public function cc($address, $name = null)
352364
return $this->setAddress($address, $name, 'cc');
353365
}
354366

367+
/**
368+
* Determine if the given recipient is set on the mailable.
369+
*
370+
* @param object|array|string $address
371+
* @param string|null $name
372+
* @return bool
373+
*/
374+
public function hasCc($address, $name = null)
375+
{
376+
return $this->hasRecipient($address, $name, 'cc');
377+
}
378+
355379
/**
356380
* Set the recipients of the message.
357381
*
@@ -364,6 +388,18 @@ public function bcc($address, $name = null)
364388
return $this->setAddress($address, $name, 'bcc');
365389
}
366390

391+
/**
392+
* Determine if the given recipient is set on the mailable.
393+
*
394+
* @param object|array|string $address
395+
* @param string|null $name
396+
* @return bool
397+
*/
398+
public function hasBcc($address, $name = null)
399+
{
400+
return $this->hasRecipient($address, $name, 'bcc');
401+
}
402+
367403
/**
368404
* Set the "reply to" address of the message.
369405
*
@@ -433,6 +469,34 @@ protected function normalizeRecipient($recipient)
433469
return $recipient;
434470
}
435471

472+
/**
473+
* Determine if the given recipient is set on the mailable.
474+
*
475+
* @param object|array|string $address
476+
* @param string|null $name
477+
* @param string $property
478+
* @return bool
479+
*/
480+
protected function hasRecipient($address, $name = null, $property = 'to')
481+
{
482+
$expected = $this->normalizeRecipient(
483+
$this->addressesToArray($address, $name)[0]
484+
);
485+
486+
$expected = [
487+
'name' => isset($expected->name) ? $expected->name : null,
488+
'address' => $expected->email,
489+
];
490+
491+
return collect($this->{$property})->contains(function ($actual) use ($expected) {
492+
if (! isset($expected['name'])) {
493+
return $actual['address'] == $expected['address'];
494+
} else {
495+
return $actual == $expected;
496+
}
497+
});
498+
}
499+
436500
/**
437501
* Set the subject of the message.
438502
*

tests/Mail/MailMailableTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,46 @@ public function testMailableSetsRecipientsCorrectly()
99
$mailable = new WelcomeMailableStub;
1010
$mailable->to('[email protected]');
1111
$this->assertEquals([['name' => null, 'address' => '[email protected]']], $mailable->to);
12+
$this->assertTrue($mailable->hasTo('[email protected]'));
1213

1314
$mailable = new WelcomeMailableStub;
1415
$mailable->to('[email protected]', 'Taylor Otwell');
1516
$this->assertEquals([['name' => 'Taylor Otwell', 'address' => '[email protected]']], $mailable->to);
17+
$this->assertTrue($mailable->hasTo('[email protected]', 'Taylor Otwell'));
18+
$this->assertTrue($mailable->hasTo('[email protected]'));
1619

1720
$mailable = new WelcomeMailableStub;
1821
$mailable->to(['[email protected]']);
1922
$this->assertEquals([['name' => null, 'address' => '[email protected]']], $mailable->to);
23+
$this->assertTrue($mailable->hasTo('[email protected]'));
24+
$this->assertFalse($mailable->hasTo('[email protected]', 'Taylor Otwell'));
2025

2126
$mailable = new WelcomeMailableStub;
2227
$mailable->to([['name' => 'Taylor Otwell', 'email' => '[email protected]']]);
2328
$this->assertEquals([['name' => 'Taylor Otwell', 'address' => '[email protected]']], $mailable->to);
29+
$this->assertTrue($mailable->hasTo('[email protected]', 'Taylor Otwell'));
30+
$this->assertTrue($mailable->hasTo('[email protected]'));
2431

2532
$mailable = new WelcomeMailableStub;
2633
$mailable->to(new MailableTestUserStub);
2734
$this->assertEquals([['name' => 'Taylor Otwell', 'address' => '[email protected]']], $mailable->to);
35+
$this->assertTrue($mailable->hasTo(new MailableTestUserStub));
36+
$this->assertTrue($mailable->hasTo('[email protected]'));
2837

2938
$mailable = new WelcomeMailableStub;
3039
$mailable->to(collect([new MailableTestUserStub]));
3140
$this->assertEquals([['name' => 'Taylor Otwell', 'address' => '[email protected]']], $mailable->to);
41+
$this->assertTrue($mailable->hasTo(new MailableTestUserStub));
42+
$this->assertTrue($mailable->hasTo('[email protected]'));
3243

3344
$mailable = new WelcomeMailableStub;
3445
$mailable->to(collect([new MailableTestUserStub, new MailableTestUserStub]));
3546
$this->assertEquals([
3647
['name' => 'Taylor Otwell', 'address' => '[email protected]'],
3748
['name' => 'Taylor Otwell', 'address' => '[email protected]'],
3849
], $mailable->to);
50+
$this->assertTrue($mailable->hasTo(new MailableTestUserStub));
51+
$this->assertTrue($mailable->hasTo('[email protected]'));
3952
}
4053

4154
public function testMailableBuildsViewData()

0 commit comments

Comments
 (0)