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

[10.x] Add missing 'attachMany' method to 'MailMessage' class #47345

Merged
merged 1 commit into from
Jun 4, 2023
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
19 changes: 19 additions & 0 deletions src/Illuminate/Notifications/Messages/MailMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,25 @@ public function attach($file, array $options = [])
return $this;
}

/**
* Attach multiple files to the message.
*
* @param array<string|\Illuminate\Contracts\Mail\Attachable|\Illuminate\Mail\Attachment|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.
*
Expand Down
45 changes: 45 additions & 0 deletions tests/Notifications/NotificationMailMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}