Reputation: 715
I am using the Google API Client Library for PHP in conjunction with the Gmail API to download and process some emails.
I am getting the messages without any problems:
$optParamsGet = [];
$optParamsGet['format'] = 'full'; // Display message in payload
$message = $service->users_messages->get('me',$id,$optParamsGet);
$messagePayload = $message->getPayload();
Then I process getParts() array and come to the first attachment, which is a .eml file with no name
[modelData:protected] => Array
(
[headers] => Array
(
[0] => Array
(
[name] => Content-Type
[value] => message/rfc822
)
[1] => Array
(
[name] => Content-Disposition
[value] => attachment; creation-date="Fri, 24 Jul 2015 00:20:48 GMT"; modification-date="Fri, 24 Jul 2015 00:20:48 GMT"
)
)
[body] => Array
(
[attachmentId] => ANGjdJ_TfJlMTssCNackUcQfT...mpppKiT8sSg
[size] => 37099
)
)
My question is: How do I unencode the "attachmentId" string? I thought it was a base64 encoding, but the command
echo base64_decode($parts[1]->body->attachmentId);
Just spits out binary data.
What did I miss?
Upvotes: 2
Views: 2161
Reputation: 112777
The attachmentId is not the data of you attachment, but an identifier of the attachment. To get the attachment, you need to do one more request with the messageId and the attachmentId, like so:
$attachmentData= $service->users_messages_attachments->get('me', $messageId, $attachmentId);
// Replace all '-' with '+' and '_' with '/' to make the url-safe
// base64 encoded data to regular base64.
$decodedData = strtr($attachmentData, array('-' => '+', '_' => '/'));
Upvotes: 6