phpmailer發送excell

使用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   = "[email protected]";  //  
	$mail->Password   = "password";   
	$mail->AddReplyTo("[email protected]","xiaoxie");//回覆地址
	$mail->From       = "[email protected]";
	$mail->FromName   = "小謝";
	$to = "[email protected]";
	$mail->AddAddress($to);
	$mail->addCC('[email protected]');//抄送人
	$mail->addCC('[email protected]');
	$mail->addCC('[email protected]');
	$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='<h3>'.$arr[$i]['shopname'].'</h3>'.'<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;
	}

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章