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] Support multiple addresses in array #25029

Merged
merged 1 commit into from Aug 1, 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
41 changes: 36 additions & 5 deletions src/Illuminate/Notifications/Messages/MailMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Illuminate\Notifications\Messages;

use Traversable;
use Illuminate\Contracts\Support\Arrayable;

class MailMessage extends SimpleMessage
{
/**
Expand Down Expand Up @@ -144,35 +147,47 @@ public function from($address, $name = null)
*/
public function replyTo($address, $name = null)
{
$this->replyTo[] = [$address, $name];
if (is_array($address) || $address instanceof Arrayable || $address instanceof Traversable) {
$this->replyTo += $this->prepareAddress($address);
} else {
$this->replyTo[] = [$address, $name];
}

return $this;
}

/**
* Set the cc address for the mail message.
*
* @param string $address
* @param array|string $address
* @param string|null $name
* @return $this
*/
public function cc($address, $name = null)
{
$this->cc[] = [$address, $name];
if (is_array($address) || $address instanceof Arrayable || $address instanceof Traversable) {
$this->cc += $this->prepareAddress($address);
} else {
$this->cc[] = [$address, $name];
}

return $this;
}

/**
* Set the bcc address for the mail message.
*
* @param string $address
* @param array|string $address
* @param string|null $name
* @return $this
*/
public function bcc($address, $name = null)
{
$this->bcc[] = [$address, $name];
if (is_array($address) || $address instanceof Arrayable || $address instanceof Traversable) {
$this->bcc += $this->prepareAddress($address);
} else {
$this->bcc[] = [$address, $name];
}

return $this;
}
Expand Down Expand Up @@ -230,4 +245,20 @@ public function data()
{
return array_merge($this->toArray(), $this->viewData);
}

/**
* Prepare address input for support multiple recipients.
*
* @param array $value
* @return array
*/
protected function prepareAddress($value)
{
$recipients = [];
foreach ($value as $name => $address) {
$recipients[] = [$address, is_numeric($name) ? null : $name];
}

return $recipients;
}
}
15 changes: 15 additions & 0 deletions tests/Notifications/NotificationMailMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public function testCcIsSetCorrectly()
->cc('test@example.com', 'Test');

$this->assertSame([['test@example.com', null], ['test@example.com', 'Test']], $message->cc);

$message = new MailMessage;
$message->cc(['test@example.com', 'Test' => 'test@example.com']);

$this->assertSame([['test@example.com', null], ['test@example.com', 'Test']], $message->cc);
}

public function testBccIsSetCorrectly()
Expand All @@ -44,6 +49,11 @@ public function testBccIsSetCorrectly()
->bcc('test@example.com', 'Test');

$this->assertSame([['test@example.com', null], ['test@example.com', 'Test']], $message->bcc);

$message = new MailMessage;
$message->bcc(['test@example.com', 'Test' => 'test@example.com']);

$this->assertSame([['test@example.com', null], ['test@example.com', 'Test']], $message->bcc);
}

public function testReplyToIsSetCorrectly()
Expand All @@ -58,5 +68,10 @@ public function testReplyToIsSetCorrectly()
->replyTo('test@example.com', 'Test');

$this->assertSame([['test@example.com', null], ['test@example.com', 'Test']], $message->replyTo);

$message = new MailMessage;
$message->replyTo(['test@example.com', 'Test' => 'test@example.com']);

$this->assertSame([['test@example.com', null], ['test@example.com', 'Test']], $message->replyTo);
}
}