在我当前的项目中,我必须即时创建一个 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 = "mail@mail.com";
$subject = "Invoice";
$headers = "From: " . "Manager" . "<" . "mail@mail.com" . ">\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");
}
希望有人可以帮助我解决问题。