DOMPDF - attach created PDF to email

Here is the solution I was looking for when I came here:

Instead of:

$dompdf->stream();

do this:

$fileatt = $dompdf->output();

And than send mail using PHPMailer and attach the pdf to mail like so:

        $filename = 'MyDocument.pdf';
        $encoding = 'base64';
        $type = 'application/pdf';

        $mail->AddStringAttachment($fileatt,$filename,$encoding,$type);

In this way you don't have to deal with saving the file on the server.


Ok. You already accepted an answer, but for anyone else coming here, I think there is an easier way, but it's also not PHP's standard mail function, which really isn't going to work. If you can get the pear packages Mail and Mail_mime, it's really easy to send emails with attachments. You can also directly attach the DomPDF output without creating a file, like so:

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->set_paper("letter", "portrait" );
$dompdf->render();

$output = $dompdf->output();

$mm = new Mail_mime("\n");

$mm->setTxtBody($body);
$mm->addAttachment($output,'application/pdf','output.pdf', false);

$body = $mm->get();
$headers = $mm->headers(array('From'=>$from,'Subject'=>$subject));

$mail =& Mail::factory('mail');
if($mail->send($to,$headers,$body)){
    echo "Your message has been sent.";
}