使用phpmailer結(jié)合PHPexcell完成定時(shí)發(fā)送郵件和excell的功能;
ini_set("magic_quotes_runtime",0);
require 'class.phpmailer.php';
require 'dbo.php';
function getExcel($fileName, $headArr, $data)
{
//導(dǎo)入PHPExcel類庫,因?yàn)镻HPExcel沒有用命名空間,只能導(dǎo)入
include 'PHPExcel.php';
include 'PHPExcel/Reader/Excel5.php';
include 'PHPExcel/Reader/Excel2007.php';
$date = date("Y_m_d", time());
//$fileName .= $date;
//創(chuàng)建PHPExcel對象,注意,不能少了\
$objPHPExcel = new \PHPExcel();
$objProps = $objPHPExcel->getProperties();
//設(shè)置表頭
$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');
//設(shè)置活動單指數(shù)到第一個表,所以Excel打開這是第一個表
$objPHPExcel->setActiveSheetIndex(0);
ob_end_clean();//清除緩沖區(qū),避免亂碼
// 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'; //設(shè)置郵件的字符編碼
$mail->SMTPAuth = true; //開啟認(rèn)證
$mail->Port = 25;
$mail->Host = "smtp.ym.163.com";
$mail->Username = "xiegs@etonesystem.com"; //
$mail->Password = "password";
$mail->AddReplyTo("xiegs@etonesystem.com","xiaoxie");//回復(fù)地址
$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 = "有設(shè)備離線";
$mail->Body = "$content";
$mail->AltBody = "設(shè)備離線請盡快查看!"; //當(dāng)郵件不支持html時(shí)備用顯示,可以省略
$mail->WordWrap = 80; // 設(shè)置每行字符串的長度
$mail->AddAttachment("/var/www/html/sendemail/111.xls"); //可以添加附件
$mail->IsHTML(true);
$mail->Send();
}
//創(chuàng)建mysql實(shí)例查詢數(shù)據(jù)
$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='
'.$arr[$i]['shopname'].'
'.'
'.
'地址:'.$arr[$i]['location'].'
'.
'聯(lián)系電話:'.$arr[$i]['relation'].'
'.
'離線ap數(shù)量: '.$arr[$i]['cnt'].'
'.'離線時(shí)間:'
.$arr[$i]['uptime'].'
'.'店鋪編號:'.$arr[$i]['shopcode'].'
'
.'上次離線原因:'.$arr[$i]['cause'].'
'.'具體描述:'
.$arr[$i]['temp'].'
';
$str.=$content;
}
foreach ($arr as $field => $v) {//第一行的頭
if ($field == 'shopcode') {
$headArr[] = '店鋪編號';
}
if ($field == 'shopname') {
$headArr[] = '店鋪名稱';
}
if ($field == 'cnt') {
$headArr[] = '離線ap數(shù)量';
}
if ($field == 'uptime') {
$headArr[] = '離線時(shí)間';
}
if ($field == 'cause') {
$headArr[] = '上次離線原因';
}if ($field == 'location') {
$headArr[] = '地址';
}if ($field == 'relation') {
$headArr[] = '聯(lián)系電話';
}
if ($field == 'temp') {
$headArr[] = '具體描述';
}
}
$filename = "111";
getExcel($filename, $headArr, $arr);
sendemail($str);
//echo $str;
}else{
exit;
}
}
成都創(chuàng)新互聯(lián)公司總部坐落于成都市區(qū),致力網(wǎng)站建設(shè)服務(wù)有
網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作、網(wǎng)絡(luò)營銷策劃、網(wǎng)頁設(shè)計(jì)、網(wǎng)站維護(hù)、公眾號搭建、
小程序制作、軟件開發(fā)等為企業(yè)提供一整套的信息化建設(shè)解決方案。創(chuàng)造真正意義上的網(wǎng)站建設(shè),為互聯(lián)網(wǎng)品牌在互動行銷領(lǐng)域創(chuàng)造價(jià)值而不懈努力!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
標(biāo)題名稱:phpmailer發(fā)送excell-創(chuàng)新互聯(lián)
當(dāng)前路徑:
http://weahome.cn/article/djpegd.html