Gmail API:获取在php

时间:2015-06-10 07:56:17

标签: php email gmail google-api-php-client gmail-api

情况

我正在为我的应用设置Gmail API

我需要导入所有标有特定标签的电子邮件,例如TRASH,SPAM,SENT,UNREAD,STARRED等。

我可以获取收件箱电子邮件列表和单个邮件信息。我可以获得标签列表以及单个标签信息。但我没有找到如何将它们结合起来。

显然,在给定labelId的情况下,没有特定的请求来检索消息列表,并且在电子邮件列表中没有labelId的痕迹。

请求输出:

获取消息列表:

array(3) { [0]=> array(5) { ["messageId"]=> string(16) "14ddc24465a9b72e" ["messageSnippet"]=> string(14) "sample message" ["messageSubject"]=> string(4) "Test" ["messageDate"]=> string(22) "Jun 10th 2015 08:24 AM" ["messageSender"]=> string(44) "SENDER_NAME" } [1]=> array(5) { ["messageId"]=> string(16) "14dd8391372f3035" ["messageSnippet"]=> string(91) "buonasera Date: Tue, 9 Jun 2015 08:08:48 -0400 Message-Id: <CAGL50m_2vPv-Bkzbd+m5iFkx-u-" ["messageSubject"]=> string(4) "Test" ["messageDate"]=> string(22) "Jun 10th 2015 08:24 AM" ["messageSender"]=> string(44) "SENDER_NAME" } [2]=> array(5) { ["messageId"]=> string(16) "14dd7f4f126103c9" ["messageSnippet"]=> string(99) "Ciao meetinghand Suggerimenti per ottenere il massimo da Gmail Importa contatti e messaggi in Gmail" ["messageSubject"]=> string(49) "Tre suggerimenti per ottenere il massimo da Gmail" ["messageDate"]=> string(21) "Jun 9th 2015 12:54 PM" ["messageSender"]=> string(42) "Il team di Gmail " } }

获取单个消息:

string(16) "14dd7f4f126103c9" string(49) "SUBJECT" string(21) "Jun 9th 2015 12:54 PM" string(42) "SENDER_NAME"

获取标签列表:

{ ["id"]=> string(7) "STARRED" ["name"]=> string(7) "STARRED" ["type"]=> string(6) "system" } [12]=> array(5) { ["id"]=> string(4) "SPAM" ["name"]=> string(4) "SPAM" ["messageListVisibility"]=> string(4) "hide" ["labelListVisibility"]=> string(9) "labelHide" ["type"]=> string(6) "system" } [13]=> array(3) { ["id"]=> string(4) "SENT" ["name"]=> string(4) "SENT" ["type"]=> string(6) "system" } } } ["processed":protected]=> array(0) { } }

获取单一标签:

{ ["internal_gapi_mappings":protected]=> array(0) { } ["id"]=> string(5) "TRASH" ["labelListVisibility"]=> string(9) "labelHide" ["messageListVisibility"]=> string(4) "hide" ["messagesTotal"]=> int(1) ["messagesUnread"]=> int(1) ["name"]=> string(5) "TRASH" ["threadsTotal"]=> int(1) ["threadsUnread"]=> int(1) ["type"]=> string(6) "system" ["modelData":protected]=> array(0) { } ["processed":protected]=> array(0) { } }

代码示例:

这是我如何在php中获取消息列表的请求示例:

public function gmail_get_messages()
{
    $service = $this->gmail_init_service();

    $list = $service->users_messages->listUsersMessages('me',['maxResults' => 10]);
    $messageList = $list->getMessages();
}

这就是我收到一条消息的方式:

$message = $service->users_messages->get( 'me', 'MESSAGE_ID');

问题:

您知道如何检索标有特定标签的所有邮件吗?

谢谢!

1 个答案:

答案 0 :(得分:6)

您是否在询问如何filter a list of messages by labelid

$opt_param = array['labelIds'] = 'Label_143';
$messagesResponse = 
     $service->users_messages->listUsersMessages($userId, $opt_param);