Exemplo n.º 1
0
 private function _parseMail()
 {
     //循环解析没封邮件
     $aFiles = self::_getMailFiles();
     if (!empty($aFiles)) {
         $strWorkingPath = MAIL_DIR_PATH . MAIL_INCOMING_DIR;
         foreach ($aFiles as $strFile) {
             $strFilePath = $strWorkingPath . '/' . $strFile;
             $objParser = new MailParser($strFilePath);
             $aDecode = $objParser->decodeMail();
             if (self::_filterMails($aDecode)) {
                 // subject 取值要放在Mask计算前面,Mask可能会更改subject
                 $strSubject = $objParser->getSubject();
                 $strOrigSubject = $strSubject;
                 $bHaveMask = true;
                 $strListMask = self::_buildMailListId($strSubject, $bHaveMask);
                 $bHaveAttachment = NOT_HAVE_ATTATCHMENT;
                 // save mail info
                 $objMaildata = new mail_data();
                 $strDate = $objParser->getHeader('date');
                 if (!empty($strDate)) {
                     $nDate = strtotime($objParser->getHeader('date'));
                     $objMaildata->create_time = date('Y-m-d H:i:s', $nDate);
                 } else {
                     $objMaildata->create_time = date('Y-m-d H:i:s');
                 }
                 $objMaildata->list_mask = $strListMask;
                 $objMaildata->mail_header = $objParser->getHeader();
                 $objMaildata->mail_from = $objParser->getHeader('from');
                 $objMaildata->mail_to = $objParser->getHeader('to');
                 $objMaildata->mail_cc = $objParser->getHeader('cc');
                 $objMaildata->title = $strSubject;
                 $objMaildata->content_text = $objParser->getMessageBody('text');
                 $objMaildata->content_html = $objParser->getMessageBody('html');
                 $objMaildata->file_name = $strFile;
                 if ($bHaveMask) {
                     $objMaildata->type = REPLAY_MAIL;
                 } else {
                     $objMaildata->type = MAIN_MAIL;
                 }
                 $aAttachments = $objParser->getAttachments();
                 if (!empty($aAttachments)) {
                     $bHaveAttachment = HAVE_ATTATCHMENT;
                 }
                 $objMaildata->attachment = $bHaveAttachment;
                 if (!$objMaildata->save(true)) {
                     $errors = $objMaildata->getErrors();
                     $msg = array();
                     foreach ($errors as $id => $error) {
                         $msg[] = " {$id}: " . implode(', ', $error);
                     }
                     $msg .= implode('<br />', $msg);
                     throw new Exception($msg);
                 } else {
                     self::_moveMail($strFile);
                     /** 
                      * create a new report_content
                      * 如果是REPLAY_MAIL 则无需创建 report_content 
                      * 只有MAIN_MAIL才创建report_content
                      */
                     if ($objMaildata->type == MAIN_MAIL) {
                         $objContent = new report_content();
                         $objContent->list_mask = $strListMask;
                         $objContent->title = $strOrigSubject;
                         $objContent->creator = self::_getMailAddr($objMaildata->mail_from);
                         $objContent->creator_mail = $objMaildata->mail_from;
                         $objContent->create_time = $objMaildata->create_time;
                         $objContent->last_update_time = date('Y-m-d H:i:s');
                         $objContent->status = UNDEAL;
                         // 未处理
                         $objContent->emergency = NORMAL_LEVEL;
                         // 一般
                         if (!$objContent->save(true)) {
                             $errors = $objContent->getErrors();
                             $msg = array();
                             foreach ($errors as $id => $error) {
                                 $msg[] = "{$id}:" . implode(', ', $error);
                             }
                             $msg .= implode('<br/>', $msg);
                             throw new Exception($msg);
                         }
                         // 写日志
                         self::_addLog($strListMask, $objContent->create_time, $objContent->creator, '创建CASE成功', NULL, NULL);
                     } else {
                         // 写日志
                         self::_addLog($strListMask, $objContent->create_time, $objContent->creator, '追加CASE成功', NULL, NULL);
                     }
                 }
                 // save mail attachment
                 if ($bHaveAttachment) {
                     foreach ($aAttachments as $aFile) {
                         $objAttatchment = new mail_attachment();
                         $objAttatchment->mail_id = $objMaildata->id;
                         $objAttatchment->path = $aFile['attachment_path'];
                         $objAttatchment->file_name = $aFile['file_name'];
                         $objAttatchment->file_type = $aFile['file_type'];
                         $objAttatchment->file_description = $aFile['file_description'];
                         $objAttatchment->file_id = $aFile['file_id'];
                         if (!$objAttatchment->save(true)) {
                             $errors = $objAttatchment->getErrors();
                             $msg = array();
                             foreach ($errors as $id => $error) {
                                 $msg[] = " {$id}: " . implode(', ', $error);
                             }
                             $msg .= implode('<br />', $msg);
                             throw new Exception($msg);
                         } else {
                             //save attachment file
                             $strOrigFile = "{$objAttatchment->path}/{$objAttatchment->mail_id}_{$objAttatchment->file_name}";
                             $nPos = stripos(strtolower($objAttatchment->file_description), 'utf-8');
                             if ($nPos === false) {
                                 $strFile = @mb_convert_encoding($strOrigFile, 'utf-8', 'gbk');
                             } else {
                                 $strFile = $strOrigFile;
                             }
                             $handle = fopen($strFile, 'wb');
                             fwrite($handle, $aFile['file_body']);
                             fclose($handle);
                         }
                     }
                 }
             } else {
                 self::_moveMail($strFile);
             }
         }
     }
 }