示例#1
0
 function parseMail($input_file)
 {
     global $argv;
     //DBG("parseMail start");
     if (!($mp = new MailParser($input_file))) {
         $this->setError('Error In MailParser');
         //DBG("parseMail error1: ".$mp->getErrorMessage());
         return false;
     } elseif ($mp->isError()) {
         $this->setError('Error In MailParser ' . $mp->getErrorMessage());
         //DBG("parseMail error2: ".$mp->getErrorMessage());
         // even if it is an error, try to get the address of the sender so we
         // can send him back the error
         $this->FromEmail = $mp->getFromEmail();
         return false;
     }
     $this->FromEmail = $mp->getFromEmail();
     //DBG("email: ".$this->FromEmail);
     //echo ")()()()()()()".$this->FromEmail."(*(*(*(*(*";
     //
     //subjects are in this required format: '[group - Forum][123456] My Subject'
     //where 123456 is the msg_id of the forum message.
     //we parse that ID to get the forum and thread that this should post to
     //
     $subj = $mp->getSubject();
     //DBG("mp headers: ".implode("**\n",$mp->headers));
     //DBG("mp body: ".$mp->body);
     //DBG("SUBJ: ".$subj);
     //DBG("BODY: ".$mp->getBody());
     /*
     		$parent_start = (strpos($subj,'[',(strpos($subj,'[')+1))+1);
     		$parent_end = (strpos($subj,']',$parent_start)-1);
     		$this->Parent = substr($subj,$parent_start,($parent_end-$parent_start+1));
     		if (!$this->Parent || !is_numeric($this->Parent)) {
     //			$argv[1] - listname
     //			echo "No Parent ".$argv[0]."||".$argv[1];
     			$this->Parent=0;
     			$this->Subject = addslashes($subj);
     //			$this->setError('No Valid Parent ID Found in Subject Line');
     //			return false;
     		} else {
     //			echo "Parent: ".$this->Parent."||".$argv[0]."||".$argv[1];
     			$this->Subject = addslashes(substr($subj,$parent_end+3));
     		}
     */
     if (ereg('(\\[)([0-9]*)(\\])', $subj, $arr)) {
         $this->Parent = $arr[2];
         $parent_end = strpos($subj, '[' . $arr[2] . ']') + strlen('[' . $arr[2] . ']');
         $this->Subject = addslashes(substr($subj, $parent_end));
     } else {
         $this->Subject = addslashes($subj);
         $this->Parent = 0;
     }
     $this->Body =& addslashes($mp->getBody());
     //DBG( "body1:". $this->Body);
     $begin = strpos($this->Body, FORUM_MAIL_MARKER);
     if ($begin === false) {
         //do nothing
         return true;
     }
     // get the part of the message located after the marker
     $this->Body = substr($this->Body, $begin + strlen(FORUM_MAIL_MARKER));
     //DBG( "body2:". $this->Body);
     // now look for the ending marker
     $end = strpos($this->Body, FORUM_MAIL_MARKER);
     if ($end === false) {
         return true;
     }
     $message = substr($this->Body, 0, $end);
     $message = trim($message);
     // maybe the last line was "> (FORUM_MAIL_MARKER)". In that case, delete the last ">"
     $message = preg_replace('/>$/', '', $message);
     $this->Message = $message;
     return true;
 }
示例#2
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);
             }
         }
     }
 }
示例#3
0
 function parseMail($input_file)
 {
     global $argv;
     if (!($mp = new MailParser($input_file))) {
         $this->setError('Error In MailParser');
         return false;
     } elseif ($mp->isError()) {
         $this->setError('Error In MailParser ' . $mp->getErrorMessage());
         // even if it is an error, try to get the address of the sender so we
         // can send him back the error
         $this->FromEmail = $mp->getFromEmail();
         return false;
     }
     $this->FromEmail = $mp->getFromEmail();
     //
     //subjects are in this required format: '[group - tracker_name][123456] My Subject'
     //where 123456 is the artifact_id of the artifact message.
     //we parse that ID to get the artifact that this should post to
     //
     $subj = $mp->getSubject();
     if (ereg('(\\[)([0-9]*)(\\])', $subj, $arr)) {
         $this->ArtifactId = $arr[2];
         $artifactid_end = strpos($subj, '[' . $arr[2] . ']') + strlen('[' . $arr[2] . ']');
         $this->Subject = addslashes(substr($subj, $artifactid_end));
     } else {
         $this->Subject = addslashes($subj);
         $this->ArtifactId = 0;
         // Not supported at the moment
         $this->setError("ArtifactId needed at the moment. Artifact creation not supported");
         return false;
     }
     $body = addslashes($mp->getBody());
     // find first occurrence of the marker in the message
     $begin = strpos($body, ARTIFACT_MAIL_MARKER);
     if ($begin === false) {
         $this->setError("Response message wasn't found in your mail. Please verify that " . "you entered your message between the correct text markers." . "\nYour message was:" . "\n" . $mp->getBody());
         return false;
     }
     // get the part of the message located after the marker
     $body = substr($body, $begin + strlen(ARTIFACT_MAIL_MARKER));
     // now look for the ending marker
     $end = strpos($body, ARTIFACT_MAIL_MARKER);
     if ($end === false) {
         $this->setError("Response message wasn't found in your mail. Please verify that " . "you entered your message between the correct text markers." . "\nYour message was:" . "\n" . $mp->getBody());
         return false;
     }
     $message = substr($body, 0, $end);
     $message = trim($message);
     // maybe the last line was "> (ARTIFACT_MAIL_MARKER)". In that case, delete the last ">"
     $message = preg_replace('/>$/', '', $message);
     $this->Message = $message;
     return true;
 }