Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gmail api. How to get message labels

Tags:

php

gmail-api

How to get message labelIds from Google_Service_Gmail_Message or by messageId

something like that:

$messages = $gmailService()->users_messages->listUsersMessages('me', ['q' => 'newer_than:1d in:anywhere']);

foreach ($messages as $message) {
    $messageLabels = $message->getLabelIds();
}
like image 558
Webeith Avatar asked Jan 26 '16 13:01

Webeith


Video Answer


1 Answers

If you try out the API Explorer at the bottom of the page, you will see that listing messages only gives you the id of the messages:

Response

{
 "messages": [
  {
   "id": "1527ddcca0fd0e08",
   "threadId": "1525a22606f6d608"
  },
  {
   "id": "1527d0e3b13fab83",
   "threadId": "152792b4f30977ae"
  }, ...
 ],
 "nextPageToken": "13090329777308767238",
 "resultSizeEstimate": 100
}

You need to get these messages individually with the message id, which you can try here:

Response

{
 "id": "1527ddcca0fd0e08",
 "threadId": "1525a22606f6d608",
 "labelIds": [
  "INBOX",
  "IMPORTANT",
  "CATEGORY_FORUMS"
 ],
 "historyId": "721186",
 "internalDate": "1453810567000",
 "payload": {
  "mimeType": "multipart/alternative",
  "filename": "", ...

As you can see, this response contains the labelIds.

Example (just getting the first message)

$messages = $service->users_messages->listUsersMessages('me', ['q' => 'newer_than:1d in:anywhere']);
$list = $messages->getMessages();
$messageId = $list[0]->getId();
$message = $service->users_messages->get('me', $messageId, ['format' => 'full']);
$labelIds = $message->getLabelIds(); 
like image 72
Tholle Avatar answered Sep 20 '22 11:09

Tholle