title: Using IMAP () in PHP to get recent unread emails count
body: <p>I am trying to get no. of recent unread mails from a gmail account.For this I have installed IMAP in my Ubuntu system and tried some PHP iMAP functions.
  Here are what i have tried till now.</p>
  
  <pre><code>/* connect to gmail */
  $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
  $username = 'user@gmail.com';
  $password = 'user_password';
  
  /* try to connect */
  $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' .    imap_last_error());
  </code></pre>
  
  <p>Now I am stating all my attempts.
  NB - I have tried each attempt by sending new mails to the testing email id</p>
  
  <p>Attempt_1: Using imap_search()</p>
  
  <pre><code>$recent_emails = imap_search($inbox,'RECENT');
  if ($recent_emails)
     echo count($recent_emails);
  else
     echo "false return";
  imap_close($inbox);
  </code></pre>
  
  <p>Now Output of Attempt_1 is "false return";</p>
  
  <p>Attempt_2: Using imap_mailboxmsginfo()</p>
  
  <pre><code>$check = imap_mailboxmsginfo($inbox);
  if ($check)
      echo "Recent: "   . $check-&gt;Recent  . "&lt;br /&gt;\n" ;
  else
      echo "imap_check() failed: " . imap_last_error() . "&lt;br /&gt;\n";
  imap_close($inbox);
  </code></pre>
  
  <p>Here the output is Recent:0 while I have sent 2 new mails to this id</p>
  
  <p>Attempt_3: using imap_status()</p>
  
  <pre><code>$status = imap_status($inbox, $hostname, SA_ALL);
  if ($status)
    echo "Recent:     " . $status-&gt;recent      . "&lt;br /&gt;\n";
  else
    echo "imap_status failed: " . imap_last_error() . "\n";
  </code></pre>
  
  <p>//Output Recent:0</p>
  
  <p>Attempt_4: Using Using imap_search() Again with parameter NEW</p>
  
  <pre><code>$recent_emails = imap_search($inbox,'NEW');
  if ($recent_emails)
     echo count($recent_emails);
  else
     echo "false return";
  imap_close($inbox);
  </code></pre>
  
  <p>Output - false return</p>
  
  <p>So Where Am I WRONG?
  How can I get the recent unread emails count?</p>