user

Mohamed Atef

14 Dec 2021

[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?

Comments

Mohamed Atef

14 Dec 2021

Best Answer

best answer
githubgithubgithub

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

© 2024 Copyrights reserved for web-brackets.com