Creating + Sending .csv on the fly

Discussion in 'PHP' started by Denvar, Apr 8, 2007.

  1. #1
    Hey guys I have a variable $csvdata which has the following value :

    $cr = "\n";
    $csvdata = "First Name" . ',' . "Last Name" . ' . $cr;
    $csvdata .= $txtFName . ',' . $txtLName . ' . $cr;


    I now want to send an email to myself with a .csv attachment containing this data, however I want the attachment to be created on the fly.

    Any ideas?
     
    Denvar, Apr 8, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Have a look at base64_encode, and the comments below. There's an example on how to mail base64 encoded strings as attachment.
     
    nico_swd, Apr 9, 2007 IP
  3. Denvar

    Denvar Peon

    Messages:
    308
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I couldnt work out how to solve my problem with the link, sorry nicro.

    What I have so far is a php file that will be used to send the email (with the csv file attached) and a php file doign the following :

    <?php
    //Define a carriage return
    $cr = "\n";

    //Set the column names for the CSV file
    $csvcontent = "First Name" . ',' . "Last Name" . $cr;

    //Enter the values for each column
    $csvcontent .= $txtFName . ',' . $txtLName . $cr;

    header("Content-type: application/vnd.ms-excel");
    header("Content-disposition: attachment; filename=temp.csv");

    echo $csvcontent;
    ?>

    As i'm lead to believe, this code will create a temp.csv file on-the-fly (dynamically without saving to the server)

    I now need the mailing code to send an email to with the temp.csv as an attachment, using a 'require' to include the .csv creation php file.



    anyone?
     
    Denvar, Apr 9, 2007 IP
  4. Denvar

    Denvar Peon

    Messages:
    308
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    no one?

    any suggestions other then the code i have so far will also be welcomed
     
    Denvar, Apr 10, 2007 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    Give this a try:

    
    <?php
    
    
    $cr = "\n";
    $csvdata = "First Name" . ',' . "Last Name"  . $cr;
    $csvdata .= $txtFName . ',' . $txtLName . $cr;
    
    $thisfile = 'file.csv';
    
    $encoded = chunk_split(base64_encode($csvdata));
    
    // create the email and send it off
    
    $subject = "File you requested from RRWH.com";
    $from = "scripts@rrwh.com";
    $headers = 'MIME-Version: 1.0' . "\n";
    $headers .= 'Content-Type: multipart/mixed;
        boundary="----=_NextPart_001_0011_1234ABCD.4321FDAC"' . "\n";
    
    $message = '
    
    This is a multi-part message in MIME format.
    
    ------=_NextPart_001_0011_1234ABCD.4321FDAC
    Content-Type: text/plain;
            charset="us-ascii"
    Content-Transfer-Encoding: 7bit
    
    Hello
    
    We have attached for you the PHP script that you requested from http://rrwh.com/scripts.php
    as a zip file.
    
    Regards
    
    ------=_NextPart_001_0011_1234ABCD.4321FDAC
    Content-Type: application/octet-stream;  name="';
    
    $message .= "$thisfile";
    $message .= '"
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment; filename="';
    $message .= "$thisfile";
    $message .= '"
    
    ';
    $message .= "$encoded";
    $message .= '
    
    ------=_NextPart_001_0011_1234ABCD.4321FDAC--
    
    ';
    
    // now send the email
    mail($email, $subject, $message, $headers, "-f$from");
    
    ?>
    
    PHP:
     
    nico_swd, Apr 10, 2007 IP
    Denvar likes this.
  6. Denvar

    Denvar Peon

    Messages:
    308
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Amazing, simply amazing, took me several days of hacking, yet you seem to of solved the issue quickly, thanks a lot!, repped
     
    Denvar, Apr 10, 2007 IP
  7. Denvar

    Denvar Peon

    Messages:
    308
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I tried this, and it worked fine! The first time...

    Now I am getting an empty email, the from + subject works fine, but i am not getting any email body and no attachment, any ideas?

    here is the code :

     
    Denvar, Apr 10, 2007 IP
  8. Denvar

    Denvar Peon

    Messages:
    308
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Ahh I fixed it, doesnt like the header/message code being indented..
     
    Denvar, Apr 10, 2007 IP
  9. sancury

    sancury Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Can you tell what exactly to do. I have same problem but not able to resolve it.
     
    sancury, Jun 20, 2008 IP