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 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器