I\'m using the PHP SDK for the new Gmail API. How do I fetch an attachment from an email?
Here is the API documentation, but in this instance, it\'s missing example
Firstly we need to get the data from the attachment object:
$attachmentObj = $service->users_messages_attachments->get($emailAccount, $messageId, $attachmentId);
$data = $attachmentObj->getData(); //Get data from attachment object
Then before writing to file, convert the data to standard RFC 4648 base64-encoding:
$data = strtr($data, array('-' => '+', '_' => '/'));
$myfile = fopen("excel.xlsx", "w+");;
fwrite($myfile, base64_decode($data));
fclose($myfile);
It now works!