Error in creating Excel file and sending it by mail as attachment
<p>I want to create an Excel file and send it by email as an attachment. I'm stuck at the point that the file is to be attached as text in the email. I'm using the following code:</p>

<pre><code>  $filename = "daily_report.xls";
  $exists = file_exists('daily_report.xls');
  if($exists)
  {
     unlink($filename);
  }
  $filename = "daily_report.xls";
  $fp = fopen($filename, "wb");
  $sql = "select * from wp_county_order";
  $result = mysql_query($sql);
  $schema_insert = "";
  $schema_insert_rows = "";
  for ($i = 0; $i &lt; mysql_num_fields($result); $i++) 
  {
     $insert_rows .= mysql_field_name($result,$i) . "\t";
  }
  $insert_rows.="\n";
  fwrite($fp, $insert_rows);
  while($row = mysql_fetch_row($result)) 
  {
     $insert = $row[0]. "\t" .$row[1]. "\t".$row[2]. "\t".$row[3]. "\t".$row[4]."
               \t".$row[5]. "\t".$row[6]. "\t".$row[7];
     $insert .= "\n";               //       serialize($assoc)
     fwrite($fp, $insert);
  }
  if (!is_resource($fp))
  {
     echo "cannot open excel file";
  }
  echo "success full export";
  header("Content-type: application/vnd.ms-excel");
  header("Content-Disposition: attachment;Filename=daily_report.xls");
  $fp=fopen($filename,'rb');
  $data =fread($fp,filesize($filename));
  fclose($fp);
  $email_from = "vishal.dwivedi48@gmail.com";  // Who the email is from 
  $email_subject ="Daily Order Report";        // The subject of the email 
  $email_txt = "Thanks.";                      // Message that the email has in it 
  $email_to ="vishal.dwivedi48@gmail.com";     // Who the email is to
  $headers = "From: ".$email_from;
  $msg_txt="";
  $semi_rand = md5(time()); 
  $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
  $headers .= "\nMIME-Version: 1.0\n" . 
              "Content-Type: multipart/mixed;\n" . 
              " boundary=\"{$mime_boundary}\""; 
  $email_txt .= $msg_txt;
  $email_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" . 
                    $email_txt . "\n\n"; 
  $data = chunk_split(base64_encode($data)); 
  $email_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"; 
  $ok = @mail($email_to, $email_subject, $email_message, $headers); 
</code></pre>

<p>Please let me know how can I fix it, or if there is any other script that I can use. If I remove these headers from the code then it is showing an empty text file in the attachment:</p>

<pre><code>  header("Content-type: application/vnd.ms-excel");
  header("Content-Disposition: attachment;Filename=daily_report.xls");
</code></pre>
