PDF attached in encrypted email arrives

Asked

Viewed 148 times

0

I am attaching a PDF to send in the email, using Laravel and DOMPDF but when the email arrives, there is no PDF attachment, it arrives encoded in the body of the email. follows the code.

Remembering that the PDF is generated 100%. I can see it through the browser. but send so in email

public function enviarEmail(Request $request, $id)
{
    $orcamento = $this->orcamentosService->exibir($id);

    $data["email"]          = $request->get("email");
    $data["client_name"]    = $request->get($orcamento->cliente->nome);
    $data["subject"]        = $request->get("assunto");

    $pdf = PDF::loadView('paginas.orcamentos.imprimir', ['orcamento' => $orcamento])->setPaper('a4', 'portrait');

    try{
        Mail::send('emails.orcamento', $data, function($message)use($data,$pdf) {
            $message->to($data["email"], $data["client_name"])
                ->from('xxx')
                ->subject($data["subject"])
                ->attachData($pdf->output(), "orcamento.pdf", ['mime' => 'application/pdf']);
        });
    }catch(JWTException $exception){
        $this->serverstatuscode = "0";
        $this->serverstatusdes = $exception->getMessage();
    }
    if (Mail::failures()) {
        $this->statusdesc  =   "Erro ao enviar e-mail";
        $this->statuscode  =   "0";

    }else{

        $this->statusdesc  =   "Email enviado com sucesso";
        $this->statuscode  =   "1";
    }
    return response()->json(compact('this'));
}

And here as the PDF arrives in the email

inserir a descrição da imagem aqui

1 answer

1

The method $pdf->output() generates the binary content of the PDF file, but the method attach() needs a physical file to be sent, so I recommend saving this content temporarily:

Storage::put('temp/mail.pdf', $pdf->output());

And send this saved file by email:

->attach(Storage::get('temp/mail.pdf'));

And after sending remove the temporary file not to fill your HD:

Storage::delete('temp/mail.pdf')
  • Actually the attachData() method does not need a file generated on the hard drive, it uses file generated in memory, which is what I am doing. https://laravel.com/docs/5.6/mail#attachments. I think the problem is different, it doesn’t seem to be rendering the PDF.

  • Have then tried using a method that extens the Illuminate\Mail\Mailable to send instead of directly using the class Mail ?

  • 1

    our brother.. after much suffering.. I discovered the problem.. if the body of the email is empty, it shoves the file into the body of the email, so it was not enough as an attachment. it was inserted into the body of the email..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.