Home > Backend Development > PHP Tutorial > 怎么把php导出的Excel 作为邮件发送

怎么把php导出的Excel 作为邮件发送

WBOY
Release: 2016-06-13 12:00:53
Original
951 people have browsed it

如何把php导出的Excel 作为邮件发送
现在实现了点击后下载excel,和发送文本邮件的功能,怎么能结合下,把php导出的excel作为附件发送就完美了。
1 .生成excel:

<br />header("Content-type:application/octet-stream");<br />    header("Accept-Ranges:bytes");<br />    header("Content-type:application/vnd.ms-excel");  <br />    header("Content-Disposition:attachment;filename=".$filename.".xls");<br />    header("Pragma: no-cache");<br />    header("Expires: 0");<br /><br />    if (!empty($title)){<br />        foreach ($title as $k => $v) {<br />            $title[$k]=iconv("UTF-8", "GB2312",$v);<br />        }<br />        $title= implode("\t", $title);<br />        echo "$title\n";<br />    }<br />    if (!empty($data)){<br />        foreach($data as $key=>$val){<br />            foreach ($val as $ck => $cv) {<br />                $data[$key][$ck]=iconv("UTF-8", "GB2312", $cv);<br />            }<br />            $data[$key]=implode("\t", $data[$key]);<br />        }<br />        echo implode("\n",$data);<br />    }<br />
Copy after login


2 . 发送邮件:
用了phpmailer类库
<br />$mail    = new PHPMailer();   <br />    $mail->CharSet    = 'UTF-8';           <br />    $mail->IsSMTP();               <br />    $mail->SMTPAuth   = true;                 <br />    $mail->SMTPSecure = '';                   <br />    $mail->Host       = $config['SMTP_HOST'];  // SMTP 服务器  <br />    $mail->Port       = $config['SMTP_PORT'];  // SMTP服务器的端口号  <br />    $mail->Username   = $config['SMTP_USER'];  // SMTP服务器用户名  <br />    $mail->Password   = $config['SMTP_PASS'];  // SMTP服务器密码  <br />    $mail->SetFrom($config['FROM_EMAIL'], $config['FROM_NAME']);  <br />    $replyEmail       = $config['REPLY_EMAIL']?$config['REPLY_EMAIL']:$config['FROM_EMAIL'];  <br />    $replyName        = $config['REPLY_NAME']?$config['REPLY_NAME']:$config['FROM_NAME'];  <br />    $mail->AddReplyTo($replyEmail, $replyName);  <br />    $mail->Subject    = $subject;  <br />    $mail->MsgHTML($body);  <br />    $mail->AddAddress($to, $name);  <br />    if(is_file($attachment)){                   // 添加附件  <br />        $mail->AddAttachment($attachment);  <br />    }  <br />    return $mail->Send()<br />
Copy after login

------解决方案--------------------
第7行处加入 
ob_start();

第23行后加入
$s = ob_get_flush();
file_put_contents($filename.".xls", $s);
$attachment = $filename.".xls";
执行邮件发送
------解决方案--------------------
肯定是你哪里出错了,认真检查一下
你实际输出的是文本文件,用记事本就可打开

ob 函数的功能、用法,手册中都有
------解决方案--------------------
汗!那样导出的还没有路径,你如何作为附件发送呢??
你这不是天方夜谭么?
------解决方案--------------------
提个思路,你可参考下:
先把excel保存在服务器上,然后获得该excel的路径,然后作为附件进行email发送,如果你不需要这个文件了,然后再执行删除操作就OK了

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template