From 64f6f170e9c8f61dd61ef6f75aae633f9547b8b9 Mon Sep 17 00:00:00 2001 From: BinotaLIU Date: Tue, 31 Jul 2018 19:40:52 +0800 Subject: [PATCH] support multiple addresses in array --- .../Notifications/Messages/MailMessage.php | 41 ++++++++++++++++--- .../NotificationMailMessageTest.php | 15 +++++++ 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Notifications/Messages/MailMessage.php b/src/Illuminate/Notifications/Messages/MailMessage.php index f0db201c64ed..a36d21a53822 100644 --- a/src/Illuminate/Notifications/Messages/MailMessage.php +++ b/src/Illuminate/Notifications/Messages/MailMessage.php @@ -2,6 +2,9 @@ namespace Illuminate\Notifications\Messages; +use Traversable; +use Illuminate\Contracts\Support\Arrayable; + class MailMessage extends SimpleMessage { /** @@ -144,7 +147,11 @@ 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; } @@ -152,13 +159,17 @@ public function replyTo($address, $name = null) /** * 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; } @@ -166,13 +177,17 @@ public function cc($address, $name = null) /** * 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; } @@ -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; + } } diff --git a/tests/Notifications/NotificationMailMessageTest.php b/tests/Notifications/NotificationMailMessageTest.php index fc623ca94e28..8f8a0878a678 100644 --- a/tests/Notifications/NotificationMailMessageTest.php +++ b/tests/Notifications/NotificationMailMessageTest.php @@ -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() @@ -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() @@ -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); } }