From 0fa361d0e2e111a1a684606a675b414ebd471257 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 7 Mar 2018 13:49:34 -0600 Subject: [PATCH] add attachFromStorage to mailables --- src/Illuminate/Mail/Mailable.php | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/Illuminate/Mail/Mailable.php b/src/Illuminate/Mail/Mailable.php index ff6787cfb80a..81506b177999 100644 --- a/src/Illuminate/Mail/Mailable.php +++ b/src/Illuminate/Mail/Mailable.php @@ -15,6 +15,7 @@ use Illuminate\Contracts\Translation\Translator; use Illuminate\Contracts\Mail\Mailer as MailerContract; use Illuminate\Contracts\Mail\Mailable as MailableContract; +use Illuminate\Contracts\Filesystem\Factory as FilesystemFactory; class Mailable implements MailableContract, Renderable { @@ -703,6 +704,38 @@ public function attach($file, array $options = []) return $this; } + /** + * Attach a file to the message from storage. + * + * @param string $path + * @param string $name + * @param array $options + * @return $this + */ + public function attachFromStorage($path, $name = null, array $options = []) + { + return $this->attachFromStorageDisk(null, $path, $name, $options); + } + + /** + * Attach a file to the message from storage. + * + * @param string $disk + * @param string $path + * @param string $name + * @param array $options + * @return $this + */ + public function attachFromStorageDisk($disk, $path, $name = null, array $options = []) + { + $storage = Container::getInstance()->make(FilesystemFactory::class)->disk($disk); + + return $this->attachData( + $storage->get($path), $name ?? basename($path), + array_merge(['mime' => $storage->mimeType($path)], $options) + ); + } + /** * Attach in-memory data as an attachment. *