Francy Posted February 8, 2023 Share Posted February 8, 2023 xxx // Get dati from db public function create_csv_string() { $data = "Select t1.FIELD1, t1.FIELD2 FROM LIB/MYFILE"; $result_a = db2_exec($this->connessione, $data); $c1 =0; // Open temp file pointer if (!$fp = fopen('php://temp', 'w+')) return FALSE; fputcsv($fp, array('Field1', 'Field2')); // Loop data and write to file pointer while ($info= db2_fetch_both($result_a)) { $line['Field1'] = $info['FIELD1']; $line['Field2'] = $info['FIELD2']; fputcsv($fp, $line); } // Place stream pointer at beginning rewind($fp); // Return the data return stream_get_contents($fp); } // Prepare and send email public function send_csv_mail($body, $to = "yourname@gmail.com", $subject = "Report", $from = "myname@gmail.com") { // This will provide plenty adequate entropy $multipartSep = '-----'.md5(time()).'-----'; // Arrays are much more readable $headers = array( "From: ".$from, "Reply-To: ".$from, "Content-Type: multipart/mixed; boundary=".$multipartSep ); // Make the attachment $attachment = chunk_split(base64_encode(self::create_csv_string())); // Make the body of the 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=TEST\r\n" . "\r\n" . $attachment."\r\n" . "--".$multipartSep."--"; // Send the email, return the result return @mail($to, $subject, $body, implode("\r\n", $headers)); } Good morning, I post you a simple script for sending an email with file attachment derived from reading a db. In the script I am reporting I can send email with csv file attachment. Everything works fine but only I have the attachment in the csv format. I receive email with csv attachment. I would like to have the format, of the file attached in the email, in xls and not in csv. Who can help me figure out how to do this? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/315892-generate-xls-file-and-send-as-email-attachment-php/ Share on other sites More sharing options...
ginerjm Posted February 8, 2023 Share Posted February 8, 2023 Have the sender give you an xls file? Quote Link to comment https://forums.phpfreaks.com/topic/315892-generate-xls-file-and-send-as-email-attachment-php/#findComment-1605487 Share on other sites More sharing options...
Francy Posted February 8, 2023 Author Share Posted February 8, 2023 Sorry, I have as a result in the email a csv file, I would like instead an xls file Quote Link to comment https://forums.phpfreaks.com/topic/315892-generate-xls-file-and-send-as-email-attachment-php/#findComment-1605488 Share on other sites More sharing options...
kicken Posted February 8, 2023 Share Posted February 8, 2023 You can use PHPSpreadSheet to generate the file. Unless you specifically need spreadsheet features I would just stick to CSV files though. It is much simpler and works just as well for sending data. Quote Link to comment https://forums.phpfreaks.com/topic/315892-generate-xls-file-and-send-as-email-attachment-php/#findComment-1605490 Share on other sites More sharing options...
ginerjm Posted February 8, 2023 Share Posted February 8, 2023 So what you are Really asking is: How do I convert a csv file to an Excel spreadsheet. Quote Link to comment https://forums.phpfreaks.com/topic/315892-generate-xls-file-and-send-as-email-attachment-php/#findComment-1605491 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.