Gmail api。如何获取消息标签

时间:2016-01-26 13:26:52

标签: php gmail-api

如何从Google_Service_Gmail_Message或messageId

获取邮件labelIds

类似的东西:

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

foreach ($messages as $message) {
    $messageLabels = $message->getLabelIds();
}

1 个答案:

答案 0 :(得分:5)

如果您试用页面底部的API Explorer,您会看到列出的消息只会显示消息的ID:

<强>响应

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

您需要使用消息ID单独获取这些消息,您可以try here

<强>响应

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

如您所见,此回复包含labelIds

示例(只收到第一条消息)

$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();