dongren1977 2012-02-06 11:40
浏览 65
已采纳

使用PHPExcel作为附件创建的PHP Mail xlsx文件

So I'm trying to generate and save an Excel-file to the server. This works flawlessly with PHPExcel. The next step is to read the file, e-mail it as an attachment and then delete it.

For some reason, PHP does not recognize xlsx as a proper file:

/* excel is generated before here */
$filename = "/results/excel/export-" . $today . ".xlsx";
$writer = new PHPExcel_Writer_Excel2007($exc);
$writer->save($filename);
/* so far so good; the file is created and exists on the server */

$to = "someone@domain.com";
$sendermail = "no-reply@domain.com";
$from = "Sender <" . $sendermail . ">";
$subject = "Email with attachment";
$message = "Here you go";
$file = $filename;

$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

$headers = "From: $from";
$headers .= "
MIME-Version: 1.0
" . "Content-Type: multipart/mixed;
" . " boundary=\"        {$mime_boundary}\""; 

$message = "--{$mime_boundary}
" . "Content-Type: text/plain; charset=\"iso-8859-1\"
" .
"Content-Transfer-Encoding: 7bit

" . $message . "

";

if(is_file($file)) {
    $message .= "--{$mime_boundary}
";
    $fp = @fopen($file, "rb");
    $data = @fread($fp, filesize($file));
    @fclose($fp);
    $data = chunk_split(base64_encode($data));
    $message .= "Content-Type: application/octet-stream; name=\"" . basename($file). ";
" . "Content-Transfer-Encoding: base64

" . $data . "

";

    $message .= "--{$mime_boundary}--";
    $returnpath = "-f" . $sendermail;

    mail($to, $subject, $message, $headers, $returnpath);
} else {
    echo("This is not a file: " . $file);
}

Which always gives me "This is not a file...". How do I read the XLSX-file?

  • 写回答

3条回答 默认 最新

  • duanchanguo7603 2012-04-27 08:03
    关注

    Okay, I found out what it was. $filename was actually a direct (http://...) link to the file, not a relative link. Changed it to be relative and it worked :).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化