Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gmail API: Get list of messages labelled with a specific label in php

THE SITUATION

I am setting up Gmail API for my app.

I need to import all the emails labelled with a specific label, for example TRASH, SPAM, SENT, UNREAD, STARRED etc..

I am able to get the inbox emails list and single message info. And i can get the list of labels as well a single label info. But i didn't find out how to get combine them.

Apparently there isn't a specific request to retrieve a list of messages, given a labelId, and in the email list there is no trace of a labelId.

REQUESTS OUTPUT:

Get message list:

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

Get single message:

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

Get label list:

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

CODE EXAMPLE:

This is a request example of how i get the message list 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 get a single message:

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

THE QUESTION:

Do you know how can i retrieve all the messages labelled with a certain label?

Thank you!

like image 951
FrancescoMussi Avatar asked Jun 10 '15 07:06

FrancescoMussi


1 Answers

Are you asking how to filter a list of messages by labelid?

$opt_param = array['labelIds'] = 'Label_143';
$messagesResponse = 
     $service->users_messages->listUsersMessages($userId, $opt_param);
like image 199
John Mee Avatar answered Oct 22 '22 22:10

John Mee