Maison >développement back-end >tutoriel php >怎么把php导出的Excel 作为邮件发送
如何把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 />
<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 />