1

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

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

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

$pdf = $dompdf->output();                                   
4

1 回答 1

5

我认为您应该能够对存储为 $pdf 中的字符串的 PDF 信息进行 base64 编码,直接插入到多部分 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);          
于 2013-07-04T19:20:38.940 回答