在 Outlook 中接收 PDF 文件
在我当前的项目中,我必须即时创建 PDF 文件并作为邮件附件附加并发送。它工作正常,文件生成并发送到我提供的电子邮件。当我将其发送到 Microsoft Outlook 或 Windows Live 帐户时,PDF 已附加,但无法使用 pdf 打开该文件,并出现错误,提示其已损坏。但对于 gmail 和 yahoo 来说效果很好。有没有人有解决方案。以下是我的代码
$dompdf = new DOMPDF();
$dompdf->load_html($message);
$dompdf->set_paper("a4", "landscape");
$dompdf->render();
// The next call will store the entire PDF as a string in $pdf
$pdf = $dompdf->output();
// You can now write $pdf to disk, store it in a database or stream it to the client.
file_put_contents("pdfs/invoice.pdf", $pdf);
$fileatt = "pdfs/invoice.pdf"; // Path to the file
$fileatt_type = "pdf"; // File Type
$fileatt_name = "invoice.pdf"; // Filename that will be used for the file as the attachment
$fp = fopen($fileatt, "rb");
$file = fread($fp, filesize($fileatt));
$file = chunk_split(base64_encode($file));
$num = md5(time());
$to = "[email protected]";
$subject = "Invoice";
$headers = "From: " . "Manager" . "<" . "[email protected]" . ">\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=".$num."\r\n";
$headers .= "--$num\r\n";
$headers .= "Message-ID: <" . gettimeofday() . " TheSystem@" . $_SERVER['SERVER_NAME'] . ">\r\n";
$headers .= "X-Mailer: PHP v" . phpversion() . "\r\n";
$headers .= "Content-Type:".$fileatt_type." ";
$headers .= "name=\"".$fileatt_name."\"r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; ";
$headers .= "filename=\"".$fileatt_name."\"\r\n";
$headers .= "".$file."\r\n";
$headers .= "--".$num."\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= "".$message."\r\n";
$headers .= "--".$num."--";
if (mail($to, $subject, $message, $headers)) {
fclose($fp);
echo "Success";
//header("location: client.php?m=1");
} else {
echo "Error";
//header("location: client.php?m=0");
}
希望有人可以帮助我解决问题。
In my current project, I have to create a PDF file on the fly and attach as a mail attachment and send. It works fine, the file generate and send to the email I provided. When I send it to Microsoft Outlook or Windows Live account, the PDF has attached but can't open the file with pdf, gives and error saying, its damaged. But with gmail and yahoo it works fine. Does anybody have a solution for this. Below is my code
$dompdf = new DOMPDF();
$dompdf->load_html($message);
$dompdf->set_paper("a4", "landscape");
$dompdf->render();
// The next call will store the entire PDF as a string in $pdf
$pdf = $dompdf->output();
// You can now write $pdf to disk, store it in a database or stream it to the client.
file_put_contents("pdfs/invoice.pdf", $pdf);
$fileatt = "pdfs/invoice.pdf"; // Path to the file
$fileatt_type = "pdf"; // File Type
$fileatt_name = "invoice.pdf"; // Filename that will be used for the file as the attachment
$fp = fopen($fileatt, "rb");
$file = fread($fp, filesize($fileatt));
$file = chunk_split(base64_encode($file));
$num = md5(time());
$to = "[email protected]";
$subject = "Invoice";
$headers = "From: " . "Manager" . "<" . "[email protected]" . ">\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=".$num."\r\n";
$headers .= "--$num\r\n";
$headers .= "Message-ID: <" . gettimeofday() . " TheSystem@" . $_SERVER['SERVER_NAME'] . ">\r\n";
$headers .= "X-Mailer: PHP v" . phpversion() . "\r\n";
$headers .= "Content-Type:".$fileatt_type." ";
$headers .= "name=\"".$fileatt_name."\"r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; ";
$headers .= "filename=\"".$fileatt_name."\"\r\n";
$headers .= "".$file."\r\n";
$headers .= "--".$num."\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= "".$message."\r\n";
$headers .= "--".$num."--";
if (mail($to, $subject, $message, $headers)) {
fclose($fp);
echo "Success";
//header("location: client.php?m=1");
} else {
echo "Error";
//header("location: client.php?m=0");
}
Hope someone can help me to solve the problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
构建自己的哑剧消息从来都不是一个好主意。使用 PHPMailer 或 Swiftmailer 相反。他们都处理构建消息的繁重工作,包括文件附件。最重要的是,它们都是免费的,而且比内置的 PHP 邮件功能要好得多。使用任一包,您上面的代码可以减少到大约 6 行邮件发送代码中的 5 行。
Building your own mime messages is never a good idea. Use PHPMailer or Swiftmailer instead. They both handle the heavy lifting of building the messages, including file attachments. Best of all, they're both free and work far far better than the built-in PHP mail function. Your code above could be reduced to about 5 of 6 lines of mail-sending-code with either of the packages.
我已经为此苦苦挣扎了将近一天,发现 Outlook 似乎无法识别附件,除非边界以制表符 (
\t
) 为前缀。例如:
您还需要同时使用
\r\n
和确保每个边界(及其说明)与其内容之间有一条清晰的界限。
I've struggled with this for almost a day and found that Outlook doesn't seem to recognise attachments unless the boundary is prefixed with a tab character (
\t
).For example :
You also need to use both
\r\n
together andmake sure there is a clear line between each boundary (and it's instructions) and it's content.