0

Hi i am trying to attach a pdf directly to an email without storing to storage using laravel mail function and sending it to the user. Everything is working fine as expected but the problem is the pdf instead of an attachment is coming as base64 encoded email content.

Can anyone please tell me what i am doing wrong?

Here is my code . This is inside mailer

$subject ='Hello! Your invoice is here';

        $data['invoice_id']=$this->content['invoice_id'];

        $pdf = PDF::loadView('frontend.pages.invoice.invoice',$data);


        return $this->from('info@examplecompany.com')
            ->subject($subject)
            ->view('backend.emailtemplates.invoiceEmail')
            ->attachData($pdf->output(),'name.pdf', [
                'mime' => 'application/pdf',
            ]) ;

I am following this How to attach a PDF using barryvdh/laravel-dompdf into an email in Laravel thread, even though i did the same way as its answer suggest , still not working.

Can anyone please help me on this. Thank you.

user7747472
  • 1,874
  • 6
  • 36
  • 80
  • This is a pretty stale question at this time but if you could, can you share what the error was you were getting or how you resolved? – OneSimpleGeek Jan 06 '20 at 20:40

2 Answers2

0

Recommend to use this package for pdf.

https://github.com/niklasravnsborg/laravel-pdf

you can easily manage your pdf export:

  • output(): Outputs the PDF as a string.
  • save($filename): Save the PDF to a file
  • download($filename): Make the PDF downloadable by the user.
  • stream($filename): Return a response with the PDF to show in the browser.
Danial Panah
  • 111
  • 3
0

I think you want to send pdf as an attachment in your email without saving it in your system .

your can refer the following code to do this .

 $data["email"]='useremail@gmail.com';
            $data["client_name"]='user_name';
            $data["subject"]='sending pdf as attachment';

            //Generating PDF with all the post details

            $pdf = PDF::loadView('userdata');  // this view file will be converted to PDF

            //Sending Mail to the corresponding user

            Mail::send([], $data, function($message)use($data,$pdf) {
            $message->to($data["email"], $data["client_name"])
            ->subject($data["subject"])
            ->attachData($pdf->output(), 'Your_Post_Detail.pdf', [
                       'mime' => 'application/pdf',
                   ])
            ->setBody('Hi, welcome user! this is the body of the mail');
            });

hope it helped you .

for better understanding you can follow this article

how to send pdf as an attachment in email in laravel

kishan maharana
  • 182
  • 1
  • 5