Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mprythero's avatar

Laravel - Unable to open file for reading / Swift_IoException

IN trying to use mailable in Laravel, I have run into an issue that I haven't come across before and it appears nothing currently out there can help.

In developing a new mailable, I have everything working except attaching an EXISTING file to the mailable.

An error returns as such:

       "message": "Unable to open file for reading [/public/storage/shipments/CJ2K4u6S6uluEGd8spOdYgwNkg8NgLFoC6cF6fm5.pdf]",
        "exception": "Swift_IoException",
        "file": "E:\webserver\htdocs\truckin\vendor\swiftmailer\swiftmailer\lib\classes\Swift\ByteStream\FileByteStream.php",
        "line": 131,

But if you go through the folders and files, there is in fact a file there and I can open it, I can even open it through an ajax popup to view details.

Here is my mailable:

    <?php
    
    namespace App\Mail;
    
    use Illuminate\Bus\Queueable;
    use Illuminate\Mail\Mailable;
    use Illuminate\Queue\SerializesModels;
    use Illuminate\Contracts\Queue\ShouldQueue;
    
    use App\Shipment;
    use App\Shipment_Attachment;
    
    class shipmentAttachments extends Mailable
    {
        use Queueable, SerializesModels;
    
        /**
         * Create a new message instance.
         *
         * @return void
         */
        public $shipment, $attachment, $storagePath;
        
        public function __construct($shipment, $attachment, $storagePath)
        {
            $this->shipment = $shipment;
            $this->attachment = $attachment;
            $this->attachmentFile = '/public'.$storagePath;
            $this->proNumber = $shipment->pro_number;
        }
    
        /**
         * Build the message.
         *
         * @return $this
         */
        public function build()
        {
             return $this->from('[email protected]')
                        ->cc('[email protected]')
                        ->subject('New Attachment(s) - '. $this->proNumber)
                        ->view('emails.shipments.shipmentAttachments',['shipment'=> $this->shipment])
                        ->attach($this->attachmentFile);
        }
    }

And here is my controller that leads to the mailable:

    public function attachmentsEmail(Request $request){
            $shipment = Shipment::findOrFail($request->shipmentID);
            $attachment = Shipment_Attachment::findOrFail($request->attachmentID);
            $storagePath = Storage::url($attachment->attachmentPath);
            $email = $request->email;
            
                 Mail::to($email)->send(new shipmentAttachments($shipment, $attachment, $storagePath));  //maybe try to use queue instead of send...        
            return back();
        }

So I'm not sure where this could be coming up from, I feel it must be simple but I'm not sure how it could be fixed. Thanks - Matt

0 likes
4 replies
sanjay23's avatar

Is your issue resolved? I am also facing the same issue.

I have set up the project like

example.com - set in root folder demo.example.com - set in root/public/demo folder

Does it make any difference?

KAII's avatar

Try this, works for me...


       $attachmentFile = storage_path('app\public\'.$storagePath);

Please or to participate in this conversation.