"Unable to open file for reading" (Swift_IoException) in Laravel Mailable

If you use Storage, and you are trying to export xlsx files, using Laravel Notifications:

in your notification class:

public function toMail($notifiable) {
    
    $path = Storage::disk('export')->getAdapter()->getPathPrefix();

    return (new MailMessage)
        ->greeting(language_data('Your file is ready', $this->user->language_id).$this->user->name)
        ->line(language_data('Please, check your Email attachments.', $this->user->language_id))
        ->subject(language_data('Export Contacts', $this->user->language_id))
        ->attach($path.$notifiable->filename, [ 'as' => $notifiable->filename, 'mime' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', ])
        ->line(language_data('If you did not request this file, please contact us.', $this->user->language_id));
}

It works fine for me.


I was serching a lot about that, it happens the same when you are tryng to build a PDF on dompdf, just exactly the same, you normaly could write this:

('/image/'.$file) and will not work , so you can solve it adding a dot just behind the rout ".", just like this:

('./image/'.$file)

It works when you want to add a attach in a mail sending or when you want to make a PDF including images in it.


Try to use public_path() laravel helper function instead of '/public'.

$this->attachmentFile = public_path() . '/' . $storagePath;

Maybe you need to change this variable in public/index.php. I have right below the require bootstrap:

$app->bind('path.public', function() {
    return __DIR__;
});

Make some tests.

dd(public_path());
dd(public_path() . '/' . $storagePath);

Or maybe verify if file exist with FileSystem class.

Hope this help you!