How to attach a PDF using barryvdh/laravel-dompdf into an email in Laravel
Asked Answered
G

1

15

I know this question has been asked before but I apparently need my hand held. I'm trying to attach the pdf to an email and send it off. I've done it outside of Laravel with PHPMailer, but now I'm trying to it the Laravel way and cant get it to work. What am I doing wrong here?

Here is the start of the error I'm getting :

Swift_IoException in FileByteStream.php line 144: Unable to open file for reading [%PDF-1.3

    public function attach_email($id){

    $info = Load::find($id);

    $info = ['info'=>$info];

    Mail::send(['text'=>'mail'], $info, function($message){

        $pdf = PDF::loadView('pdf.invoice');

        $message->to('[email protected]','John Smith')->subject('Send Mail from Laravel');

        $message->from('[email protected]','The Sender');

        $message->attach($pdf->output());

    });
   echo 'Email was sent!';
  }
Gyniatrics answered 28/11, 2016 at 15:31 Comment(0)
P
26

Use $message->attachData($pdf->output(), 'filename.pdf'); instead of $message->attach($pdf->output());.

Pronounce answered 28/11, 2016 at 20:3 Comment(2)
How does the filename be dynamic?Ganiats
@AmranurRahman if you mean a dynamic generated filename, you can use php.net/manual/en/function.uniqid.php to append to the filename 'filename.pdf'Wallas

© 2022 - 2024 — McMap. All rights reserved.