一:相关配置
/** * 操作员明细报表 */ public function indexAction() { $service = $this->createService(); $stocks = WmsStock::query($this->fetchSql); $path = SKIHAT_PATH_APP_MODULES . \'/wms/docs/waring.xls\'; $subjects = "库存预警通知(".date("Ymd").")"; $body = "您好,以下商品库存不足(详情见附件),请及时补货!邮件由系统发出,请勿回复!"; if ($stocks) { $service->excel($stocks, $path); $service->sendMail($path, $subjects, $body); } die(); }
/** * 生成excel文件 * @param $stocks * @param $path * @throws PHPExcel_Exception * @throws PHPExcel_Reader_Exception */ public function excel($stocks, $path) { require_once(SKIHAT_PATH_VENDOR . \'/php-excel/PHPExcel.php\'); $objExcel = new PHPExcel(); $title = \'库存预警明细\'; //文件标题 $sheet = $objExcel->setActiveSheetIndex(0); $sheet->setCellValue(\'A2\', \'仓库名称\'); $sheet->setCellValue(\'B2\', \'仓库id\'); $sheet->setCellValue(\'C2\', " ".\'单品id\'); $sheet->setCellValue(\'D2\', \'单品名称\'); $sheet->setCellValue(\'E2\', \'品牌\'); $sheet->setCellValue(\'F2\', \'库存总量\'); $sheet->setCellValue(\'G2\', \'库存预警值\'); #合并单元格 $objExcel->setActiveSheetIndex(0)->mergeCells(\'A1:I1\'); $objExcel->setActiveSheetIndex(0)->setCellValue(\'A1\', $title); #设置font $objExcel->setActiveSheetIndex(0)->getStyle(\'A1\')->getFont()->setSize(16); #设置居中(上下左右) $objExcel->setActiveSheetIndex(0)->getStyle(\'A1\')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objExcel->setActiveSheetIndex(0)->getStyle(\'A1\')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); $rows = 3; foreach ($stocks as $item) { $sheet->setCellValue("A{$rows}", $item[\'house_name\']); $sheet->setCellValue("B{$rows}", $item[\'house_id\']); $sheet->setCellValue("C{$rows}", $item[\'sku_id\']); $sheet->setCellValue("D{$rows}", $item[\'sku_name\']); $sheet->setCellValue("E{$rows}", $item[\'brand_name\']); $sheet->setCellValue("F{$rows}", $item[\'num\']); $sheet->setCellValue("G{$rows}", $item[\'stock_warn\']); ++$rows; } $objExcel->getActiveSheet()->setTitle(\'SetExcelName\'); $objExcel->setActiveSheetIndex(0); $objWriter = PHPExcel_IOFactory::createWriter($objExcel, \'Excel5\'); $objWriter->save($path); }
/** * 发送邮件 * @param $path * @param $subjects * @param $body * @return bool * @throws FileNotFoundException * @throws phpmailerException */ public function sendMail($path, $subjects, $body) { //加载文件 Skihat::import(\'php-mailer.php-mailer-autoload\', SKIHAT_PATH_VENDOR); $emails = WmsWarnEmail::fetchAll(array(\'status\'=>1))->fetchObjects(); $mail = new PHPMailer(); //发送邮件服务器配置 $mail->isSMTP(); $mail->CharSet = "UTF-8"; $mail->Encoding = "base64"; $mail->Host = \'smtp.163.com\'; $mail->SMTPAuth = true; $mail->Username = \'xxxx@163.com\'; //发件邮箱 $mail->Password = self::PASSWORD; $mail->SMTPSecure = tls; $mail->Port =25; $mail->setFrom(xxxx@163.com, \'xxxx\'); //多人发送 foreach($emails as $key=>$val){ $mail->addAddress("$val->warn_email"); } $mail->isHTML(true); $mail->Subject = $subjects; //主题 $mail->Body = $body; //内容 $mail->AddAttachment($path, \'xxx.xls\'); //附件 if (!$mail->send()) { echo "Mailer Error: " . $mail->ErrorInfo; return false; } else { echo "发送成功!!"; return true; } }
二、报错
554 554 MI:SPB UserReject
0,smtp4,DtGowEBZfFjwIs5O2VSdBg--.25073S2 1322132210 please feedback at
554 MI:SPB 此用户不在网易允许的发信用户列表里
可能原因:
1、发送大量的广告信/推广信。(广告邮件)
2、发送了用户事先并未提出要求或者同意接收的邮件。(垃圾邮件)
3、发送的邮件包含大量不存在的收件人。
1、发送大量的广告信/推广信。(广告邮件)
2、发送了用户事先并未提出要求或者同意接收的邮件。(垃圾邮件)
3、发送的邮件包含大量不存在的收件人。
请登录页面http://feedback.mail.126.com/antispam/complain.php?user=hz_viigoo@163.com
申请移除对xxx@xx.com 的相关限制
网易相关错误码信息链接
二:如果报SMTP ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: Name or service not known (0)
SMTP错误:连接到服务器失败:PHPyNETWorksGETActudi:GETADDRIN失败:名称或服务不知道(0)
可以先试试网络是否能ping 通