Skip to content

Commit

Permalink
support multiple addresses in array
Browse files Browse the repository at this point in the history
  • Loading branch information
binotaliu committed Jul 31, 2018
1 parent 689e087 commit 64f6f17
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
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);
}
}

0 comments on commit 64f6f17

Please sign in to comment.