Devith
Devith

Reputation: 341

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.

Upvotes: 5

Views: 3333

Answers (3)

Tanvir Bhuiyan
Tanvir Bhuiyan

Reputation: 17

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', 
]);

Upvotes: 1

Tanvir Bhuiyan
Tanvir Bhuiyan

Reputation: 17

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', ]); `

Upvotes: 0

Tuim
Tuim

Reputation: 2511

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);

Upvotes: 9

Related Questions