Skip to content

Commit 44c32a2

Browse files
authored
[7.x] Let mailables accept a simple array of email addresses as cc or bcc (#33810)
* let mailables accept a simple array of email addresses as cc or bcc * use closure to older php compatibility * make styleci pass
1 parent 90b378d commit 44c32a2

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

src/Illuminate/Mail/Mailable.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,12 @@ protected function addressesToArray($address, $name)
641641
protected function normalizeRecipient($recipient)
642642
{
643643
if (is_array($recipient)) {
644+
if (array_values($recipient) === $recipient) {
645+
return (object) array_map(function ($email) {
646+
return compact('email');
647+
}, $recipient);
648+
}
649+
644650
return (object) $recipient;
645651
} elseif (is_string($recipient)) {
646652
return (object) ['email' => $recipient];

tests/Mail/MailMailableTest.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,118 @@ public function testMailableSetsRecipientsCorrectly()
5454
$this->assertTrue($mailable->hasTo('[email protected]'));
5555
}
5656

57+
public function testMailableSetsCcRecipientsCorrectly()
58+
{
59+
$mailable = new WelcomeMailableStub;
60+
$mailable->cc('[email protected]');
61+
$this->assertEquals([['name' => null, 'address' => '[email protected]']], $mailable->cc);
62+
$this->assertTrue($mailable->hasCc('[email protected]'));
63+
64+
$mailable = new WelcomeMailableStub;
65+
$mailable->cc('[email protected]', 'Taylor Otwell');
66+
$this->assertEquals([['name' => 'Taylor Otwell', 'address' => '[email protected]']], $mailable->cc);
67+
$this->assertTrue($mailable->hasCc('[email protected]', 'Taylor Otwell'));
68+
$this->assertTrue($mailable->hasCc('[email protected]'));
69+
70+
$mailable = new WelcomeMailableStub;
71+
$mailable->cc(['[email protected]']);
72+
$this->assertEquals([['name' => null, 'address' => '[email protected]']], $mailable->cc);
73+
$this->assertTrue($mailable->hasCc('[email protected]'));
74+
$this->assertFalse($mailable->hasCc('[email protected]', 'Taylor Otwell'));
75+
76+
$mailable = new WelcomeMailableStub;
77+
$mailable->cc([['name' => 'Taylor Otwell', 'email' => '[email protected]']]);
78+
$this->assertEquals([['name' => 'Taylor Otwell', 'address' => '[email protected]']], $mailable->cc);
79+
$this->assertTrue($mailable->hasCc('[email protected]', 'Taylor Otwell'));
80+
$this->assertTrue($mailable->hasCc('[email protected]'));
81+
82+
$mailable = new WelcomeMailableStub;
83+
$mailable->cc(new MailableTestUserStub);
84+
$this->assertEquals([['name' => 'Taylor Otwell', 'address' => '[email protected]']], $mailable->cc);
85+
$this->assertTrue($mailable->hasCc(new MailableTestUserStub));
86+
$this->assertTrue($mailable->hasCc('[email protected]'));
87+
88+
$mailable = new WelcomeMailableStub;
89+
$mailable->cc(collect([new MailableTestUserStub]));
90+
$this->assertEquals([['name' => 'Taylor Otwell', 'address' => '[email protected]']], $mailable->cc);
91+
$this->assertTrue($mailable->hasCc(new MailableTestUserStub));
92+
$this->assertTrue($mailable->hasCc('[email protected]'));
93+
94+
$mailable = new WelcomeMailableStub;
95+
$mailable->cc(collect([new MailableTestUserStub, new MailableTestUserStub]));
96+
$this->assertEquals([
97+
['name' => 'Taylor Otwell', 'address' => '[email protected]'],
98+
['name' => 'Taylor Otwell', 'address' => '[email protected]'],
99+
], $mailable->cc);
100+
$this->assertTrue($mailable->hasCc(new MailableTestUserStub));
101+
$this->assertTrue($mailable->hasCc('[email protected]'));
102+
103+
$mailable = new WelcomeMailableStub;
104+
$mailable->cc(['[email protected]', '[email protected]']);
105+
$this->assertEquals([
106+
['name' => null, 'address' =>'[email protected]'],
107+
['name' => null, 'address' =>'[email protected]'],
108+
], $mailable->cc);
109+
$this->assertTrue($mailable->hasCc('[email protected]'));
110+
$this->assertTrue($mailable->hasCc('[email protected]'));
111+
}
112+
113+
public function testMailableSetsBccRecipientsCorrectly()
114+
{
115+
$mailable = new WelcomeMailableStub;
116+
$mailable->bcc('[email protected]');
117+
$this->assertEquals([['name' => null, 'address' => '[email protected]']], $mailable->bcc);
118+
$this->assertTrue($mailable->hasBcc('[email protected]'));
119+
120+
$mailable = new WelcomeMailableStub;
121+
$mailable->bcc('[email protected]', 'Taylor Otwell');
122+
$this->assertEquals([['name' => 'Taylor Otwell', 'address' => '[email protected]']], $mailable->bcc);
123+
$this->assertTrue($mailable->hasBcc('[email protected]', 'Taylor Otwell'));
124+
$this->assertTrue($mailable->hasBcc('[email protected]'));
125+
126+
$mailable = new WelcomeMailableStub;
127+
$mailable->bcc(['[email protected]']);
128+
$this->assertEquals([['name' => null, 'address' => '[email protected]']], $mailable->bcc);
129+
$this->assertTrue($mailable->hasBcc('[email protected]'));
130+
$this->assertFalse($mailable->hasBcc('[email protected]', 'Taylor Otwell'));
131+
132+
$mailable = new WelcomeMailableStub;
133+
$mailable->bcc([['name' => 'Taylor Otwell', 'email' => '[email protected]']]);
134+
$this->assertEquals([['name' => 'Taylor Otwell', 'address' => '[email protected]']], $mailable->bcc);
135+
$this->assertTrue($mailable->hasBcc('[email protected]', 'Taylor Otwell'));
136+
$this->assertTrue($mailable->hasBcc('[email protected]'));
137+
138+
$mailable = new WelcomeMailableStub;
139+
$mailable->bcc(new MailableTestUserStub);
140+
$this->assertEquals([['name' => 'Taylor Otwell', 'address' => '[email protected]']], $mailable->bcc);
141+
$this->assertTrue($mailable->hasBcc(new MailableTestUserStub));
142+
$this->assertTrue($mailable->hasBcc('[email protected]'));
143+
144+
$mailable = new WelcomeMailableStub;
145+
$mailable->bcc(collect([new MailableTestUserStub]));
146+
$this->assertEquals([['name' => 'Taylor Otwell', 'address' => '[email protected]']], $mailable->bcc);
147+
$this->assertTrue($mailable->hasBcc(new MailableTestUserStub));
148+
$this->assertTrue($mailable->hasBcc('[email protected]'));
149+
150+
$mailable = new WelcomeMailableStub;
151+
$mailable->bcc(collect([new MailableTestUserStub, new MailableTestUserStub]));
152+
$this->assertEquals([
153+
['name' => 'Taylor Otwell', 'address' => '[email protected]'],
154+
['name' => 'Taylor Otwell', 'address' => '[email protected]'],
155+
], $mailable->bcc);
156+
$this->assertTrue($mailable->hasBcc(new MailableTestUserStub));
157+
$this->assertTrue($mailable->hasBcc('[email protected]'));
158+
159+
$mailable = new WelcomeMailableStub;
160+
$mailable->bcc(['[email protected]', '[email protected]']);
161+
$this->assertEquals([
162+
['name' => null, 'address' =>'[email protected]'],
163+
['name' => null, 'address' =>'[email protected]'],
164+
], $mailable->bcc);
165+
$this->assertTrue($mailable->hasBcc('[email protected]'));
166+
$this->assertTrue($mailable->hasBcc('[email protected]'));
167+
}
168+
57169
public function testMailableSetsReplyToCorrectly()
58170
{
59171
$mailable = new WelcomeMailableStub;

0 commit comments

Comments
 (0)