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 = "
[email protected]", $subject = "Report", $from = "
[email protected]")
{
// 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