收到时,PHP电子邮件附件为0字节文件大小

时间:2019-04-22 20:41:05

标签: php email attachment email-attachments

我有一个电子邮件附件脚本,该脚本可以工作并向我发送名称正确的文件...但是该文件为0字节。

这是php:

          $namer = $_FILES["cv_upload"]["name"];
          $file ="/home2/deserul7/public_html/nkaccounting/"."temp_cv/"."".$namer."";
          $contenttype = $_FILES["cv_upload"]['type'];
          $handle = fopen($file, "rb");
          $file_size = filesize($file);
          $content = fread($handle, $file_size);
          fclose($handle);

          $content = chunk_split(base64_encode($content));
          $uid = md5(uniqid(time()));
          $name = basename($file);

          $eol = PHP_EOL;

          // Basic headers
          $header = "From: NK Accounting <sal@desertsunstudio.com>".$eol;
          $header .= "MIME-Version: 1.0\r\n";
          $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"";

          // Put everything else in $message
          $message = "--".$uid.$eol;
          $message .= "Content-Type: text/html; charset=ISO-8859-1".$eol;
          $message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
          $message .= $msg2."<br><br><br>".$eol;
          $message .= "--".$uid.$eol;
          $message .= "Content-Type: ".$contenttype."; name=\"".$name."\"".$eol;
          $message .= "Content-Transfer-Encoding: base64".$eol;
          $message .= "Content-Disposition: attachment; filename=\"".$name."\"".$eol;
          $message .= $content.$eol;
          $message .= "--".$uid."--";

          if (@mail($ouremail, $subject2, $message, $header)){
            echo "sent";
          }

当我将其发送到gmail时,它可以正常工作,但是当我尝试通过桌面上的邮件应用程序获取它时,它的字节数为0 ...请帮助

1 个答案:

答案 0 :(得分:1)

在附件标题和附件内容之间应该有一个空白行。您那里只有一个$eol,所以没有空行(您为带有$msg2的零件正确地做了这行)。将Content-Disposition行更改为:

      $message .= "Content-Disposition: attachment; filename=\"".$name."\"".$eol.$eol;

如果使用here-string而不是串联,则可以更轻松地看到类似的内容。

$message = <<<EOF
--$uid
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit

$msg2<br><br><br>
--$uid
Content-Type: $contenttype; name="$name"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="$name"

$content
--$uid
EOF;