PHP在电子邮件中添加pdf文件 [英] PHP adding a pdf to email
问题描述
我已经在网站上成功创建了pdf文件.我想做的是将该pdf作为附件发送到电子邮件中.到目前为止,看来我已经成功附加了文件,但是当尝试打开附件acrobat Reader时,出现以下错误消息:
I have successful created a pdf file on a website. What I would like to do is send that pdf as an attachment in an email. So far it appears that I have successfully attached the file however when attempting to open the attachment acrobat reader give me this error message:
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)
我认为此错误消息已引起人们的关注,以下是我的代码,有人知道需要更改什么吗?谢谢你提前:)
I think this error message has hit the nail on the head, below is my code does anyone know what needs to be changed? Thanks advance :)
$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);
推荐答案
$filename = "example.pdf";
// encode data (puts attachment in proper format)
$attachment = chunk_split(base64_encode("/include/pdf.php?reportid=849980"));
似乎是您的问题.您的附件似乎确实是pdf,但是却是一个php页面...此外,您还需要二进制(rb)读取pdf的文件内容,例如:
looks to be your problem. Your attachment does appear to be the pdf, but instead a php page... also you need to binary (rb) read-in the file contents of the pdf, something like:
$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));
这篇关于PHP在电子邮件中添加pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!