Monday 15 June 2015

Creating excel from mysql and sending this as attachment email in php -



Creating excel from mysql and sending this as attachment email in php -

i need send email attachment, , attachment file contain info fetched mysql database @ same time.

that problem asked , described here there no working answer. can have solution, please answer.

while( $row = mysql_fetch_row( $sqlquery ) ) { $line = ''; foreach( $row $value ) { if ( ( !isset( $value ) ) || ( $value == "" ) ) { $value = "\t"; } else { $value = str_replace( '"' , '""' , $value ); $value = '"' . $value . '"' . "\t"; } $line .= $value; } $data .= trim( $line ) . "\n"; } $data = str_replace( "\r" , "" , $data ); $cho = "$header\n$data"; echo $cho; $headers = "from:abcdf.k@gmail.com(pfc web admin) \r\n"; $headers .= "mime-version: 1.0\r\n"; $headers .= "content-type: application/vnd.ms-excel"; $headers .= 'content-disposition: attachment; filename=test.xls' . "\r\n"; $headers .= "content-transfer-encoding: base64\r\n"; $headers .= "".$cho."\r\n"; $subject = 'record november'; $mail = mail( 'abc.k@gmail.com', $subject, $msg, $headers ); if( $mail == true ) { echo "message sent"; } else { echo "message not sent"; }

excel creating , have proper info need mail service rather download.

try this,it's working

<?php include_once('inc/dbconnect.inc.php'); error_reporting(e_error); $sql = mysql_query("select * tablename"); $row=mysql_fetch_assoc($sql); $filename='temp/'.$filename.'.csv'; $fp=fopen($filename,"w"); $seperator=""; $comma=""; foreach($row $name =>$value) { $seperator.=$comma.''.str_replace('','""',$name); $comma=","; } $seperator.="\n"; $seperator; fputs($fp,$seperator); mysql_data_seek($sql,0); while($row=mysql_fetch_assoc($sql)) { $seperator=""; $comma=""; foreach($row $name =>$value) { $seperator.=$comma.''.str_replace('','""',$value); $comma=","; } $seperator.="\n"; fputs($fp,$seperator); } fclose($fp); $my_file = $filename.'.csv'; $path = "temp/"; $from_name = "hhhhh"; $from_mail = "abc@gmail.com"; $mailto = "abc@gmail.com"; $subject = "this mail service attachment."; $message = "hi,\r\n got attachment?\r\n\r\hara"; $replyto="haraprasad@lemonpeak.com"; $file = $path.$my_file; $file_size = filesize($file); $handle = fopen($file, "r"); $content = fread($handle, $file_size); fclose($handle); $content = chunk_split(base64_encode($content)); $uid = md5(uniqid(time())); $name = basename($file); $header = "from: ".$from_name." <".$from_mail.">\r\n"; $header .= "reply-to: ".$replyto."\r\n"; $header .= "mime-version: 1.0\r\n"; $header .= "content-type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"; $header .= "this multi-part message in mime format.\r\n"; $header .= "--".$uid."\r\n"; $header .= "content-type:text/plain; charset=iso-8859-1\r\n"; $header .= "content-transfer-encoding: 7bit\r\n\r\n"; $header .= $message."\r\n\r\n"; $header .= "--".$uid."\r\n"; $header .= "content-type: application/octet-stream; name=\"".$filename."\"\r\n"; // utilize different content types here $header .= "content-transfer-encoding: base64\r\n"; $header .= "content-disposition: attachment; filename=\"".$filename."\"\r\n\r\n"; $header .= $content."\r\n\r\n"; $header .= "--".$uid."--"; mail($mailto, $subject, "", $header) ?>

php mysql email sendmail

No comments:

Post a Comment