是否可以将dompdf生成的PDF发送到电子邮件,而无需将PDF保存在服务器上,也无需使用pear类?
我找到的解决方案只有: 1)将pdf保存在服务器上,然后将其作为附件添加,或者2)使用一些pear类。
这两件都不适合我。我在变量中有pdf:
$pdf = $dompdf->output();
发布于 2013-07-04 19:20:39
我认为你应该能够对你在$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);
https://stackoverflow.com/questions/17476269
复制