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
https://forums.phpfreaks.com/topic/231036-php-mail-csv-file-attachment/
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;

}

 

?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.