The solution was pretty simple. The was creating problems. It was supposed to be capital.and
All of these worked:
is:unread in:random
is:unread AND label:random
label:unread label:random
label:unread AND label:random
You retrieve all threads with the label 'Inbox' and then you retrieve all messages for those threads.
Be aware that a thread contains both incoming and outgoing messages.
So it will contain the label 'Inbox' (for the incoming messages), but
Instead of querying
$this->gmailApi->get_threads($service,$userId,$param);
you need to use
$service->users_messages->listUsersMessages($userId, $param);
specifying as optional parameter for this method.labelIds[]
q parameter and labelIds "INBOX", the results will contain all threads which contain at least one unread message in the inbox.q "is:unread" and labelIds "INBOX"Sample code snippet based on your code:
//the following code works only if setUseBatch() is set to false
$client->setUseBatch(false);
$messagesResponse = $service->users_messages->listUsersMessages($userId, $param);
if ($messagesResponse) {
$messages = $messagesResponse->getMessages();
foreach ($messages as $key => $val) {
print ("\n unread message id ".$val->id."\n");
}
}