在电子邮件附件中获取空白csv文件 [英] Getting blank csv file in email attachment
本文介绍了在电子邮件附件中获取空白csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用php我试图通过以下附件中的csv格式发送电子邮件数据库表数据是我尝试过的代码,但我得到了空白csv附件中的文件。
我尝试了什么:
Hi,
Using php i am trying to email database table data in csv format in attachment below is the code which i have tried but i am getting blank csv file in attachment.
What I have tried:
<?php
$hostname = "localhost";
$username = "root";
$password = "";
$database = "owner";
$conn = mysql_connect($hostname, $username, $password) or die(mysql_error());
$db_selected = mysql_select_db($database, $conn);
$sql = mysql_query("SELECT * FROM bucket");
$row=mysql_fetch_assoc($sql);
$filename='ptp/'.$filename.'.csv';
$fp=fopen($filename,"w");
$seperator="";
$comma="";
foreach($row as $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 as $name =>$value)
{
$seperator.=$comma.''.str_replace('','""',$value);
$comma=",";
}
$seperator.="\n";
fputs($fp,$seperator);
}
fclose($fp);
$my_file = $filename.'.csv';
$path = "temp/";
$from_name = "PTP database";
$from_mail = "r.shishodia@99acres.com";
$mailto = "r.shishodia@99acres.com";
$subject = "This is a mail with attachment.";
$message = "Hi,\r\n do you 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 is a 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"; // use 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."--";
if (mail($mailto, $subject, $message, $header)){
echo "Mail send";
} else{
echo "can not send mail";
}
?>
推荐答案
主机名 = localhost;
hostname = "localhost";
用户名 = 根跨度>;
username = "root";
密码 = 跨度>;
password = "";
这篇关于在电子邮件附件中获取空白csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文