将生成的pdf(带有dompdf)发送到电子邮件,而不保存在服务器上和没有梨类

时间:2013-07-04 18:20:29

标签: html css dompdf

是否可以将dompdf生成的PDF发送到电子邮件,而无需在服务器上保存PDF并且不使用梨类?

我发现只有以下方面的解决方案: 1)在服务器上保存pdf,然后将其添加为附件或 2)使用一些梨类

这两个都不适合我。我在变量中有pdf:

$pdf = $dompdf->output();                                   

1 个答案:

答案 0 :(得分:4)

我认为你应该能够对你在$ pdf中存储为字符串的PDF信息进行64位编码,直接插入到多部分mime电子邮件中,然后使用PHP的mail()函数发送它,如下所示:

    // to, from, subject, message body, attachment filename, etc.
    $to = "to@to.com";
    $from = "from@from.com";
    $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);