title: get count of unread messages using imap php function
body: <p>Actually I am using imap functions but the problem is the count I am getting is of all the emails I want count that give me unread mail count can anyone tell me how can I do it. I can delete the seen mail and then it is working fine but I want to keep the mail and read the unread one.</p>
  
  <p>I am taking help from <a href="http://www.phpclasses.org/package/3324-PHP-Retrieve-e-mail-messages-into-a-MySQL-database.html" rel="nofollow">this class</a></p>
  
  <pre><code>    for ($i = 0; $i &lt; $total_messages; $i++) {
  
      $email = $vars-&gt;email_get($i+1);
      $ismsgdb = $vars-&gt;db_add_message($email); // vars INSERTS EACH MSG INTO DATABASE
      #Get store dir
      $dir = $vars-&gt;dir_name();
      $id_log = $vars-&gt;add_db_log($email, 'Copy e-mail - start ');
  
      foreach($vars-&gt;partsarray as $part){
  
      if($part[text][type] == 'HTML'){
          #$message_HTML = $part[text][string];
          $vars-&gt;db_update_message($part[text][string], $type= 'HTML');
      }
      elseif($part[text][type] == 'PLAIN'){
          $message_PLAIN = $part[text][string];
          $vars-&gt;db_update_message($part[text][string], $type= 'PLAIN');
      }
      elseif($part[attachment]){
      #Save files(attachments) on local disc
  
      foreach(array($part[attachment]) as $attach){
          $attach[filename] = $vars-&gt;mimie_text_decode($attach[filename]);
          $attach[filename] = preg_replace('/[^a-z0-9_\-\.]/i', '_', $attach[filename]);
          $vars-&gt;add_db_log($email, 'Start coping file:"'.strip_tags($attach[filename]).'"');
  
          $vars-&gt;save_files($vars-&gt;newid.$attach[filename], $attach[string]);
          $filename =  $dir.$vars-&gt;newid.$attach[filename];
          $vars-&gt;db_add_attach($attach[filename], $filename);
          $vars-&gt;update_db_log('&lt;b&gt;'.$filename.'&lt;/b&gt;Finish coping:  "'.strip_tags($attach[filename]).'"', $vars-&gt;logid);
      }
  
      }elseif($part[image]){
      #Save files(attachments) on local disc
  
      $message_IMAGE[] = $part[image];
  
      foreach($message_IMAGE as $image){
          $image[filename] = $vars-&gt;mimie_text_decode($image[filename]);
          $image[filename] = preg_replace('/[^a-z0-9_\-\.]/i', '_', $image[filename]);
          $vars-&gt;add_db_log($email, 'Start coping file: "'.strip_tags($image[filename]).'"');
  
  
          $vars-&gt;save_files($vars-&gt;newid.$image[filename], $image[string]);
          $filename =  $dir.$vars-&gt;newid.$image[filename];
          $vars-&gt;db_add_attach($image[filename], $filename);
          $vars-&gt;update_db_log('&lt;b&gt;'.$filename.'&lt;/b&gt;Finish  
          coping:"'.strip_tags($image[filename]).'"', $vars-&gt;logid);
          }
  
         }
  
       }
         $vars-&gt;spam_detect();
         $vars-&gt;email_setflag(); 
        //$vars-&gt;email_delete(); // WHEN I REMOVE vars THE SCRIPT WONT GRAB NEW EMAILS
        //$vars-&gt;email_expunge(); // WHEN I REMOVE vars THE SCRIPT WONT GRAB NEW EMAILS
  
        $vars-&gt;update_db_log('Finish coping', $id_log);
  }
  </code></pre>