Jump to content

php mail- csv file attachment


Trisha

Recommended Posts

Hi,

 

I have the program which get's the results from the database and allows download of the info in .csv format.

Now I want to be able to mail this file as an attachment to an email address using gmail server.  I've come up with a mail program which can mail the info from database in text format... need help in modifying it to send csv attachment.  Any help is greatly appreciated.

Thanks in advance!!

 

[attachment deleted by admin]

Link to comment
Share on other sites

that's weird.. I thought I attached the code :P

anyway, I'll paste it here again... Thanks for the response!! :)

 

<?php

 

  // Connect to the database server

  $dbcnx = @mysql_connect("localhost",

          "root", "");

  if (!$dbcnx) {

    echo( "<P>Unable to connect to the " .

          "database server at this time.</P>" );

    exit();

  }

 

  // Select database

  if (! @mysql_select_db("adcol") ) {

    echo( "<P>Unable to locate " .

          "database at this time.</P>" );

    exit();

  }

 

// Request data

  $result = mysql_query(

            "SELECT usertype,COUNT(name) as data FROM jos_users GROUP BY usertype");

  if (!$result) {

    echo("<P>Error performing query: " .

        mysql_error() . "</P>");

    exit();

  }

$num_fields = mysql_num_fields($result);

$headers = array();

 

$fp = fopen('php://output', 'w');

if ($fp && $result) {

    header('Content-Type: text/csv');

    header('Content-Disposition: attachment; filename="export.csv"');

    header('Pragma: no-cache');

    header('Expires: 0');

    fputcsv($fp, $headers);

    while ($row = mysql_fetch_assoc($result)) {

        fputcsv($fp, array_values($row));

    }

    die;

}

 

?>

 

Link to comment
Share on other sites

The above code downloads the csv file.... as "export.csv".... I need help with the mail function that attaches the above downloaded file and sends to a particular email address... I've tried plenty but can't even seem to get close :(

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.