title: PHP multiple attachment email shows up as text
body: <p>=======UPDATE========</p>
  
  <p>The problem ended up being that I was using the header ContentType when I should have been using Content-Type.</p>
  
  <p>Thanks for your help guys.</p>
  
  <p>Derp derp derp.</p>
  
  <p>=======/UPDATE=======</p>
  
  <p>I apologize in advance for the obnoxiously long question.</p>
  
  <p>I'm attempting to send emails with attachments through PHP. I've done this successfully before with a single attachment. But I can't get it to work with multiple attachments, or for that matter now with a single attachment using this method. So, what am I doing wrong?</p>
  
  <p>I understand that there are other libraries for these functions available. But I want to know WHY this one is not working.</p>
  
  <p>Here is the method that generates the actual email:</p>
  
  <pre><code>function gen_mail ()
  {
      $files = $this-&gt;files();
      $has_attachments = (bool) $files;
  
      $EOL = "\r\n"; // tried \n, PHP_EOL
      $message = $this-&gt;message;
  
      $headers = $this-&gt;gen_headers();
      $headers .= "MIME-Version: 1.0$EOL";
  
      if ($has_attachments) {
          $hash = md5(time());
          $sep = "=={$hash}==";
          $headers .= "ContentType: multipart/mixed; boundary=\"$sep\"$EOL$EOL";
          $headers .= "This is a MIME-encoded message.$EOL$EOL";
          $headers .= "--$sep$EOL";
      }
  
      $headers .= "Content-Type: text/html; charset=\"iso-8859-1\"$EOL";
      $headers .= "Content-Disposition: inline$EOL";
      $headers .= "Content-Transfer-Encoding: 7bit$EOL$EOL";
      $headers .= "$message$EOL$EOL";
  
      if ($has_attachments) { // here go attachments
          foreach ($files as $key =&gt; $value) {
              $filename = is_numeric($key)? basename($value) : $key;
              $file = fopen($value, 'rb');
              $data = fread($file, filesize($value));
                              fclose($file);
              $content = chunk_split(base64_encode($data));
              $headers .= "--$sep$EOL";
              $headers .= "Content-Type: application/octet-stream; ";
              $headers .= "name=\"$filename\"$EOL";
              $headers .= "Content-Transfer-Encoding: base64$EOL";
              // tried Content-Disposition, attachment, and every combination
              $headers .= "Content-Description: inline; $EOL ";
              $headers .= "filename=\"$filename\"$EOL$EOL";
              $headers .= "$content$EOL$EOL";
          }
          $headers .= "--$sep--$EOL$EOL";
      }
      return $headers;
  }
  </code></pre>
  
  <p>And this is what the headers returned by the above function look like:</p>
  
  <pre><code>From: test@urbido.es
  MIME-Version: 1.0
  ContentType: multipart/mixed; boundary="==cd42448edf10bca4640bc7d6365e1961=="
  
  This is a MIME-encoded message.
  
  --==cd42448edf10bca4640bc7d6365e1961==
  Content-Type: text/html; charset="iso-8859-1"
  Content-Disposition: inline
  Content-Transfer-Encoding: 7bit
  
  Travis, you are so cool!!!1!
  
  --==cd42448edf10bca4640bc7d6365e1961==
  Content-Type: application/octet-stream; name="html.login.php"
  Content-Transfer-Encoding: base64
  Content-Description: inline; 
   filename="html.login.php"
  
  PD9waHANCgkNCiRoZWFkZXJfY29uZmlnID0gYXJyYXkoKTsNCiRtc2cgPSAnUmVnaXN0ZXJlZCBP
  cGVyYXRvcnMsIGVudGVyIHlvdXIgT3BlcmF0b3IgSUQgYW5kIHBhc3N3b3JkIHRvIGxvZ2luJzsN
  CiRtc2dfY2xhc3MgPSAnbm9ybWFsJzsNCmlmICgkbG9nZ2VkX2luKSB7DQoJJGhlYWRlcl9jb25m
  aWdbJ2NvbnRyb2xzJ10gPSBhcnJheSAoDQoJCWFycmF5ICgndGV4dCcgPT4gJ1N1Ym1pdCBFbnRy
  aWVzJywgJ2hyZWYnID0+ICdlbnRyeScpLA0KCSk7DQoJJG1zZyA9ICdZb3UgYXJlIGxvZ2dlZCBp
  bi4gVXNlIHRoZSBhYm92ZSBsaW5rcywgb3IgDQoJCWNsaWNrIDxhIGhyZWY9ImVudHJ5Ij5oZXJl
  PC9hPiB0byBiZWdpbiBvciByZXN1bWUgYSBqb2InOw0KCSRtc2dfY2xhc3MgPSAnc3VjY2Vzcyc7
  DQp9DQoNCj8+
  
  
  --==cd42448edf10bca4640bc7d6365e1961==--
  </code></pre>
  
  <p>I've tried functions written by other people, and they produce seemingly identical output. But when I send those headers with
      mail($address, $subject, '', $headers)</p>
  
  <p>I get this in my inbox:</p>
  
  <pre><code>This is a MIME-encoded message.
  
  --==cd42448edf10bca4640bc7d6365e1961==
  Content-Type: text/html; charset="iso-8859-1"
  Content-Disposition: inline
  Content-Transfer-Encoding: 7bit
  
  Travis, you are so cool!!!1!
  
  --==cd42448edf10bca4640bc7d6365e1961==
  Content-Type: application/octet-stream; name="html.login.php"
  Content-Transfer-Encoding: base64
  Content-Description: inline;
   filename="html.login.php"
  
  PD9waHANCgkNCiRoZWFkZXJfY29uZmlnID0gYXJyYXkoKTsNCiRtc2cgPSAnUmVnaXN0ZXJlZCBP
  cGVyYXRvcnMsIGVudGVyIHlvdXIgT3BlcmF0b3IgSUQgYW5kIHBhc3N3b3JkIHRvIGxvZ2luJzsN
  CiRtc2dfY2xhc3MgPSAnbm9ybWFsJzsNCmlmICgkbG9nZ2VkX2luKSB7DQoJJGhlYWRlcl9jb25m
  aWdbJ2NvbnRyb2xzJ10gPSBhcnJheSAoDQoJCWFycmF5ICgndGV4dCcgPT4gJ1N1Ym1pdCBFbnRy
  aWVzJywgJ2hyZWYnID0+ICdlbnRyeScpLA0KCSk7DQoJJG1zZyA9ICdZb3UgYXJlIGxvZ2dlZCBp
  bi4gVXNlIHRoZSBhYm92ZSBsaW5rcywgb3IgDQoJCWNsaWNrIDxhIGhyZWY9ImVudHJ5Ij5oZXJl
  PC9hPiB0byBiZWdpbiBvciByZXN1bWUgYSBqb2InOw0KCSRtc2dfY2xhc3MgPSAnc3VjY2Vzcyc7
  DQp9DQoNCj8+
  
  
  --==cd42448edf10bca4640bc7d6365e1961==--
  </code></pre>