"We support the Gmail REST API on Stack Overflow. Google engineers monitor and answer questions with the gmail-api tag. You should use this tag when asking questions. We aim to answer all questions within a couple of days."
OK so after many frustrating days I found this on the Gmail api support page so here goes.
The short question:
How can I get the attachments for an specific email via the getData() method while looping through $emails from a batch request?
This would be so that when a user loads our inbox they can all the data required to view say 20 messages, instead of having to click on each one and wait for another call to be made which tends to take up to 6 seconds to process.
More Info
I'm trying to upgrade our platform which uses the Gmail API for our Gmail users to manage their inboxes from inside our platform.
I am trying to get all attachments for an email (inline or attached separately) to allow the user to read the email, with any inline images already visible on the page, and also be able to quickly preview any attachments or just download.
I have no issues getting everything I need for a single message.
But as soon as I try to use that code inside a batched request response, I cannot get the attachments out without creating new services and calls for each message.
This really slows everything down as it's making a new call for every email just to get the attachments.
Here's my code:
This is what I am using to get the attachment details out of the relevant part:
public function get_attachment_from_part($service, $emailId, $part) {
//spit_out($service);
//echo 'THIS IS THE FIRST PART:::';
//spit_out($part);
echo $attachmentId = $part->getBody()->getAttachmentId();
echo "<br/>".$filename = $part->getFilename();
echo "<br/>".$mimeType = $part->getMimeType();
echo "<br/>".$partId = $part->getPartId();
$attachment = new stdClass();
$attachment->filename = $filename;
$attachment->displayHeader = 'Content-Type: '.$mimeType;
$attachment->mimeType = $mimeType;
echo "<br/>".$attachment->messageId = $emailId;
$attachment->attachmentId = $attachmentId;
$attachment->partId = $partId;
$file = $service->users_messages_attachments->get('me', $emailId, $attachmentId);
spit_out($file);
$file = $file->getData(); // This fails because the response to $file has nothing in it.
$file = $this->inbox_new_model->decodeBody($file);
$attachment->file = $file;
return $attachment;
}
This is the response I get back when calling the attachments method:
Google_Http_Request Object
(
[batchHeaders:Google_Http_Request:private] => Array
(
[Content-Type] => application/http
[Content-Transfer-Encoding] => binary
[MIME-Version] => 1.0
)
[queryParams:protected] => Array
(
)
[requestMethod:protected] => GET
[requestHeaders:protected] => Array
(
[authorization] => Bearer ya29.Gl0WBEV_MbKok8PAG3YCBtlMI0hMnRsjJ7wCKWPJDAL9nFJlN7TuX8mJICDko-6PuCpsYiqaaVf-NVGzet9SIdPyJ9PZWTsEmlZVHdNOQXn8Kdc1NYOKU5_dNO4QiKU
)
[baseComponent:protected] => https://www.googleapis.com
[path:protected] => /gmail/v1/users/me/messages/15af343839f05824/attachments/ANGjdJ9FYdeQWlu1inYKKPZ6KQZ8G1hkjel1CuOMXKYI8eZNlRl2IjZ8oc_e9TqJ3Q_anX96afyxe6mr3jbZdGnQN0oy-C5uiMi5GMUC-Y_oQgqohgXkG1QcNukKZZ_rgRkqmqLy0O8zcEv2Y5WuOSEeHB9t6iDjmEgWSCy1VXHLmC-3XJVHlHyrCE6-tLHWPvaKf9gdvnuWhyl9XZJ_GdVpvhvJMs8Q5cPwk9jDwuQBw1unHbLqJeZxeGEBYNCH4ri-AuyV048E-3IhFOzfuW-5ST7fDQuhRsBzGKYZax3RDrNQT8mGV3qHXzoISLI
[postBody:protected] =>
[userAgent:protected] =>
[canGzip:protected] =>
[responseHttpCode:protected] =>
[responseHeaders:protected] =>
[responseBody:protected] =>
[expectedClass:protected] => Google_Service_Gmail_MessagePartBody
[accessKey] =>
)
as opposed to this when I use a single request per email:
Google_Service_Gmail_MessagePartBody Object
(
[attachmentId] =>
[data] => ## THE DATA RETURNED HERE IS THE CORRECT FILE AND I CAN DECODE AND USE IT
[size] => 8908
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
)
Thanks in advance. Let me know if you need to see any other parts of the code.
Also to note, everything is retrieve successfully while looping through the batch response, it is only attachments which do not actually get returned in the following request and need to have a separate call made to get the actual attachment data:
$service->users_messages->get('me', $messageId, $optParamsGetMsg);