Skip to content

Commit 4de8ef4

Browse files
[8.x] Mail empty address handling (#39035)
* Handle empty address inputs cleanly * Tests for empty mail inputs * Tests for from address * Update Mailable.php * Think this is simpler * DRY, also test `false` Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 5588c7a commit 4de8ef4

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

src/Illuminate/Mail/Mailable.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,10 @@ public function hasReplyTo($address, $name = null)
618618
*/
619619
protected function setAddress($address, $name = null, $property = 'to')
620620
{
621+
if (empty($address)) {
622+
return $this;
623+
}
624+
621625
foreach ($this->addressesToArray($address, $name) as $recipient) {
622626
$recipient = $this->normalizeRecipient($recipient);
623627

@@ -679,6 +683,10 @@ protected function normalizeRecipient($recipient)
679683
*/
680684
protected function hasRecipient($address, $name = null, $property = 'to')
681685
{
686+
if (empty($address)) {
687+
return false;
688+
}
689+
682690
$expected = $this->normalizeRecipient(
683691
$this->addressesToArray($address, $name)[0]
684692
);

tests/Mail/MailMailableTest.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ public function testMailableSetsRecipientsCorrectly()
5252
], $mailable->to);
5353
$this->assertTrue($mailable->hasTo(new MailableTestUserStub));
5454
$this->assertTrue($mailable->hasTo('taylor@laravel.com'));
55+
56+
foreach (['', null, [], false] as $address) {
57+
$mailable = new WelcomeMailableStub;
58+
$mailable->to($address);
59+
$this->assertFalse($mailable->hasTo(new MailableTestUserStub));
60+
$this->assertFalse($mailable->hasTo($address));
61+
}
5562
}
5663

5764
public function testMailableSetsCcRecipientsCorrectly()
@@ -108,6 +115,13 @@ public function testMailableSetsCcRecipientsCorrectly()
108115
], $mailable->cc);
109116
$this->assertTrue($mailable->hasCc('taylor@laravel.com'));
110117
$this->assertTrue($mailable->hasCc('not-taylor@laravel.com'));
118+
119+
foreach (['', null, [], false] as $address) {
120+
$mailable = new WelcomeMailableStub;
121+
$mailable->cc($address);
122+
$this->assertFalse($mailable->hasCc(new MailableTestUserStub));
123+
$this->assertFalse($mailable->hasCc($address));
124+
}
111125
}
112126

113127
public function testMailableSetsBccRecipientsCorrectly()
@@ -164,6 +178,13 @@ public function testMailableSetsBccRecipientsCorrectly()
164178
], $mailable->bcc);
165179
$this->assertTrue($mailable->hasBcc('taylor@laravel.com'));
166180
$this->assertTrue($mailable->hasBcc('not-taylor@laravel.com'));
181+
182+
foreach (['', null, [], false] as $address) {
183+
$mailable = new WelcomeMailableStub;
184+
$mailable->bcc($address);
185+
$this->assertFalse($mailable->hasBcc(new MailableTestUserStub));
186+
$this->assertFalse($mailable->hasBcc($address));
187+
}
167188
}
168189

169190
public function testMailableSetsReplyToCorrectly()
@@ -211,6 +232,67 @@ public function testMailableSetsReplyToCorrectly()
211232
], $mailable->replyTo);
212233
$this->assertTrue($mailable->hasReplyTo(new MailableTestUserStub));
213234
$this->assertTrue($mailable->hasReplyTo('taylor@laravel.com'));
235+
236+
foreach (['', null, [], false] as $address) {
237+
$mailable = new WelcomeMailableStub;
238+
$mailable->replyTo($address);
239+
$this->assertFalse($mailable->hasReplyTo(new MailableTestUserStub));
240+
$this->assertFalse($mailable->hasReplyTo($address));
241+
}
242+
}
243+
244+
public function testMailableSetsFromCorrectly()
245+
{
246+
$mailable = new WelcomeMailableStub;
247+
$mailable->from('taylor@laravel.com');
248+
$this->assertEquals([['name' => null, 'address' => 'taylor@laravel.com']], $mailable->from);
249+
$this->assertTrue($mailable->hasFrom('taylor@laravel.com'));
250+
251+
$mailable = new WelcomeMailableStub;
252+
$mailable->from('taylor@laravel.com', 'Taylor Otwell');
253+
$this->assertEquals([['name' => 'Taylor Otwell', 'address' => 'taylor@laravel.com']], $mailable->from);
254+
$this->assertTrue($mailable->hasFrom('taylor@laravel.com', 'Taylor Otwell'));
255+
$this->assertTrue($mailable->hasFrom('taylor@laravel.com'));
256+
257+
$mailable = new WelcomeMailableStub;
258+
$mailable->from(['taylor@laravel.com']);
259+
$this->assertEquals([['name' => null, 'address' => 'taylor@laravel.com']], $mailable->from);
260+
$this->assertTrue($mailable->hasFrom('taylor@laravel.com'));
261+
$this->assertFalse($mailable->hasFrom('taylor@laravel.com', 'Taylor Otwell'));
262+
263+
$mailable = new WelcomeMailableStub;
264+
$mailable->from([['name' => 'Taylor Otwell', 'email' => 'taylor@laravel.com']]);
265+
$this->assertEquals([['name' => 'Taylor Otwell', 'address' => 'taylor@laravel.com']], $mailable->from);
266+
$this->assertTrue($mailable->hasFrom('taylor@laravel.com', 'Taylor Otwell'));
267+
$this->assertTrue($mailable->hasFrom('taylor@laravel.com'));
268+
269+
$mailable = new WelcomeMailableStub;
270+
$mailable->from(new MailableTestUserStub);
271+
$this->assertEquals([['name' => 'Taylor Otwell', 'address' => 'taylor@laravel.com']], $mailable->from);
272+
$this->assertTrue($mailable->hasFrom(new MailableTestUserStub));
273+
$this->assertTrue($mailable->hasFrom('taylor@laravel.com'));
274+
275+
$mailable = new WelcomeMailableStub;
276+
$mailable->from(collect([new MailableTestUserStub]));
277+
$this->assertEquals([['name' => 'Taylor Otwell', 'address' => 'taylor@laravel.com']], $mailable->from);
278+
$this->assertTrue($mailable->hasFrom(new MailableTestUserStub));
279+
$this->assertTrue($mailable->hasFrom('taylor@laravel.com'));
280+
281+
$mailable = new WelcomeMailableStub;
282+
$mailable->from(collect([new MailableTestUserStub, new MailableTestUserStub]));
283+
$this->assertEquals([
284+
['name' => 'Taylor Otwell', 'address' => 'taylor@laravel.com'],
285+
['name' => 'Taylor Otwell', 'address' => 'taylor@laravel.com'],
286+
], $mailable->from);
287+
$this->assertTrue($mailable->hasFrom(new MailableTestUserStub));
288+
$this->assertTrue($mailable->hasFrom('taylor@laravel.com'));
289+
290+
foreach (['', null, [], false] as $address) {
291+
$mailable = new WelcomeMailableStub;
292+
$mailable->from($address);
293+
$this->assertFalse($mailable->hasFrom(new MailableTestUserStub));
294+
$this->assertFalse($mailable->hasFrom($address));
295+
}
214296
}
215297

216298
public function testItIgnoresDuplicatedRawAttachments()

0 commit comments

Comments
 (0)