title: CSV attachment in PHP
body: <p>I am trying to send a CSV file as an attachment in PHP and using mail function for attachments. The following code works fine. It successfully attaches the CSV file and sends it to the recipient but the ATTACHED output file (sent in email) comes in text format(.txt). I don't know where i am making mistake and what i need to change in header to retain original CSV file format in attached email.</p>
  
  <pre><code>        $path ="/myhost/public_html/csv/"; 
          $file_name = $path."Test";
          $file_name.=".csv";
          $from     = "some@myemail.co.uk";
          $to       = "other@hisemail.co.uk";
          $fat=$file_name;
          $subject  = $_POST[subject];
          //   $message  = $_POST[message];
          //replace \n with &lt;br&gt;
          $message = str_replace("\n", "&lt;br&gt;",$message);
          //report
          echo "&lt;b&gt;&lt;font color=#8080FF&gt; From: $from &lt;/b&gt;&lt;br&gt;";
          echo "&lt;b&gt;To: $to &lt;/b&gt;&lt;br&gt;";
          echo "&lt;b&gt;Subject: $subject&lt;/b&gt;&lt;br&gt;&lt;br&gt;&lt;/font&gt;";
          // Obtain file upload variables
          $fileatt      = $_FILES[$fat]['tmp_name'];
          $fileatt_type = $_FILES[$fat]['type'];
          $fileatt_name = $_FILES[$fat]['name'];
  
          $headers = "From: $from  \n";
  
  
          // if($_FILES['fileatt']['size'] &gt; 0)
          if (file_exists($fat)) {
             // Read the file to be attached ('rb' = read binary)
             $file = fopen($fat,'rb');
             $data = fread($file,filesize($fat));
             fclose($file);
             // Generate a boundary string
             $semi_rand = md5(time());
             $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
             // Add the headers for a file attachment
             $headers .= "MIME-Version: 1.0\n" .
                      "Content-Type: multipart/mixed;\n" .
                      " boundary=\"{$mime_boundary}\"";
             // Add a multipart boundary above the  message
             $message = "This is a multi-part message in MIME format.\n\n" .
             "--{$mime_boundary}\n" .
             "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
             "Content-Transfer-Encoding: 7bit\n\n" .
             $message . "\n\n";
  
             // Base64 encode the file data
             $data = chunk_split(base64_encode($data));
             // Add file attachment to the message
             $message .= "--{$mime_boundary}\n" .
             "Content-Type: {$fileatt_type};\n" .
             " name=\"{$fileatt_name}\"\n" .
             //"Content-Disposition: attachment;\n" .
             //" filename=\"{$fileatt_name}\"\n" .
             "Content-Transfer-Encoding: base64\n\n" .
             $data . "\n\n" .
             "--{$mime_boundary}--\n";
          } else echo "File error!  ";
  
          //send the mail
          if(mail($to, $subject, $message,$headers))echo "&lt;b&gt;&lt;font color=#FF0000&gt;Message was sent!&lt;b&gt;&lt;/font&gt;";
          else echo "&lt;b&gt;&lt;font color=#FF0000&gt;Message error!&lt;b&gt;&lt;/font&gt;";
  </code></pre>
  
  <p>I just want to retain original file format in email attachments. Help me please.</p>