title: image file attachment on email not sent properly through PHP
body: <p>I'm using the <code>mail()</code> function to test sending emails with image file attachments to myself.
  The problem with the script is that when I look at the email, I see a long wall of random numbers, letters, and symbols where an image is supposed to be. </p>
  
  <p>Please don't recommend PHPmailer or manually putting in an image tag in the message body.</p>
  
  <p>The main PHP file I'm using:</p>
  
  <pre><code>&lt;?
  
   include('init.php'); 
  
  if (isset($_POST['name'], $_FILES['file'])){
  
      $body = &lt;&lt;&lt;BODY
      From: {$_POST['name']}
  
      Details: 
          Name:   {$_FILES['file']['name']}
          Size:   {$_FILES['file']['size']}
          Type:   {$_FILES['file']['type']}
  
  BODY;
  
  mail_file('receiver@email.com','test@php.com','a file upload',$body,$_FILES['file']);   
  }
  
  ?&gt;
  
  &lt;form action="" method="POST" enctype="multipart/form-data"&gt;
     &lt;input type="text" name="name" id="name"&gt;
     &lt;input type="file" name="file" id="file"&gt;
     &lt;input type="submit" value="Email File"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>init.php:</p>
  
  <pre><code>&lt;?
  
  $path = dirname(__FILE__);
  include("{$path}/mail.php");
  
  ?&gt;
  </code></pre>
  
  <p>mail.php:</p>
  
  <pre><code>&lt;? 
  
  function mail_file($to, $from, $subject, $body, $file){
      $boundary = md5(rand());
  
      $headers = array(
          'MIME-Version: 1.0',
          "Content-Type: multipart/mixed; boundary=\"{$boundary}\"",
          "From: {$from}"
      );
  
      $message = array(
          "--{$boundary}",
          'Content-Type: text/html',
          'Content-Transfer-Encoding: 7bit',
          '',
          chunk_split($body),
          "--{$boundary}",
          "Content-Type: {$file['type']}; name=\"{$file['name']}\"",
          "Content-Disposition: attachment; name=\"{$file['name']}\"",
          "Content-Transfer-Encoding: base64",
          '',
          chunk_split(base64_encode(file_get_contents($file['tmp_name']))),
          "--{$boundary}--"
      );
  
      mail($to, $subject, implode('\r\n', $message), implode('\r\n', $headers));
  }
  ?&gt;
  </code></pre>
  
  <p>Example of an email sent:</p>
  
  <pre><code>--4f8b4f63eb5fb4f1c3cde903c7e5b306\r\nContent-Type: text/html\r\nContent-Transfer-Encoding: 7bit\r\n\r\n    From: asas
  
  Details: 
  Name: 728x90-Brink-Banner-V2.jpg
  Size:   41583
  
  Type:   image/jpeg
  
  \r\n--4f8b4f63eb5fb4f1c3cde903c7e5b306\r\nContent-Type: image/jpeg; name="728x90-Brink-Banner-V2.jpg"\r\nContent-Disposition: attachment; name="728x90-Brink-Banner-V2.jpg"\r\nContent-Transfer-Encoding: base64\r\n\r\n/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAAUAAA/+4ADkFkb2JlAGTAAAAAAf/b
  AIQAAgICAgICAgICAgMCAgIDBAMCAgMEBQQEBAQEBQYFBQUFBQUGBgcHCAcHBgkJCgoJCQwMDAwM
  DAwMDAwMDAwMDAEDAwMFBAUJBgYJDQsJCw0PDg4ODg8PDAwMDAwPDwwMDAwMDA8MDAwMDAwMDAwM
  DAwMDAwMDAwMDAwMDAwMDAwM/8AAEQgAWgLYAwERAAIRAQMRAf/EAOgAAAAGAwEAAAAAAAAAAAAA
  AAMEBQYHCAECCQABAAAHAQEBAAAAAAAAAAAAAAABAgMEBQYHCAkQAAEDAgUBBgMDBAsHDA0NAQEC
  
  
  
  ...
  
  
  L81/7CT7TfOuj+7T/wCxbf6cmeeXwouVG4P5we/F/E/yvzHTHMh6q9MNzUc7m/T5X4f4h6fP+6uF
  jIJbcyh7Z0b6/h/d6fbhYSHp7D+cIXk/RYHy/RU9P+y/j6sIk9VBmYV1+H+sf9B6Dz+b7cZufPat
  RB6oyV2LP+B/6NfhD8X8X7Ph34OPJMy+so95T/RGP/NzzD+bfxuuFyeoozf1ozUXWfoz+n/jJ/Rv
  P9n8XvxTvzWktcx8eSvxxR+Gn9P6p83k7Ov72JFl4VE1T1tngUtb2/mmd+H+iu/jfyD1xZyZKqhz
  VHdz/wA4H9F/Aa6/h/oKPxP+y+GKmX4eNXtuv//Z
  \r\n--4f8b4f63eb5fb4f1c3cde903c7e5b306--
  </code></pre>