user image

Mohamed Atef
Published in : 2021-12-14

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

Laravel

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?

web-brackets.com

Comments

Mohamed Atef Date : 2021-12-14

Best answers

51

Best answers

51

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('test@gmail.com')->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

How can I validate recaptcha in Livewire class ??
Publish date: 2022-02-23 | Comments: 2
[solved] All assets in laravel storage returns 404
Publish date: 2021-12-14 | Comments: 2