DOMPDF – attach created PDF to email

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.";
}

Leave a Comment