无法在邮件中打开使用 mpdf 创建的 pdf 文件
Can't open pdf files created with mpdf in mail
我正在尝试使用 mpdf 创建一个 pdf 文档,在第一步中我尝试将它保存在我的下载文件夹中,然后我尝试打开它并且它运行良好。我在pdf文档里可以看到我需要的东西
在下一步中,我尝试将 pdf 作为附件发送,并且使用 pdf 附件成功发送了电子邮件,但是当我尝试从邮件中打开它时,它返回给我一个错误 "Adobe cannot open the pdf document because it is neither a supported file type or the file is damaged......." .
我在 Whosebug 中搜索了不同的问题,我也尝试了他们的宝贵答案,但 none 对我有帮助。
下面是同样的问题,但那里发布的答案也没有帮助我。
这是我的代码:
$mpdf=new mPDF( );
$mpdf->SetDisplayMode('fullpage');
$mpdf->list_indent_first_level = 0;
$mpdf->WriteHTML($html,2);
ob_clean(); // **Tried this from Whosebug answers**
$mpdf->Output($filename.'.pdf', 'I');
$mailto = $row['Email1'].",".$row['Email5'];
$path = 'C:\Users\Downloads\';
$file = $path."/".$filename;
$subject = 'Test Email';
$message = 'Test <br> Case';
$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));
$separator = md5(time());
$eol = PHP_EOL;
$headers = "From: John <john.xxxxx@gmail.com>".$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"".$eol.$eol;
// message
$body .= "Content-Transfer-Encoding: 7bit".$eol;
$body .= "This is a MIME encoded message.".$eol;
$body = "--" . $separator.$eol;
$body .= "Content-Type: text/html; charset=\"UTF-8\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;
// attachment
$body .= "--" . $separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"" . $filename . '.pdf' . "\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol ;
$body .= $content.$eol;
$body .= "--" . $separator . "--";
//SEND Mail
if (mail($mailto, $subject, $body, $headers)) {
//echo "mail send ... OK"; // or use booleans here
}
您是否尝试过更改为附件设置的 mime 类型?
pdf 文件的标准 mime 类型是:
application/pdf
我注意到您正在尝试从本地文件系统加载文件。脚本 运行 是否在同一台机器上?或者在网络服务器上?
如果它 运行 在网络服务器上,您可能需要先将文件上传到网络服务器。上传后,您需要更改 $path
和 $file
变量。
$path = 'www/images/[some-path]';
$file = '/'.$filename;
如果它 运行 与文件所在的机器在同一台机器上,您可能需要更新 $file
变量以便它使用反斜杠 \
而不是正斜杠。
$path = 'C:\Users\Downloads\';
$file = $path.'\'.$filename;
脚本中的另一个 error
:您从未将 mpdf 内容设置为电子邮件附件的内容。尝试类似的东西:
$content = chunk_split(base64_encode($mpdf->Output($filename.'.pdf', 'I')));
您必须检查 mpdf 的 Output()
函数中的 I
是否适合此用途。我不确定。
编辑
// First save it to the server somewhere
$mpdf->Output($filename.'.pdf', 'F'); // Make sure the path is valid and writing permissions are set correctly to prevent writing errors.
// Then get the contents from the PDF
$content = chunk_split(base64_encode($mpdf->Output($filename.'.pdf', 'S')));
我正在尝试使用 mpdf 创建一个 pdf 文档,在第一步中我尝试将它保存在我的下载文件夹中,然后我尝试打开它并且它运行良好。我在pdf文档里可以看到我需要的东西
在下一步中,我尝试将 pdf 作为附件发送,并且使用 pdf 附件成功发送了电子邮件,但是当我尝试从邮件中打开它时,它返回给我一个错误 "Adobe cannot open the pdf document because it is neither a supported file type or the file is damaged......." .
我在 Whosebug 中搜索了不同的问题,我也尝试了他们的宝贵答案,但 none 对我有帮助。
下面是同样的问题,但那里发布的答案也没有帮助我。
这是我的代码:
$mpdf=new mPDF( );
$mpdf->SetDisplayMode('fullpage');
$mpdf->list_indent_first_level = 0;
$mpdf->WriteHTML($html,2);
ob_clean(); // **Tried this from Whosebug answers**
$mpdf->Output($filename.'.pdf', 'I');
$mailto = $row['Email1'].",".$row['Email5'];
$path = 'C:\Users\Downloads\';
$file = $path."/".$filename;
$subject = 'Test Email';
$message = 'Test <br> Case';
$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));
$separator = md5(time());
$eol = PHP_EOL;
$headers = "From: John <john.xxxxx@gmail.com>".$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"".$eol.$eol;
// message
$body .= "Content-Transfer-Encoding: 7bit".$eol;
$body .= "This is a MIME encoded message.".$eol;
$body = "--" . $separator.$eol;
$body .= "Content-Type: text/html; charset=\"UTF-8\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;
// attachment
$body .= "--" . $separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"" . $filename . '.pdf' . "\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol ;
$body .= $content.$eol;
$body .= "--" . $separator . "--";
//SEND Mail
if (mail($mailto, $subject, $body, $headers)) {
//echo "mail send ... OK"; // or use booleans here
}
您是否尝试过更改为附件设置的 mime 类型? pdf 文件的标准 mime 类型是:
application/pdf
我注意到您正在尝试从本地文件系统加载文件。脚本 运行 是否在同一台机器上?或者在网络服务器上?
如果它 运行 在网络服务器上,您可能需要先将文件上传到网络服务器。上传后,您需要更改
$path
和$file
变量。$path = 'www/images/[some-path]'; $file = '/'.$filename;
如果它 运行 与文件所在的机器在同一台机器上,您可能需要更新
$file
变量以便它使用反斜杠\
而不是正斜杠。$path = 'C:\Users\Downloads\'; $file = $path.'\'.$filename;
脚本中的另一个 error
:您从未将 mpdf 内容设置为电子邮件附件的内容。尝试类似的东西:
$content = chunk_split(base64_encode($mpdf->Output($filename.'.pdf', 'I')));
您必须检查 mpdf 的 Output()
函数中的 I
是否适合此用途。我不确定。
编辑
// First save it to the server somewhere
$mpdf->Output($filename.'.pdf', 'F'); // Make sure the path is valid and writing permissions are set correctly to prevent writing errors.
// Then get the contents from the PDF
$content = chunk_split(base64_encode($mpdf->Output($filename.'.pdf', 'S')));