简体   繁体   中英

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();
$dompdf->load_html($html);
$dompdf->render();    
//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));
jexit();

Just for clarification - this is being used in Joomla.

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

Cheers;-)

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

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.

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',
                ]);
}

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,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM