I’m using Gmail API to fetch email address (of the sender) for last 10 emails.
URL: https://developers.google.com/gmail/api/v1/reference/users/messages/list
But Users.messages: list
API returns message resource (which includes only email ID).
And for fetching sender email address, I have to make one Users.messages: get
API call for each message resources, that’s increases load time.
How can I fetch email address of sender along with email ID in the response of Users.messages: list
API?
OR Somehow I can fetch last 10 sender email address in one API call?
My current code Example:
$params = [ 'labelIds' => 'INBOX', 'maxResults' => 10 ]; $emails = $service->users_messages->listUsersMessages('me', $params); foreach ($emails->getMessages() as $email) { $email_data = $service->users_messages->get('me', $email->id, ['format' => 'full']); }
Advertisement
Answer
How can I fetch email address of sender along with email ID in the response of Users.messages: list API?
Answer:
You can’t, Users.messages: get
needs to be used to retrieve this information.
More Information:
As per the Users.messages: list
documentation about the properties of the response resource:
messages[]
: List of messages. Note that each message resource contains only an id and a threadId. Additional message details can be fetched using themessages: get
method.
The Users.message
resource page gives more information about the full response which is able to be retrieved from messages: get
:
{ "id": string, "threadId": string, "labelIds": [ string ], "snippet": string, "historyId": unsigned long, "internalDate": long, "payload": { "partId": string, "mimeType": string, "filename": string, "headers": [ { "name": string, "value": string } ], "body": users.messages.attachments Resource, "parts": [ (MessagePart) ] }, "sizeEstimate": integer, "raw": bytes }
What you need to do:
In order to get the message ID and sender email address, you need to make a Users.messages: list
call to get the email IDs, then make a Users.messages: get
request on each message ID to get the sender email. As there is no Users.messages: batchGet
method, there is unfortunately no way around this.