saransh Posted April 25, 2019 Share Posted April 25, 2019 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 = '[email protected]'; $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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/308636-getting-blank-csv-file-in-email-attachedment/ Share on other sites More sharing options...
requinix Posted April 25, 2019 Share Posted April 25, 2019 You realize that you're creating a string (incorrectly, we'll get to that), writing it to a file, reading it right back out of that file, and then putting it into the email? Why bother with the file at all? But at least it's handy for the moment: have you checked if the file on your server has the data you think it should? Quote Link to comment https://forums.phpfreaks.com/topic/308636-getting-blank-csv-file-in-email-attachedment/#findComment-1566299 Share on other sites More sharing options...
saransh Posted April 25, 2019 Author Share Posted April 25, 2019 (edited) Hi requinix, Thanks for the reply,Yes file on the server has data. I think the issue is file permissions on server for read and write. But i am able to download data. Edited April 25, 2019 by saransh Quote Link to comment https://forums.phpfreaks.com/topic/308636-getting-blank-csv-file-in-email-attachedment/#findComment-1566305 Share on other sites More sharing options...
ginerjm Posted April 25, 2019 Share Posted April 25, 2019 Here is your code with some comments added: $sql = "SELECT * From abc"; $res = mysql_query($sql); $fileatt_type = "text/csv"; $myfile = "myfile.csv"; $handle = fopen($myfile, "w"); $str = ""; // Get the data from the query results while($row = mysql_fetch_array($res)) { // only saving the last row in the $str variable $str = $row['username'].",".$row['mobile']."\n"; } fwrite($handle, $str); fclose($handle); //******************* // Read the newly made file back in? $file_size = filesize($myfile); // re-open the file for WRITING again??? $handle = fopen($myfile, "w"); $content = fread($handle, $file_size); fclose($handle); // break up the just-read content and encrypt it? $content = chunk_split(base64_encode($content)); $uid = md5(uniqid(time())); // Start building the email needs $to = '[email protected]'; $subject = 'Test email with attachment'; $message = 'welocme'; $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"; } Problems - 1 - STOP using the mysql extension since it is no longer valid. Switch to PDO (if available) or else go to mysqlI for your database needs. 2 - Learn how to build a string using concatenation. You aren't building the output message into $str if that is what you think you are doing. 3 - You aren't creating a message for your email. You simply put the word 'welocme' into $message. PS - why are you including $message in your $headers? 4 - You seem to be writing out the data to a file but then you turn around and attempt to read it back in (using a "write" mode?) 5 - Not sure how a multi-part message is meant to be built but I'm going to guess that you don't insert the message body into the headers string. My guess is that you build the message using the different parts, separating them with your $uid value that you define in the header. 6 - Note how neatly my block of code is written. Makes for easier reading, no? And adding comments helps keep your thought processes clearly understood when you come back for maintenance later on. Quote Link to comment https://forums.phpfreaks.com/topic/308636-getting-blank-csv-file-in-email-attachedment/#findComment-1566306 Share on other sites More sharing options...
saransh Posted April 26, 2019 Author Share Posted April 26, 2019 Hi ginerjm Thanks for the reply. still getting the same output after following your comments Quote Link to comment https://forums.phpfreaks.com/topic/308636-getting-blank-csv-file-in-email-attachedment/#findComment-1566328 Share on other sites More sharing options...
ginerjm Posted April 26, 2019 Share Posted April 26, 2019 "after following your comments" So - what exactly did you do? Post it perhaps? We are not mind-readers so nobody knows what your code looks like now. Maybe you could add some echo statements to output values that are important to the process so that YOU can verify that what you think is happening is really happening. That's how real programmers debug things that are puzzling them. Quote Link to comment https://forums.phpfreaks.com/topic/308636-getting-blank-csv-file-in-email-attachedment/#findComment-1566335 Share on other sites More sharing options...
kicken Posted April 26, 2019 Share Posted April 26, 2019 (edited) If you look at the list of file open modes, you'll see that mode w is Quote 'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. So when you re-open your file you're deleting everything you'd previously written to it. Mode w also doesn't allow reading so any fread call would return nothing anyway. You want mode r to read back your contents. As requinix mentioned though, the whole file bit seems pointless if you're just going to write data to it then read it all back out. Edited April 26, 2019 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/308636-getting-blank-csv-file-in-email-attachedment/#findComment-1566343 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.