0

我已成功在网站上创建了一个 pdf 文件。我想做的是将该pdf作为电子邮件的附件发送。到目前为止,我似乎已经成功地附加了文件,但是在尝试打开附件时,acrobat reader 给了我这个错误消息:

Acrobat could not open 'example2.pdf' because it is either not a supported type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded)

我认为这个错误消息已经击中了头,下面是我的代码有人知道需要更改什么吗?谢谢提前:)

$to = "me@domain"; 
$from = "me@domain.com"; 
$subject = "send email with pdf attachment"; 
$message = "<p>Please see the attachment.</p>";
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "example.pdf";
// encode data (puts attachment in proper format)
$attachment = chunk_split(base64_encode("/include/pdf.php?reportid=849980"));
// main header (multipart mandatory)
$headers  = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol; 
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;
// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$headers .= $message.$eol.$eol;
// attachment
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";
// send message
mail($to, $subject, "", $headers);
4

1 回答 1

0
$filename = "example.pdf";
// encode data (puts attachment in proper format)
$attachment = chunk_split(base64_encode("/include/pdf.php?reportid=849980"));

看起来是你的问题。您的附件确实似乎是 pdf,而是一个 php 页面......您还需要二进制(rb)读入 pdf 的文件内容,例如:

$filename = 'example.pdf';  
$fileloc = '/path/to/pdf/'.$filename;   

$filehandle = fopen($fileloc, 'rb');
$data = fread($filehandle, filesize($fileloc));
fclose($filehandle);                

$attachment = chunk_split(base64_encode($data)); 
于 2012-09-24T21:02:45.543 回答