phpmailer发送excell

作者:谢高升 发布:2017-10-27 浏览:1729次

使用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='<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;
	}

}