1

In PHP, using Gmail api I am fetching client's gmail inbox.

Using

Array ( ['maxResults'] => 50, ['labelIds'] => INBOX, ['q'] => is:unread)

I am searching for unread emails in mailbox/gmail but I got 1 email which is labelled with

[labelIds] => Array([0] => Label_2 [1] => SENT). 

How gmail identify this email as unread & on what bases I can highlight this unread email? my 2nd question is the email is labelled with SENT how come it appeared into INBOX ?

$service = new Google_Service_Gmail($client);
        $batch = new Google_Http_Batch($client);
        $userId='me';
        $threads = array();
        $pageToken = NULL;
        $param = array();
        $getPageToken = $getParam('pageToken');
        if ($getPageToken && $getPageToken!='') {
            $param['pageToken'] = $getPageToken;
            $pageToken = $getPageToken;
        }
        if($getParam('limit'))
            $param['maxResults']=$getParam('limit');
        else
            $param['maxResults'] = 50;
        if($getParam('in')) {
            $this->view->in=$getParam('in');
            $param['labelIds'] = strtoupper($this->view->in);
        }
        else
            $param['labelIds'] = 'INBOX';
        $search = "";
        $read = $getParam('read_status');
        if($read && (@$read=='read'||$read=='unread'))
            $search = 'is:'.$read;

        if($getParam('search')) {
            $this->view->search = $getParam('search');
            $search .= " ".$this->view->search;
        }
        if($search)
            $param['q'] = $search;
            $threadsResponse=$this->gmailApi->get_threads($service,$userId,$param);
        if($threadsResponse) {
                $threads=$threadsResponse->getThreads();
            $this->view->next_page=$threadsResponse->getNextPageToken();
            $client->setUseBatch(true);
            foreach ($threads as $key => $val) {
                    $thread = $service->users_threads->get($userId, $val->id,['format' => 'full', 'metadataHeaders' => ['From','Date','Subject']]);      
                $batch->add($thread, "mail-".$val->id);
            }
            $results = $batch->execute();
                $threads=$this->gmailApi->get_batch_messages($service,$userId,$results);

can anyone tell me if this is the issue generated here(https://issuetracker.google.com/issues/135166258)

softech
  • 356
  • 1
  • 4
  • 23
  • Any chance you can post your PHP code to help others in the future? – Linda Lawton - DaImTo Sep 20 '19 at 13:08
  • It is always useful to test with the Try this API to understand either the problem is originating from the request itself or its implementation in PHP: https://developers.google.com/gmail/api/v1/reference/users/messages/list?apix_params=%7B%22userId%22%3A%22me%22%2C%22q%22%3A%22label%3Ainbox%20label%3Aunread%22%7D – ziganotschka Sep 20 '19 at 14:46
  • @ziganotschka how can I use `Try this API` I don't have login details – softech Sep 25 '19 at 12:03
  • @softech You can log in with the same Google or Gmail account you use to access your Inbox – ziganotschka Sep 25 '19 at 12:54
  • @ziganotschka this problem is in one client's gmail account & I can't duplicate it into my gmail & if I log in with the same Google or Gmail account which I use to access my Inbox than it will give my inbox – softech Sep 26 '19 at 07:28

2 Answers2

0

You are only searching on emails in that are unread. If you want unread and in inbox label then try using

q = label:unread label:inbox 

In the future the easiest way to test gmail search is in the gmail web application you can get the search results you want there and just copy it into q.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
0

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

if you list all messages of the thread, you will also list the sent messages.

Instead of querying

$this->gmailApi->get_threads($service,$userId,$param);

you need to use

$service->users_messages->listUsersMessages($userId, $param);

specifying labelIds[] as optional parameter for this method.

Why?

  • If you query for threads with "is:unread" as q parameter and labelIds "INBOX", the results will contain all threads which contain at least one unread message in the inbox.
  • If in the next step you retrieve all messages belonging to those threads, you will obtain all messages belonging to those threads - some of which might be read and/ or not in the inbox.
  • Instead, you need to directly query only for all those messages (not threads) with 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");
  }
 }
ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • No I am listing only inbox labelid using same method which you mentioned – softech Sep 26 '19 at 11:15
  • If I am not mistaken, you are getting all messages for the listed threads, rather than listing all messages with the specified labels – ziganotschka Sep 26 '19 at 11:23
  • I am searching for `unread` emails in `INBOX`, in question I already mentioned list of parameters which I am passing. but in response I am getting 1 or 2 emails which do not have INBOX label nor UNREAD label – softech Sep 27 '19 at 05:29
  • @softech: I updated my answer, hopefully it is more clear now. – ziganotschka Oct 01 '19 at 14:09
  • @ziganotchka I know the results of my code will contain all threads which contain at least one unread message in the inbox but there is only single message when I open that thread & also my 2nd question is the email is labelled with SENT how come it appeared into INBOX ? – softech Oct 03 '19 at 10:30
  • So every of your threads has only one email? As for your second question, the thread will have the label INBOX if it contains at least one email with the flag INBOX, but the same thread can also contain emails in SENT so these emails will also come up in your your results if you implement `q` when listing threads rather than listing messages – ziganotschka Oct 03 '19 at 10:33
  • but I think that in a thread one message must have `INBOX` label to come up with when fetch `INBOX` labelled thread/message. So every of your threads has only one email? - Not for all thread but yes, there is only 1 email in which emails this issue is happening. also pl note that it does not mean that this issue is for all those emails who has only 1 email in thread – softech Oct 03 '19 at 10:43
  • Maybe the easiest is if you try my modification suggestion and see if it solves the problem or not. – ziganotschka Oct 03 '19 at 10:45