php - Generate a csv and send to email -
i want generate csv file , send email, tried :
function create_csv_string($data) { // open temp file pointer if (!$fp = fopen('php://temp', 'w+')) return false; fputcsv($fp, array('jour', 'nombre 1', 'nombre 2', 'total')); // loop data , write file pointer foreach ($data $line){ fputcsv($fp, $line); } // place stream pointer @ beginning rewind($fp); // return data return stream_get_contents($fp); } function send_csv_mail($csvdata, $body, $to = 'myemail@gmail.com', $subject = 'stats', $from = 'noreply@my.com') { // provide plenty adequate entropy $multipartsep = '-----'.md5(time()).'-----'; $headers = array( "from: $from", "reply-to: $from", "content-type: multipart/mixed; boundary=\"$multipartsep\"" ); // make attachment $attachment = chunk_split(base64_encode(create_csv_string($csvdata))); // make body of message $body = "--$multipartsep\r\n" . "content-type: text/plain; charset=iso-8859-1; format=flowed\r\n" . "content-transfer-encoding: 7bit\r\n" . "\r\n" . "$body\r\n" . "--$multipartsep\r\n" . "content-type: text/csv\r\n" . "content-transfer-encoding: base64\r\n" . "content-disposition: attachment; filename=\"stats-report-" . date("f-j-y") . ".csv\"\r\n" . "\r\n" . "$attachment\r\n" . "--$multipartsep--"; // send email, return result return @mail($to, $subject, $body, implode("\r\n", $headers)); }
my array $csvdata
:
( [2015-07-07] => 20 [2015-07-08] => 48 [2015-07-09] => 42 [2015-07-10] => 94 [2015-07-11] => 0 [2015-07-12] => 0 [2015-07-13] => 6 [2015-07-14] => 0 )
i email .csv
csv empty column array('jour', 'nombre 1', 'nombre 2', 'total')
wrote in 1 column. can me please ? in advance.
Comments
Post a Comment