Google's GMail API download attachments

后端 未结 4 818
清歌不尽
清歌不尽 2021-01-05 13:37

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

4条回答
  •  星月不相逢
    2021-01-05 14:19

    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!

提交回复
热议问题