Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate PDF from view to send email attaching the PDF file without saving it into disk Laravel 5

I am working on sending email function. Firstly, I want to generate .pdf file from my view. Then I want to attach the generated .pdf file by email without saving it into disk. I use below in my controller:

$pdf = PDF::loadView('getpdf', $data);
Mail::to($to_email)->send(new Mysendmail($post_title, $full_name))
->attachData($pdf->output(), "newfilename.pdf");

And I get this error: "Call to a member function attachData() on null"

If I use below without attachment, it works well:

$pdf = PDF::loadView('getpdf', $data);
Mail::to($to_email)->send(new Mysendmail($post_title, $full_name));

Please advise.

like image 382
Devith Avatar asked Jun 15 '17 06:06

Devith


2 Answers

I think you need to attach it to the message, not to the mailer.

$pdf = PDF::loadView('getpdf', $data);
$message = new Mysendmail($post_title, $full_name);
$message->attachData($pdf->output(), "newfilename.pdf");
Mail::to($to_email)->send($message);
like image 74
Tuim Avatar answered Nov 15 '22 07:11

Tuim


Just use 'mime' => 'application/pdf', at last of your code. Simple!

$pdf = PDF::loadView('getpdf', $data); 
Mail::to($to_email)->send(new Mysendmail($post_title, $full_name)) 
->attachData($pdf->output(), "newfilename.pdf"), [ 
'mime' => 'application/pdf', 
]);
like image 40
Tanvir Bhuiyan Avatar answered Nov 15 '22 07:11

Tanvir Bhuiyan