renathy
renathy

Reputation: 5355

send generated pdf (with dompdf) to email without saving on server and without pear class

Is it possible to send dompdf generated PDF to email, without saving PDF on server and without using pear classes?

I have found sollutions only for: 1) saving pdf on server and then adding it as attachement or 2) Using some pear class

Both of those doesn't fit me. I am having pdf in variable:

$pdf = $dompdf->output();                                   

Upvotes: 1

Views: 2669

Answers (1)

mti2935
mti2935

Reputation: 12027

I think you ought to be able to base64-encode the PDF information which you've stored as a string in $pdf, insert directly into a multipart mime email, and send it using PHP's mail() function, like below:

    // to, from, subject, message body, attachment filename, etc.
    $to = "[email protected]";
    $from = "[email protected]";
    $subject = "subject";
    $message = "this is the message body";        
    $fname="nameofpdfdocument.pdf";

    $headers = "From: $from"; 
    // boundary 
    $semi_rand = md5(time()); 
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

    // headers for attachment 
    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 

    // multipart boundary 
    $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
    $message .= "--{$mime_boundary}\n";

    // preparing attachment            
        $data=$pdf;
        $data = chunk_split(base64_encode($data));
        $message .= "Content-Type: {\"application/pdf\"};\n" . " name=\"$fname\"\n" . 
        "Content-Disposition: attachment;\n" . " filename=\"$fname\"\n" . 
        "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
        $message .= "--{$mime_boundary}\n";


    // send
    //print $message;

    $ok = @mail($to, $subject, $message, $headers, "-f " . $from);          

Upvotes: 5

Related Questions