现在实现了点击后下载excel,和发送文本邮件的功能,怎么能结合下,把php导出的excel作为附件发送就完美了。
1 .生成excel:
header(content-type:application/octet-stream);
header(accept-ranges:bytes);
header(content-type:application/vnd.ms-excel);
header(content-disposition:attachment;filename=.$filename..xls);
header(pragma: no-cache);
header(expires: 0);
if (!empty($title)){
foreach ($title as $k => $v) {
$title[$k]=iconv(utf-8, gb2312,$v);
}
$title= implode(\t, $title);
echo $title\n;
}
if (!empty($data)){
foreach($data as $key=>$val){
foreach ($val as $ck => $cv) {
$data[$key][$ck]=iconv(utf-8, gb2312, $cv);
}
$data[$key]=implode(\t, $data[$key]);
}
echo implode(\n,$data);
}
2 . 发送邮件:
用了phpmailer类库
$mail = new phpmailer();
$mail->charset = 'utf-8';
$mail->issmtp();
$mail->smtpauth = true;
$mail->smtpsecure = '';
$mail->host = $config['smtp_host']; // smtp 服务器
$mail->port = $config['smtp_port']; // smtp服务器的端口号
$mail->username = $config['smtp_user']; // smtp服务器用户名
$mail->password = $config['smtp_pass']; // smtp服务器密码
$mail->setfrom($config['from_email'], $config['from_name']);
$replyemail = $config['reply_email']?$config['reply_email']:$config['from_email'];
$replyname = $config['reply_name']?$config['reply_name']:$config['from_name'];
$mail->addreplyto($replyemail, $replyname);
$mail->subject = $subject;
$mail->msghtml($body);
$mail->addaddress($to, $name);
if(is_file($attachment)){ // 添加附件
$mail->addattachment($attachment);
}
return $mail->send()
------解决方案--------------------
第7行处加入
ob_start();
第23行后加入
$s = ob_get_flush();
file_put_contents($filename..xls, $s);
$attachment = $filename..xls;
执行邮件发送
------解决方案--------------------
肯定是你哪里出错了,认真检查一下
你实际输出的是文本文件,用记事本就可打开
ob 函数的功能、用法,手册中都有
------解决方案--------------------
汗!那样导出的还没有路径,你如何作为附件发送呢
你这不是天方夜谭么?
------解决方案--------------------
提个思路,你可参考下:
先把excel保存在服务器上,然后获得该excel的路径,然后作为附件进行email发送,如果你不需要这个文件了,然后再执行删除操作就ok了
http://www.bkjia.com/phpjc/820422.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/820422.htmltecharticle如何把php导出的excel 作为邮件发送 现在实现了点击后下载excel,和发送文本邮件的功能,怎么能结合下,把php导出的excel作为附件发送就完...