Skip to content

Commit 49c617e

Browse files
[12.x] add Attachment::fromUploadedFile method (#56027)
* add Attachment::fromUploadedFile method * Update Attachment.php * Update composer.json * Update composer.json --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent b4d67f9 commit 49c617e

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

src/Illuminate/Mail/Attachment.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Closure;
66
use Illuminate\Container\Container;
77
use Illuminate\Contracts\Filesystem\Factory as FilesystemFactory;
8+
use Illuminate\Http\UploadedFile;
89
use Illuminate\Support\Traits\Macroable;
910
use RuntimeException;
1011

@@ -79,6 +80,23 @@ public static function fromData(Closure $data, $name = null)
7980
))->as($name);
8081
}
8182

83+
/**
84+
* Create a mail attachment from an UploadedFile instance.
85+
*
86+
* @param \Illuminate\Http\UploadedFile $file
87+
* @return static
88+
*/
89+
public static function fromUploadedFile(UploadedFile $file)
90+
{
91+
return new static(function ($attachment, $pathStrategy, $dataStrategy) use ($file) {
92+
$attachment
93+
->as($file->getClientOriginalName())
94+
->withMime($file->getMimeType() ?? $file->getClientMimeType());
95+
96+
return $dataStrategy(fn () => $file->get(), $attachment);
97+
});
98+
}
99+
82100
/**
83101
* Create a mail attachment from a file in the default storage disk.
84102
*

src/Illuminate/Mail/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
},
3838
"suggest": {
3939
"aws/aws-sdk-php": "Required to use the SES mail driver (^3.322.9).",
40+
"illuminate/http": "Required to create an attachment from an UploadedFile instance (^12.0).",
4041
"resend/resend-php": "Required to enable support for the Resend mail transport (^0.10.0).",
4142
"symfony/http-client": "Required to use the Symfony API mail transports (^7.2).",
4243
"symfony/mailgun-mailer": "Required to enable support for the Mailgun mail transport (^7.2).",

tests/Mail/AttachableTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Tests\Mail;
44

55
use Illuminate\Contracts\Mail\Attachable;
6+
use Illuminate\Http\Testing\File;
67
use Illuminate\Mail\Attachment;
78
use Illuminate\Mail\Mailable;
89
use PHPUnit\Framework\TestCase;
@@ -138,4 +139,34 @@ public function toMailAttachment()
138139
],
139140
], $mailable->attachments[0]);
140141
}
142+
143+
public function testFromUploadedFileMethod()
144+
{
145+
$mailable = new class extends Mailable
146+
{
147+
public function build()
148+
{
149+
$this->attach(new class implements Attachable
150+
{
151+
public function toMailAttachment()
152+
{
153+
return Attachment::fromUploadedFile(
154+
File::createWithContent('example.pdf', 'content')
155+
->mimeType('application/pdf')
156+
);
157+
}
158+
});
159+
}
160+
};
161+
162+
$mailable->build();
163+
164+
$this->assertSame([
165+
'data' => 'content',
166+
'name' => 'example.pdf',
167+
'options' => [
168+
'mime' => 'application/pdf',
169+
],
170+
], $mailable->rawAttachments[0]);
171+
}
141172
}

0 commit comments

Comments
 (0)