PHP邮件附件为空

发布于 2024-08-17 19:29:35 字数 1703 浏览 4 评论 0原文

我收到了接受附件并发送附件的表单,但如果它是文档,则它是空白的:

   $fileatt = $_FILES['file']['tmp_name'];
   $fileattType = $_FILES['file']['type'];
   $fileattName = $_FILES['file']['name'];
   $file = fopen($fileatt,'rb');
   $data = fread($file,filesize($fileatt));
   fclose($file);

   $semi_rand = md5(time());
   $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

   $headers = "from: $email";

   $headers .= "\nMIME-Version: 1.0\n" .
        "Content-Type: multipart/mixed;\n" .
        " boundary=\"{$mime_boundary}\"";

   $content = "Info about user";

   $content = "This is a multi-part message in MIME format.\n\n" .
        "--{$mime_boundary}\n" .
        "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
        "Content-Transfer-Encoding: 7bit\n\n" .
        $content . "\n\n";

   $data = chunk_split(base64_encode($data));

   $content .= "--{$mime_boundary}\n" .
        "Content-Type: {$fileattType};\n" .
        " name=\"{$fileattName}\"\n" .
        "Content-Disposition: attachment;\n" .
        " filename=\"{$fileattName}\"\n" .
        "Content-Transfer-Encoding: base64\n\n" .
        $data . "\n\n" .
        "--{$mime_boundary}--\n";

   $send = mail( "[email protected]", "Form", $content, $headers);
         if($send)
            header('Location: success page');
         else
            header('Location: fail page');

电子邮件发送正确,有一个具有正确名称、文件类型和文件大小的附件。当它打开时它是空的。

我从 sitepoint 获得了教程: http://articles.sitep... ... mail-php/5

我测试了他们提供的文件,同样的事情发生了。任何想法都会很棒。谢谢大家!

I got the form to accept an attachment and send the attachment but if it's a doc it's blank:

   $fileatt = $_FILES['file']['tmp_name'];
   $fileattType = $_FILES['file']['type'];
   $fileattName = $_FILES['file']['name'];
   $file = fopen($fileatt,'rb');
   $data = fread($file,filesize($fileatt));
   fclose($file);

   $semi_rand = md5(time());
   $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

   $headers = "from: $email";

   $headers .= "\nMIME-Version: 1.0\n" .
        "Content-Type: multipart/mixed;\n" .
        " boundary=\"{$mime_boundary}\"";

   $content = "Info about user";

   $content = "This is a multi-part message in MIME format.\n\n" .
        "--{$mime_boundary}\n" .
        "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
        "Content-Transfer-Encoding: 7bit\n\n" .
        $content . "\n\n";

   $data = chunk_split(base64_encode($data));

   $content .= "--{$mime_boundary}\n" .
        "Content-Type: {$fileattType};\n" .
        " name=\"{$fileattName}\"\n" .
        "Content-Disposition: attachment;\n" .
        " filename=\"{$fileattName}\"\n" .
        "Content-Transfer-Encoding: base64\n\n" .
        $data . "\n\n" .
        "--{$mime_boundary}--\n";

   $send = mail( "[email protected]", "Form", $content, $headers);
         if($send)
            header('Location: success page');
         else
            header('Location: fail page');

The email sends correctly, there is an attachment with the correct name, file type, and file size. when it opens it is empty.

I got the tutorial from sitepoint: http://articles.sitep... ... mail-php/5

I tested the files they provide and the same thing happens. Any ideas would be great. thanks everyone!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

無心 2024-08-24 19:29:35

为什么不使用一个像样的库来发送电子邮件呢?

我强烈建议您开始使用库来发送电子邮件,它简化了编写电子邮件的过程并使代码更加短暂。

我可以为 Swiftmailer 提供担保,它是一个积极开发的库,具有出色的功能。

特别复杂的任务,如附件、内嵌图像和多个收件人,都可以轻松实现。

Why not use a decent library for sending emails?

I strongly suggest you start using a library for sending emails, it eases the process of writing emails and makes the code a lot more transient.

I can vouch for Swiftmailer, an actively developed library with excellent features.

Especially complicated tasks like attachments, inline images and multiple recipients are implemented easily.

那请放手 2024-08-24 19:29:35

强烈建议使用现有工具发送电子邮件,例如Swift。它会为您解决很多注意事项,并且非常易于使用。

I would strongly recommend using a preexisting tool to send emails, such as Swift. There are a lot of caveats that it will take care of for you, and it's quite easy to use.

箜明 2024-08-24 19:29:35

欢迎来到SO。

这不是对您问题的直接答案,但我建议您使用现成的邮件类,例如 PHPMailer。它已经内置了所有的怪癖、编码和附加功能,这比手工操作更不容易出错。如果你想这样做,也许这个集合PHPMailer 上的教程链接将帮助您入门。

Welcome to SO.

This is not a direct answer to your question, but I would recommend you use a ready-made mailing class like PHPMailer. It has all the quirks, encoding and attaching functions already built in, which is much less error-prone than doing it by hand. If you want to go that way, maybe this collection of tutorial links on PHPMailer will help you get started.

半暖夏伤 2024-08-24 19:29:35

也许您可以使用 \r\n 而不是那些 \n\n

Perhaps you could use \r\n instead of those \n\n

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