Jump to content

Getting blank csv file in email attachedment


saransh

Recommended Posts

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";
                }
                
                ?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 = 'abc@gmail.com';
$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.

Link to comment
Share on other sites

"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.

Link to comment
Share on other sites

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 by kicken
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.