2012-10-15 3 views
1

받은 편지함에서 이메일을 읽고 티켓을 업데이트 한 다음 이메일을 처리 된 폴더로 옮기는 스크립트를 작성/복사했습니다. 이 모든 기능은받은 편지함에 새로운 이메일을 완벽하게 전송하지만 다른 사람이 이메일에 회신하고받은 편지함에서 끝나면 내 scrtipt는 이메일에서 아무 것도 읽지 않습니다.PHP Imap받은 편지함에서 답장 받기

전자 메일이 회신 될 때 전자 메일의 구조와 다른 점이 있습니까? 전자 메일에서 내용을 읽는 방법이 필요하므로 내용으로 티켓을 업데이트 할 수 있습니다. 업데이트 할 이메일을 아는 것만으로도 완전히 혼란스럽게 콘텐츠를 읽는 것이 중요합니다.

는 여기에 몇 가지 의미가 누군가가 실제로 도움이 될 수 있습니다 코드

class Email 
{ 

    // imap server connection 
    public $conn; 

    // inbox storage and inbox message count 
    public $inbox; 
    private $msg_cnt; 

    // email login credentials 

     private $server = '????????????'; 

    private $user = '????????'; 
    private $pass = '?????????????'; 
    private $port = ??; 

    // connect to the server and get the inbox emails 
    function __construct() 
     { 
     $this->connect(); 
     $this->inbox(); 

     } 

     function getdecodevalue($message,$coding) 
     { 
      switch($coding) { 
        case 0: 
        case 1: 
          $message = imap_8bit($message); 
          break; 
        case 2: 
          $message = imap_binary($message); 
          break; 
        case 3: 
        case 5: 
          $message=imap_base64($message); 
          break; 
        case 4: 
          $message = imap_qprint($message); 
          break; 
      } 
      return $message; 
     } 

    // close the server connection 
    function close() 
     { 
     $this->inbox = array(); 
     $this->msg_cnt = 0; 

     imap_close($this->conn); 
    } 

    // open the server connection 
    // the imap_open function parameters will need to be changed for the particular server 
    // these are laid out to connect to a Dreamhost IMAP server 
    function connect() 
     { 
     $this->conn = imap_open("{".$this->server.":".$this->port."/imap/novalidate-cert}INBOX", $this->user, $this->pass); 

    } 

    // move the message to a new folder 
    function move($msg_index, $folder='Read') 
     { 
     // move on server 
      imap_mail_move($this->conn, $msg_index, $folder); 


     // re-read the inbox 
     //$this->inbox(); 
    } 

    // get a specific message (1 = first email, 2 = second email, etc.) 
    function get($msg_index=NULL) 
     { 
     if(count($this->inbox) <= 0) 
    { 
      return array(); 
     } 
     elseif(! is_null($msg_index) && isset($this->inbox[$msg_index])) 
    { 
      return $this->inbox[$msg_index]; 
     } 

     return $this->inbox[0]; 
    } 

    // read the inbox 
    function inbox() 
     { 
     $this->msg_cnt = imap_num_msg($this->conn); 

     $in = array(); 
     for($i = 1; $i <= $this->msg_cnt; $i++) 
    { 
       $in[] = array(
         'index'  => $i, 
         'header' => imap_headerinfo($this->conn, $i), 
         'body'  => $this->cleanBody(imap_fetchbody($this->conn, $i,1)), 
         'structure' => imap_fetchstructure($this->conn, $i) 
        ); 
     } 

     $this->inbox = $in; 
    } 


     function cleanBody($body) 
     { 


      $delimiter = '#'; 
      $startTag = '----------START REPLY----------'; 
      $endTag = '----------END REPLY----------'; 
      $regex = $delimiter . preg_quote($startTag, $delimiter) 
           . '(.*?)' 
           . preg_quote($endTag, $delimiter) 
           . $delimiter 
           . 's'; 
      preg_match($regex,$body,$matches); 

      $ret = trim($matches[1]); 

      return $ret; 
    } 

} 




$emails = new Email(); 



$email = array(); 
$emailCount = 1; 
foreach($emails->inbox as $ems => $em) 
{ 

      $email[$emailCount]['subject'] = $sub = $em['header']->subject; 

      //echo $sub; 

      $subParts = explode('-',$sub); 

      $ticketUniqueCode = trim($subParts[1]); 

      $sql = "SELECT * FROM ticket_main WHERE uniquecode = '".mysql_escape_string($ticketUniqueCode)."' LIMIT 1"; 

      $query = mysql_query($sql); 

      if(mysql_num_rows($query)) 
      { 
       $res = mysql_fetch_object($query); 

       $ticketBody = $em['body']; 
       $customerID = $res->customerID; 


       $sql2 = "INSERT INTO ticket_updates SET ticketID = '".$res->ticketID."' , submitted = NOW() , submittedBy = '".$res->customerID."' , message = '".mysql_escape_string($ticketBody)."' , inhouse = 0"; 
       $query = mysql_query($sql2); 


       // attachment section 
       $message_number = $em['index']; 

       $attachments = array(); 
       if(isset($em['structure']->parts) && count($em['structure']->parts)) 
       { 
         //echo 'hi'; 
         for($i = 0; $i < count($em['structure']->parts); $i++) 
         { 
           $attachments[$i] = array(
             'is_attachment' => false, 
             'filename' => '', 
             'name' => '', 
             'attachment' => '' 
           ); 

           if($em['structure']->parts[$i]->ifdparameters) { 
               foreach($em['structure']->parts[$i]->dparameters as $object) 
             { 
               if(strtolower($object->attribute) == 'filename') 
               { 
                 $attachments[$i]['is_attachment'] = true; 
                 $attachments[$i]['filename'] = $object->value; 
               } 
             } 
           } 

           if($em['structure']->parts[$i]->ifparameters) { 
             foreach($em['structure']->parts[$i]->parameters as $object) 
             { 
               if(strtolower($object->attribute) == 'name') 
               { 
                 $attachments[$i]['is_attachment'] = true; 
                 $attachments[$i]['name'] = $object->value; 
               } 
             } 
           } 
           if($attachments[$i]['is_attachment']) 
           { 
             $attachments[$i]['attachment'] = imap_fetchbody($emails->conn, $message_number, $i+1); 
             if($em['structure']->parts[$i]->encoding == 3) 
             { // 3 = BASE64 
               $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']); 
             } 
             elseif($em['structure']->parts[$i]->encoding == 4) 
             { // 4 = QUOTED-PRINTABLE 
               $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']); 
             } 
           } 

           if(isset($em['structure']->parts[$i]->disposition) && $em['structure']->parts[$i]->disposition == "attachment") 
           { 
             $filename = $attachments[$i]['name']; 
             $mege=""; 
             $data=""; 
             $mege = imap_fetchbody($emails->conn, $message_number, $i+1); 
             $filename= $filename; 
             $fp=fopen('???????????????'.$filename,"w"); 
             $data=$emails->getdecodevalue($mege,$em['structure']->parts[$i]->type); 

             fputs($fp,$data); 
             fclose($fp); 
             $email[$emailCount]['attachment'] = $attachments; 



           } 


         } 


       } 

      } 

      $emailCount++; 

} 
$emailNumbers = imap_search($emails->conn,'ALL'); 

if(!empty($emailNumbers)) 
{ 
foreach($emailNumbers as $a) 
{ 
    $emails->move($a); 
} 
imap_expunge($emails->conn); 
} 
$emails->close(); 

희망을 것입니다.

'body'  => $this->cleanBody(imap_fetchbody($this->conn, $i,1)), 

이 단지에서 찾고 의미 : 사전에

많은 많은 감사는

답변

2

그럼 가장 확실한 것은 당신의 라인 코드는 메시지가 1 부에 항상 가정이다 본문 1 순수 텍스트 이메일 본문 인 경우 1이 적합하지만 multipart/alternative (텍스트 & html) 본문 1은 하위 본문 (본문 1.1, 1.2)이 실제로 있음을 알리는 메시지 MIME입니다. 내용을 유지한다.

답장에 포함 된 이미지가 포함되어 있거나 첨부 파일로 답장하는 메시지가 포함되어있는 경우 더 많은 신체/위치가있을 수 있습니다.

그래서 시체를 어떻게 찾습니까? (물어보십시오) ... 잘 imap_fetchstructure을 사용하여 모든 신체 부위에 대해 알아보고, 그 부분을 검색하여 유형 = 0 (텍스트) 인 부분을 찾은 다음 해당 신체 부위를 다운로드하십시오. 첫 번째로 발견 된 텍스트는 올바른 텍스트이어야하지만 이메일에 두 개 이상의 텍스트 본문 유형이있을 수 있습니다.

+0

감사합니다. imap_fetchstructure를보고 고맙게 생각하면서 몸을 정확하게 해독했다는 것을 깨달았습니다. 실제로는 전혀 해독하지 못했습니다. 그것을 할 수있는 기능이 있었지만 그것을 사용하여 waslol. 다시 한 번 스타가되어 주셔서 감사합니다. 추신 : 정말로 빠른 답장을 보내 주셔서 감사합니다. – user1187274