The email attachment for the csv file keeps ringing up 0 bytes in the filesize. I can get it to download via browser, but for some reason it won't send the file to email.

It sends the attachment, but for some reason it turns out to be a blank .csv file. What are my problems in the code?

<?
// generate table.csv file
header("Content-type: application/octet-stream"); 
header("Content-Disposition: attachment; filename=table.csv"); 
header("Content-Transfer-Encoding: binary"); 

$query = "SELECT * FROM datainfo"; 
$result = mysql_query($query) or die(mysql_error()); 
$numOfRows = mysql_num_rows($result); 

for ($i = 0; $i < $numOfRows; $i++){ 
   $id    = mysql_result ($result, $i, ID);
   $ageverification    = mysql_result ($result, $i, ageverification); 
   $creditchecking      = mysql_result ($result, $i, creditchecking); 
   $First_Name      = mysql_result ($result, $i, First_Name); 
   $Last_Name    = mysql_result ($result, $i, Last_Name); 
   $email      = mysql_result ($result, $i, email); 
   $DayPhone   = mysql_result ($result, $i, DayPhone); 
   $EvePhone      = mysql_result ($result, $i, EvePhone); 
   $Address      = mysql_result ($result, $i, Address); 
   $City      = mysql_result ($result, $i, City); 
   $State      = mysql_result ($result, $i, State); 
   $Zip      = mysql_result ($result, $i, Zip);   

   print "$id\",\"$ageverification\",\"$creditchecking\",\"$First_Name\",\"$Last_Name\",\"$email\",\"$DayPhone\",\"$EvePhone\",
   \"$Address\",\"$City\",\"$State\",\"$Zip\"\"\n"; 
} 

$file = "table.csv";
$file_source = "/home/browngai/public_html/testing/table.csv"; 

$adminemail = "persona@animeorganizations.com, [email]shinmeiryu_san@yahoo.com[/email]";
$subject = 'Weekly csv imported file for today: '.date("l, F jS Y").'';
$message = "
To the admin,

Here is your csv imported as an e-mail attachment.";
$message.= "\n \n ================= \n \n"; 
// set up the attachment
$headers .=  "From: Properhosting Webmaster<webmaster@properhosting.com>\n 
  X-Mailer: PHP/" . phpversion();
$headers .= "MIME-Version: 1.0\r\n"; 
$headers .= "Content-Type: multipart/mixed; boundary=\"CCM-12345\"\r\n\r\n"; 
$headers .= "This is a MIME encoded message\r\n\r\n"; 
$headers .= "--CCM-12345\r\n"; 
$headers .= "Content-Type: text/plain; charset=\"us-ascii\"\r\n"; 
$headers .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; 
$headers .= "Please find attached file\r\n\r\n"; 
$headers .= "$message\r\n\r\n"; 
$headers .= "--CCM-12345\r\n"; 
$headers .= "Content-Type: application/octet-stream; name=\"$file\"\r\n"; 
$headers .= "Content-Transfer-Encoding: base64\r\n"; 
$headers .= "Content-Disposition: attachment; filename=\"$file\"\r\n\r\n"; 
set_magic_quotes_runtime(0); 
$fp = fopen($file_source, "rb"); 
$fstr = fread($fp, filesize($file_source)); 
$headers .= chunk_split(base64_encode($fstr),72); 
fclose($fp); 
set_magic_quotes_runtime(get_magic_quotes_gpc()); 
$headers .= "\r\n"; 
$headers .= "--CCM-12345--\r\n\r\n\r\n"; 

echo "<pre>$headers</pre>"; 

// send the email out to administrators
mail($adminemail, $subject, $message, $headers); 
?>

    Would anyone know of the problem?

    All I know of is that it's not printing out the table.csv into a file for attachment.

      This is totally wrong - this is not the way to create the csv!
      See the code below - you will need to change quite a few things like links to the file etc, as I've just copied it from one of my projects.

       
      include ('/home/sites/'.$HTTP_HOST.'/web/_includes/connect.php');
      $file = "".date("Ymd")."";
      $filename4refundscsv = "/home/sites/$HTTP_HOST/csv/refunds/$file.csv";
      fopen ($filename4refundscsv, "w+");
      $somecontentcsv = "";
      $cursorcsv = mysql_query("SELECT * FROM data");
      if (!$cursorcsv) {  $somecontent.="<tr><th>MySQL error: " . mysql_error() . "</th></tr>";} 
      while ( $row = mysql_fetch_array($cursorcsv) )
      {
      $ageverification = $row["ageverification"];
      //Insert rest of vars here
      $somecontentcsv.="$id,$ageverification,$creditchecking,$First_Name,$Last_Name,$email,$DayPhone,$EvePhone, $Address,$City,$State,$Zip\n"; 
      }
      if (is_writable($filename4refundscsv)) {
      if (!$handlecsv = fopen($filename4refundscsv, 'a')) {
      print "Cannot open file ($filename4refundscsv)";
      exit;
      }
      if (!fwrite($handlecsv, $somecontentcsv)) {
      print "Cannot write to file ($filename4refundscsv)";
      exit;
      }
      fclose($handlecsv);
      }
      //Then Mail it

        Actually my code does create the csv, and your way does too. That's not the problem I'm facing, the problem I'm facing is that it does not attach the .csv file to the email I'm sending.

        Either way I try I still keep getting failed to open stream and permission denial errors. No matter what path to the file that I use.

        I'm currently using this as the path: $file_source = "/home/browngai/public_html/testing/$file.csv";

        or in your way: $filename4refundscsv = "/home/browngai/public_html/testing/$file.csv";

          Oh, ok - sorry about that!! 🙂
          Anyway - this is a script I use to send attachments with emails.

          class sendmail
          	{
          		// Variable deklarieren
          		var $emailheader = "";
          		var $textheader = "";
          		var $textboundary = "";
          		var $emailboundary = "";
          		var $betreff = "";
          		var $empfaenger = "";
          		var $attachment = array();
          
          	function sendmail()
          	{
          		$this->textboundary = uniqid(time());
          		$this->emailboundary = uniqid(time());
          	}
          
          	// Von wem die Email kommt in den Header setzen
          	function from($name,$email)	
          	{
          		$this->emailheader .= "From: \"Surname, Firstname\" <email@domain.com>\n";
          		$this->emailheader .= "MIME-Version: 1.0\n";
          	}
          
          	// Funktion um den Adressaten anzugeben
          	function to($to)
          	{
          		$this->empfaenger = $to;
          	}
          	// Funktionn für den Betreff anzugeben
          	function subject($subject)
          	{
          		$this->betreff = $subject;
          	}
          
          	// Textdaten in Email Header packen
          	function text($text)
          	{
          		$this->textheader .= "Content-Type: multipart/alternative; boundary=\"$this->textboundary\"\n\n";
          		$this->textheader .= "--$this->textboundary\n";
          		$this->textheader .= "Content-Type: text/plain; charset=\"ISO-8859-1\"\n";
          		$this->textheader .= "Content-Transfer-Enconding: quoted-printable\n\n";
          		$this->textheader .= strip_tags($text)."\n\n";
          		$this->textheader .= "--$this->textboundary\n";
          		$this->textheader .= "Content-Type: text/html; charset=\"ISO-8859-1\"\n";
          		$this->textheader .= "Content-Transfer-Enconding: quoted-printable\n\n";
          		$this->textheader .= "<html><body>$text</body></html>\n\n";
          		$this->textheader .= "--$this->textboundary--\n\n";
          	}
          
          	// Funktion zum anhängen für Attachments in der Email
          	function attachment($datei,$FileName)
          	{
          		// Überprüfen ob File Existiert
          		if(is_file($datei))		
          		{
          			// Header für Attachment erzeugen
          			$attachment_header = "--".$this->emailboundary."\n" ;
          			$attachment_header .= "Content-Type: application/octet-stream;\n name=\"$FileName\"\n";
          			$attachment_header .= "Content-Transfer-Encoding: base64\n";
          			$attachment_header .= "Content-Disposition: attachment;\n filename=\"$FileName\"\n\n";
          			// Daten der Datei einlesen, in das BASE64 Format formatieren und auf max 72 Zeichen pro Zeile
          			// aufteilen
          			$file['inhalt'] = fread(fopen($datei,"rb"),filesize($datei));
          			$file['inhalt'] = base64_encode($file['inhalt']);
          			$file['inhalt'] = chunk_split($file['inhalt'],72);
          
          			// Attachment mit Header in der Klassenvariable speichern
          			$this->attachment[] = $attachment_header.$file['inhalt']."\n";
          		}
          		else
          		{
          			echo "Die Datei $datei existiert nicht...\n";
          		}
          	}
          
          	// Funktion zum erstellen des Kompletten Headers der Email	
          	// Senden der Email
          	function send()	
          	{
          		$header = $this->emailheader;
          
          		// Überprüfen ob Attachments angehängt wurden			
          		if(count($this->attachment)>0)
          		{
          			$header .= 	"Content-Type: multipart/mixed; boundary=\"$this->emailboundary\"\n\n";
          			$header .= "--$this->emailboundary\n";
          			$header .= $this->textheader;
          
          			for($i=0;$i<count($this->attachment);$i++)
          			{				
          				$header .= $this->attachment[$i];
          			}
          
          			$header .= "--$this->emailboundary--";
          		}
          		else
          		{
          			$header .= $this->textheader;
          		}
          		// Versenden der Mail
          		mail("$this->empfaenger",$this->betreff,"",$header);
          	}
          }
          $mail = new sendmail();
          $mail->from("Surname, Firstname","email@domain.com");
          $mail->to("email@domain.com");
          $mail->subject("Email Subject");
          $mail->text("Email Body Text (If Any, if not leave space)");
          $mail->attachment("../csv/hallofshame/$date4hos.csv", "DailyHallOfShame.csv");
          $mail->send();
          

          Hope this helps

            Write a Reply...