怎么把php导出的Excel 作为邮件发送
怎么把php导出的Excel 作为邮件发送
如何把php导出的Excel 作为邮件发送现在实现了点击后下载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 "$titlen";
}
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了