Hi,
I am new to this forum,
I am trying to create a php script to email and attached database table data in csv file.
Below is the script which i have created but i am received only blank csv file in attachment.
I have no clue where i committed mistake.
Please help!
<?php
$sql = "SELECT * From abc";
$res = mysql_query($sql);
$fileatt_type = "text/csv";
$myfile = "myfile.csv";
$handle = fopen($myfile, "w");
$str = "";
while($row = mysql_fetch_array($res))
{
$str = $row['username'].",".$row['mobile']."\n";
}
fwrite($handle, $str);
fclose($handle);
$file_size = filesize($myfile);
$handle = fopen($myfile, "w");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$to = 'abc@gmail.com';
$subject = 'Test email with attachment';
$message = 'welocme';
#$header = "From: ".$from_name." <".$from_mail.">\r\n";
#$header .= "Reply-To: ".$replyto."\r\n";
$header = "From: abc.com\r\n Reply-To: From: abc.com";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/html; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: text/csv; name=\"".$myfile."\"\r\n"; // use diff. tyoes here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$myfile."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
if (mail($to, $subject, $message, $header)){
echo "Mail send";
} else{
echo "can not send mail";
}
?>