Search code examples
phpgmailgoogle-oauthgmail-api

Gmail OAuth API Count Messages PHP


I have the following code to count unread emails with Gmail OAuth API in PHP. The problem I have it will only return a max 100 due to API rate limits. Is there anyway to count unread emails using oauth (not imap) and return correct value even if more than 100 items?

$optParams['labelIds'] = 'UNREAD'; 

            $messages = $service->users_messages->listUsersMessages('me',$optParams);
   $list = $messages->getMessages();


    $result = count($list);
    echo "Total Inbox = ".$result ;

Solution

  • There is an estimatedResultCount that's always returned on all list() calls but it's an estimate clearly.

    If you want an exact count of total+unread messages on a label then you can now get that from label.get().