Gmail API: get list of messages marked with a specific label in php

SITUATION

I am setting up the Gmail API for my application.

I need to import all emails with a label, like TRASH, SPAM, SENT, UNREAD, STARRED, etc.

I can get a list of inbox mailboxes and information about a single message. And I can get a list of labels as well as information about one label. But I haven't found out how to combine them.

There seems to be no specific request to get a list of messages given the labelId, and there is no label in the email list.

EXIT REQUESTS:

Get a list of messages:

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 " } }

      

Receive one message:

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

      

Get a list of shortcuts:

{ ["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) { } }

      

Get a single label:

{ ["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) { } }

      

EXAMPLE CODE:

This is an example request for how I get the list of posts in php:

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

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

      

This is how I can get one message:

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

      

QUESTION:

Do you know how I can get all messages tagged with a specific label?

Thank!

+3


source to share


1 answer


Are you asking how to filter the message list using labelid ?



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

      

+5


source







All Articles