-1

i have an email attachment script that does work and sends me a file with the correct name...however the file is 0 bytes.

here is the 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";
          }

when i send it to my gmail it works fine but when i try to get it thru my mail app in my desktop it comes in at 0 bytes...please help

sallycakes
  • 176
  • 2
  • 11
  • php's mail() is rather under featured, your a lot better off using the likes of phpmailer –  Apr 22 '19 at 21:19
  • Are you sure `$content` has anything in it? What does `echo strlen($content);` show? – Barmar Apr 22 '19 at 22:13
  • @Barmar it echos out 12514; im unsure what this means – sallycakes Apr 22 '19 at 22:32
  • @tim this is for my portfolio/learning...ive struggled alot just to get the file to actually move from the tmp folder, id rather not use a library at this moment – sallycakes Apr 22 '19 at 22:33
  • 1
    well no one actually uses mail for much, so your not going to learn anything you would actual want to use a a real world situation –  Apr 22 '19 at 22:39
  • @tim It's not like he needs `mail` to do much, it just has to send the message that he gives it. – Barmar Apr 22 '19 at 23:41
  • @sallycakes What are you unsure about? `strlen()` returns the length of a string, so that means `$content` has 12514 characters in it, so it's not really empty. – Barmar Apr 22 '19 at 23:42
  • Have you looked at the raw source of the email when you receive it, to see if the attachment is in there? – Barmar Apr 22 '19 at 23:42
  • I think you need two `$eol` between the `Content-Disposition` line and `$content`. – Barmar Apr 22 '19 at 23:43
  • If it "works fine" in GMail but not a local "mail app" then I'd be looking somewhere other than your PHP code. Local virus software, ISP policies, etc. If this is something like a Word document, you're likely to have those sorts of issues. – miken32 Apr 22 '19 at 23:51
  • @miken32 I try most of the time, occasionally I slip up. Sorry. – Barmar Apr 22 '19 at 23:57
  • @miken32 I suspect that Gmail is "fixing" the error that I noticed regarding the missing blank line. – Barmar Apr 22 '19 at 23:59
  • @miken32 Yeah, I noticed now -- I don't usually make a note of usernames. – Barmar Apr 22 '19 at 23:59
  • @Barmar two $eol was the fix thank you!! also sorry im still kind of a noob in PHP – sallycakes Apr 23 '19 at 01:27

1 Answers1

1

There's supposed to be a blank line between the attachment header and the attachment contents. You only have one $eol there, so there's no blank line (you did it correctly for the part with $msg2). Change the Content-Disposition line to:

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

You can see things like this more easily if you use a here-string instead of concatenation.

$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;
Barmar
  • 741,623
  • 53
  • 500
  • 612