5

I want to create a PDF using the barryvdh/laravel-dompdf package and send this with an email as attachment.

The code I have now is:

$pdf = PDF::loadView('layouts.factuur', array('factuur' => $factuur));

Mail::queue('emails.factuur', array('factuur' => $factuur), function($message) use ($pdf)
   {
       $message->to(Input::get('email'), Input::get('naam'))->subject('Onderwerp');
       $message->attach($pdf->output());
    });

But now I get the following error:

Serialization of 'Closure' is not allowed
Keith666
  • 77
  • 1
  • 6

1 Answers1

13

You can only send serializable entities to the queue. This includes Eloquent models etc. But not the PDF view instance. So you will probably need to do the following:

Mail::queue('emails.factuur', array('factuur' => $factuur), function($message)
   {
       $pdf = PDF::loadView('layouts.factuur', array('factuur' => $factuur));
       $message->to(Input::get('email'), Input::get('naam'))->subject('Onderwerp');
       $message->attach($pdf->output());
    });
Luceos
  • 6,629
  • 1
  • 35
  • 65
  • 2
    You could also swap $message->attach() for $message->attachData() if you want to attach in-memory data as an attachment. – 0x1ad2 Apr 20 '16 at 11:10