我已成功在网站上创建了PDF文件。我想要做的就是将该pdf作为附件发送到电子邮件中。到目前为止,似乎我已经成功连接的文件但试图打开附件Acrobat Reader软件给我这个错误消息时:PHP将电子邮件添加到电子邮件
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 = "[email protected]";
$from = "[email protected]";
$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);
对于此MAX_INTth时间网站,不要建立你自己的哑剧电子邮件。使用[PHPMailer](http://phpmailer.worxware.com)或[Swiftmailer](http://swiftmailer.org)为你做。 –
使用为您做附件处理的邮件库。没有必要自己写一切,两个常见的是Phpmailer和Swiftmailer。编辑:@MarcB:大声笑 – hakre
他们说,与旋钮! – 2012-09-24 20:22:13