diff --git a/src/Illuminate/Notifications/Messages/MailMessage.php b/src/Illuminate/Notifications/Messages/MailMessage.php index d696c8a84a98..d847072c8f59 100644 --- a/src/Illuminate/Notifications/Messages/MailMessage.php +++ b/src/Illuminate/Notifications/Messages/MailMessage.php @@ -262,6 +262,25 @@ public function attach($file, array $options = []) return $this; } + /** + * Attach multiple files to the message. + * + * @param array $files + * @return $this + */ + public function attachMany($files) + { + foreach ($files as $file => $options) { + if (is_int($file)) { + $this->attach($options); + } else { + $this->attach($file, $options); + } + } + + return $this; + } + /** * Attach in-memory data as an attachment. * diff --git a/tests/Notifications/NotificationMailMessageTest.php b/tests/Notifications/NotificationMailMessageTest.php index 3ac76045a502..7a59cfd8890d 100644 --- a/tests/Notifications/NotificationMailMessageTest.php +++ b/tests/Notifications/NotificationMailMessageTest.php @@ -314,4 +314,49 @@ public function toMailAttachment() ], ], $mailMessage->rawAttachments[0]); } + + public function testItAttachesManyFiles() + { + $mailMessage = new MailMessage(); + $attachable = new class() implements Attachable + { + public function toMailAttachment() + { + return Attachment::fromData(fn () => 'bar', 'foo.jpg')->withMime('image/png'); + } + }; + + $mailMessage->attachMany([ + $attachable, + '/path/to/forge.svg', + '/path/to/vapor.svg' => [ + 'as' => 'Logo.svg', + 'mime' => 'image/svg+xml', + ], + ]); + + $this->assertSame([ + [ + 'data' => 'bar', + 'name' => 'foo.jpg', + 'options' => [ + 'mime' => 'image/png', + ], + ], + ], $mailMessage->rawAttachments); + + $this->assertSame([ + [ + 'file' => '/path/to/forge.svg', + 'options' => [], + ], + [ + 'file' => '/path/to/vapor.svg', + 'options' => [ + 'as' => 'Logo.svg', + 'mime' => 'image/svg+xml', + ], + ], + ], $mailMessage->attachments); + } }