user image

Mohamed Atef
Published in : 2021-12-14

[solved] Unable to open file for reading attaching PDF into laravel mail


Swift_loException: Unable to open file for reading, I am facing this error when I try to attach a PDF with email in Laravel using markdown, I still getting the same error however the file exists in the correct directory which I use in the assets functions

 public function build(){ return $this->subject('New Form submission')->markdown('mails.confirmation')->with([ "fname" => $this->data["fname"], "lname" => $this->data["lname"], "email" => $this->data["email"], "dateOfBirth" => $this->data["dateOfBirth"], "phone" => $this->data["phone"], "address1" => $this->data["address1"], "address2" => $this->data["address2"], "city" => $this->data["city"], "postal" => $this->data["postal"], ])->attach(asset('/storage/pdf/invoice.pdf'), ['mime' => 'application/pdf']); }

Any thoughts on how can I solve this error?


Mohamed Atef Date : 2021-12-14

Best answers


Best answers


After a lot of searching i couldn't handle it in the same way, so the only way which worked for me is attaching the link of the PDF into the email with download button, so the functionality should be like this ,

into the controller i am going to save the PDF and generate the link to download the PDF

 $pdf = PDF::loadView('pdf.HomepagePDF', ['data' => $data]); $pdf->setOptions(['defaultFont' => 'sans-serif']); $file_name = time().'.pdf'; Storage::disk('local')->put('/public/pdf/'.$file_name, $pdf->output()); $link = url('/').'/storage/pdf/'.$file_name; $mailArr = [ "link" => $link, "fname" => $fname, "lname" => $lname, "email" => $email, "dateOfBirth" => $dateOfBirth, "phone" => $phone, "address1" => $address1, "address2" => $address2, "city" => $city, "postal" => $postal, ]; Mail::to('[email protected]')->send(new \App\Mail\DefaultFormMail($mailArr));

then I will receive it from the mail class like regular integer 

 public function build(){ return $this->subject('New Form submission')->markdown('mails.confirmation')->with([ "DownloadLink" => $this->data["link"], "fname" => $this->data["fname"], "lname" => $this->data["lname"], "email" => $this->data["email"], "dateOfBirth" => $this->data["dateOfBirth"], "phone" => $this->data["phone"], "address1" => $this->data["address1"], "address2" => $this->data["address2"], "city" => $this->data["city"], "postal" => $this->data["postal"], ]); }

if anyone know how to attach it let us know how you handle it, i searched a lot but this is the only thing which worked for me, Thanks in advance

Leave a comment

Join us

Join our community and get the chance to solve your code issues & share your opinion with us

Sign up Now

Related posts

I want to make multiple foreign keys in Laravel, can I?
Publish date: 2022-01-27 | Comments: 5
How can i pass data from blade template to vue component?
Publish date: 2022-02-11 | Comments: 1
[solved] All assets in laravel storage returns 404
Publish date: 2021-12-14 | Comments: 2
laravel installation error with composer install
Publish date: 2022-02-23 | Comments: 2
How can I use arabic words, paragraphs in the faker?
Publish date: 2022-03-06 | Comments: 1
[solved] Nginx 413 Request Entity Too Large - Laravel
Publish date: 2022-02-01 | Comments: 7