在 Outlook 中接收 PDF 文件

发布于 2024-11-01 01:15:53 字数 2366 浏览 7 评论 0原文

在我当前的项目中,我必须即时创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的痛♀有谁懂 2024-11-08 01:15:53

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.

夏日浅笑〃 2024-11-08 01:15:53

我已经为此苦苦挣扎了将近一天,发现 Outlook 似乎无法识别附件,除非边界以制表符 (\t) 为前缀。

例如:

$headers.="Content-type: multipart/mixed;\r\n\tboundary=\"uniqueID\"\r\n\r\n";

您还需要同时使用 \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 :

$headers.="Content-type: multipart/mixed;\r\n\tboundary=\"uniqueID\"\r\n\r\n";

You also need to use both \r\n together and
make sure there is a clear line between each boundary (and it's instructions) and it's content.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文