Jump to content

Recommended Posts

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 

 

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.