how to send PDF by email with dompdf

1

How do I send a pdf generated by email with dompdf ? Here is my source code:

/* Cria a instância */
$dompdf = new DOMPDF();

/* Carrega seu HTML */

$dompdf->load_html("<p align=\"center\"><img src=\"caminho da imagem\"></p>");


/* Renderiza */
$dompdf->render();

sendMailerPDF("[email protected]", "teste01", "teste PDF", $dompdf->stream("STS_Brochure_".rand(10,1000).".pdf", array("Attachment" => false)));

Now using phpmailer to send.

function sendMailerPDF($destino, $nome_remetente, $assunto, $mensagem) {
        global $erro;
        $mail = new PHPMailer();
        $mail->IsSMTP(); // ativa smtp
        $mail->SMTPAuth = true; // ativa autent SMTP
        $mail->SMTPSecure = 'ssl'; // ssl ou tls define autenticação como ssl
        $mail->CharSet = "UTF-8";
        $mail->Host = "smtp.gmail.com"; // nome do servidor smtp        
        $mail->Port = 465; // porta do smtp
        $mail->Username = "usuario de acesso"; // usuário de acesso smtp
        $user = $mail->Username;
        $mail->Password = "senha"; // senha
        $mail->IsHTML(true); // define que mensagens serão html
        $mail->SetFrom($user, $nome_remetente); // define remetente
        $mail->Subject = $assunto; //define assunto
        $mail->Body = $mensagem; // define mensagem
        $mail->AddAttachment($mensagem);  // Insere um anexo
        $mail->AddAddress($destino); // define destinatario
        if ($mail->Send()) {
            return true;
        }
        else{
            $erro = $mail->ErrorInfo;
            return false;
        }
    }
    
asked by anonymous 04.06.2017 / 07:54

1 answer

1

I believe you can not do this, because dompdf it renders the page to generate the PDF, and in PHPMail you need a file on disk to be able to attach.

My suggestion is as follows:

1 - Render the file and save to the server
2 - Get the name of this file you just rendered attach and send by email
3 - Delete the file.

    
05.12.2017 / 15:50