您好,登錄后才能下訂單哦!
使用phpmailer結合PHPexcell完成定時發送郵件和excell的功能; ini_set("magic_quotes_runtime",0); require 'class.phpmailer.php'; require 'dbo.php'; function getExcel($fileName, $headArr, $data) { //導入PHPExcel類庫,因為PHPExcel沒有用命名空間,只能導入 include 'PHPExcel.php'; include 'PHPExcel/Reader/Excel5.php'; include 'PHPExcel/Reader/Excel2007.php'; $date = date("Y_m_d", time()); //$fileName .= $date; //創建PHPExcel對象,注意,不能少了\ $objPHPExcel = new \PHPExcel(); $objProps = $objPHPExcel->getProperties(); //設置表頭 $key = ord("A"); //print_r($data);exit; foreach ($headArr as $v) { $colum = chr($key); $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v); $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v); $key += 1; } $column = 2; $objActSheet = $objPHPExcel->getActiveSheet(); //print_r($data);exit; foreach ($data as $key => $rows) { //行寫入 $span = ord("A"); foreach ($rows as $keyName => $value) {// 列寫入 $j = chr($span); $objActSheet->setCellValue($j . $column, $value); $span++; } $column++; } $fileName = iconv("utf-8", "gb2312", $fileName); //重命名表 //$objPHPExcel->getActiveSheet()->setTitle('test'); //設置活動單指數到第一個表,所以Excel打開這是第一個表 $objPHPExcel->setActiveSheetIndex(0); ob_end_clean();//清除緩沖區,避免亂碼 // header('Content-Type: application/vnd.ms-excel'); //header("Content - Disposition: attachment;filename = '$fileName'"); // header('Content-Disposition: attachment;filename="' . $fileName . '.xls"'); // header('Cache-Control: max-age=0'); $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save("/var/www/html/sendemail/upload/$fileName.xls"); //文件通過瀏覽器下載 //exit; } function sendemail($content){ $mail = new PHPMailer(true); $mail->IsSMTP(); $mail->CharSet='UTF-8'; //設置郵件的字符編碼 $mail->SMTPAuth = true; //開啟認證 $mail->Port = 25; $mail->Host = "smtp.ym.163.com"; $mail->Username = "xiegs@etonesystem.com"; // $mail->Password = "password"; $mail->AddReplyTo("xiegs@etonesystem.com","xiaoxie");//回復地址 $mail->From = "xiegs@etonesystem.com"; $mail->FromName = "小謝"; $to = "736214763@qq.com"; $mail->AddAddress($to); $mail->addCC('111@189.cn');//抄送人 $mail->addCC('222@163.com'); $mail->addCC('xiaoxie@qq.com'); $mail->Subject = "有設備離線"; $mail->Body = "$content"; $mail->AltBody = "設備離線請盡快查看!"; //當郵件不支持html時備用顯示,可以省略 $mail->WordWrap = 80; // 設置每行字符串的長度 $mail->AddAttachment("/var/www/html/sendemail/111.xls"); //可以添加附件 $mail->IsHTML(true); $mail->Send(); } //創建mysql實例查詢數據 $dbo = new MysqlConnector(); $d=date('w'); if($d > 0 && $d < 6){ $time=date('Y-m-d H:i:s'); $sql="select shopcode,shopname ,cnt ,uptime,cause,location,relation,temp from table"; $result=$dbo->returnResult($sql); $str="你好:"; if($result){ while($row=mysql_fetch_array($result,MYSQL_ASSOC)){ $arr[]=$row; } } // if($arr){ // $count=count($arr); for($i=0;$i<$count;$i++){ $content='<h4>'.$arr[$i]['shopname'].'</h4>'.'<br/>'. '地址:'.$arr[$i]['location'].'<br/>'. '聯系電話:'.$arr[$i]['relation'].'<br/>'. '離線ap數量: '.$arr[$i]['cnt'].'<br/>'.'離線時間:' .$arr[$i]['uptime'].'<br/>'.'店鋪編號:'.$arr[$i]['shopcode'].'<br/>' .'上次離線原因:'.$arr[$i]['cause'].'<br/>'.'具體描述:' .$arr[$i]['temp'].'<br/>'; $str.=$content; } foreach ($arr as $field => $v) {//第一行的頭 if ($field == 'shopcode') { $headArr[] = '店鋪編號'; } if ($field == 'shopname') { $headArr[] = '店鋪名稱'; } if ($field == 'cnt') { $headArr[] = '離線ap數量'; } if ($field == 'uptime') { $headArr[] = '離線時間'; } if ($field == 'cause') { $headArr[] = '上次離線原因'; }if ($field == 'location') { $headArr[] = '地址'; }if ($field == 'relation') { $headArr[] = '聯系電話'; } if ($field == 'temp') { $headArr[] = '具體描述'; } } $filename = "111"; getExcel($filename, $headArr, $arr); sendemail($str); //echo $str; }else{ exit; } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。