Gmail API。如何获取邮件标签

2022-04-14 00:00:00 gmail-api php

如何从Google_Service_Gmail_Message或通过消息ID获取消息标签ID

类似的内容:

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

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

解决方案

如果您尝试页面底部的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(); 

相关文章