DOMPDF – attach created PDF to email

What is the easiest way to attach a PDF to an email via DOMPDF?

The end of my script I am using (part of it) is below:

$dompdf = new DOMPDF();
//below to save the pdf file - not needed if emailing pdf
file_put_contents('/home/ststrave/public_html/pdf/STS_Brochure.pdf', $dompdf->output());
//below to open pdf in browser - required
$dompdf->stream("STS_Brochure_".rand(10,1000).".pdf", array("Attachment" => false));

Just for clarification – this is being used in Joomla.

Appreciate the simplest/quickest way using standard PHP mail function.

Cheers 😉

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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->set_paper("letter", "portrait" );

$output = $dompdf->output();

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

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

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

$mail =& Mail::factory('mail');
    echo "Your message has been sent.";

Solution 2

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

Instead of:


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';


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

Solution 3

PHP’s mail function has no “standard” file attachment method. It’s an extremely barebones interface to the SMTP system that forces you to do ALL the work of attaching a file yourself.

I strongly suggest using PHPMailer or Swiftmailer to do the email for you – it reduces the heavy grunt work of generating your own MIME email and inserting the attachment (many many lines of code) does to maybe 5 lines total.

Note that neither of them will handle a streamed PDF from DOMPDF. You’ll have to save the PDF to a temporary file and attach that,

Solution 4

Raw Data Attachments
The attachData method may be used to attach a raw string of bytes as an attachment. For example, you might use this method if you have generated a PDF in memory and want to attach it to the email without writing it to disk. The attachData method accepts the raw data bytes as its first argument, the name of the file as its second argument, and an array of options as its third argument:

 * Build the message.
 * @return $this
public function build()
    return $this->view('emails.orders.shipped')
                ->attachData($this->pdf, 'name.pdf', [
                    'mime' => 'application/pdf',

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply