我的代码创建xls文件&将它附在邮件中如下:
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=test.xls");
header("Pragma: no-cache");
header("Expires: 0");
$sep = "\t";
echo "Name \t Email \t Phone \t \n ";
while($row = mysqli_fetch_array($result,MYSQLI_BOTH)){
$schema_insert = "";
$schema_insert .= "$row[0]".$sep;
$schema_insert .= "$row[1]".$sep;
$schema_insert .= "$row[2]".$sep;
$schema_insert = str_replace($sep."$", "", $schema_insert);
$schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
$schema_insert .= "\t";
print(trim(str_replace(',', " ", $schema_insert)));
print "\n";
}
file_put_contents('test.xls', $schema_insert);
$to = "ex@example.com";
$from = "ex@example.com";
$subject = "test mail";
$separator = md5(date('r', time()));
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "test.xls";
//$pdfdoc is PDF generated by FPDF
$attachment = chunk_split(base64_encode(file_get_contents('test.xls')));
// main header
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$header .= "Content-type:text/plain; charset=iso-8859-1".$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;
// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment; filename=\"".$filename."\"".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
if (mail($to, $subject, $body, $headers)) {
echo "mail send ... OK";
} else {
echo "mail send ... ERROR";
}
它创建.xls文件和邮件附件工作完美。当我点击提交按钮时,这一切都会发生。
我的问题是当我点击提交按钮这个.xls文件下载。这不应该发生。 &安培; .xls文件echo "Name \t Email \t Phone \t \n ";
中的列名仅显示在.xls文件中,该文件是在提交按钮单击时下载的。但是发送到邮件的.xls文件只包含ROW数据,没有列名。
任何人都可以建议我的代码中有什么错误吗?最后,这个.xls文件应该可以从邮件中下载。但我只能在新标签中查看它。没有预览,无法下载。
任何帮助,将不胜感激。感谢。