how to send PDF by email with dompdf

Asked

Viewed 608 times

2

How do I send one pdf generated by email with dompdf? Below 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 the 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;
        }
    }

1 answer

2

I don’t think you can do this, because dompdf it renders the page to generate the PDF, and in Phpmail you need a disk file to attach.

My suggestion is this::

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

Browser other questions tagged

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