PHP IMAP。停止标记为已显示的消息

时间:2018-05-04 18:51:39

标签: php gmail imap

我正在通过IMAP检索Gmail电子邮件。我正在设置FT_PEEK选项,最​​后,我甚至以只读方式打开邮箱(OP_READONLY)。然而,我的代码将消息标记为已读。

以下是代码:

class imap{
   CONST HOSTNAME='{imap.gmail.com:993/imap/ssl}INBOX';
   CONST USERNAME = '[Address hidden]';
   CONST PASSWORD = '[Password hidden]'; //App password from Google

   function getMessagesSince($date){
      //This will return a collection of email objects.

      $messages=array();
      if(!$imap=imap_open($this::HOSTNAME, $this::USERNAME, $this::PASSWORD, OP_READONLY)) throw new exception("Unable to connect to IMAP mailbox. ".imap_last_error());

      $since=date_format($date, 'j F Y');

      $emails=imap_search($imap, 'SINCE "'.$since.'"', SE_UID|FT_PEEK);

      foreach($emails as $email){
         $messages[]=$this->getMessage($email, $imap);
      }

      imap_close($imap);

      return $messages;
   }

   private function getMessage($uid, $imap){
      //First get the headers
      $headers=$this->getHeaders($uid, $imap);
      $datereceived=date('Y-m-d H:i:s', strtotime($headers->date));
      $sender=$headers->from[0]->mailbox."@".$headers->from[0]->host;
      $cc=$headers->cc;
      $subject=$headers->subject;

      //Now get the message body
      $message=$this->getBody($uid, $imap);

      $email=new email();
      $email->uid=$uid;
      $email->datereceived=$datereceived;
      $email->sender=$sender;
      $email->cc=$cc;
      $email->subject=$subject;
      $email->message=$message;
      return $email;
   }

   private function getHeaders($uid, $imap){
      //Return an array of headers for the referenced message
      //$overview = imap_fetch_overview($imap, $uid, FT_UID); //As we used the SE_UID flag when searching, we have to use it when fetching.
      //We use this, rather than fetch_overview, because the overview doesn't have the cc information.
      $hText = imap_fetchbody($imap, $uid, '0', FT_UID|FT_PEEK);
      $headers = imap_rfc822_parse_headers($hText);
      return $headers;
   }
   private function getBody($uid, $imap){
      $body = $this->get_part($imap, $uid, "TEXT/HTML");
      // if HTML body is empty, try getting text body
      if ($body == "") {
         $body = $this->get_part($imap, $uid, "TEXT/PLAIN");
      }
      return $body;
   }

   private function get_part($imap, $uid, $mimetype, $structure = false, $partNumber = false){
      if (!$structure) {
         $structure = imap_fetchstructure($imap, $uid, FT_UID);
      }
      if ($structure) {
         if ($mimetype == $this->get_mime_type($structure)) {
               if (!$partNumber) {
                  $partNumber = 1;
               }
               $text = imap_fetchbody($imap, $uid, $partNumber, FT_UID|FT_PEEK);
               switch ($structure->encoding) {
                  case 3:
                     return imap_base64($text);
                  case 4:
                     return imap_qprint($text);
                  default:
                     return $text;
               }
         }

         // multipart
         if ($structure->type == 1) {
               foreach ($structure->parts as $index => $subStruct) {
                  $prefix = "";
                  if ($partNumber) {
                     $prefix = $partNumber . ".";
                  }
                  $data = $this->get_part($imap, $uid, $mimetype, $subStruct, $prefix . ($index + 1));
                  if ($data) {
                     return $data;
                  }
               }
         }
      }
      return false;
   }

   private function get_mime_type($structure){
      $primaryMimetype = ["TEXT", "MULTIPART", "MESSAGE", "APPLICATION", "AUDIO", "IMAGE", "VIDEO", "OTHER"];

      if ($structure->subtype) {
         return $primaryMimetype[(int)$structure->type] . "/" . $structure->subtype;
      }
      return "TEXT/PLAIN";
   }
}

有人能发现我遗失的东西吗?

(这只是一些额外的文字,因为它说帖子主要是代码。我只是在这里输入,直到它让我发帖。)

1 个答案:

答案 0 :(得分:-1)

您应该从imap_open()中删除OP_READONLY标志。