Can't open downloaded attachments from Gmail API

前端 未结 3 545
孤街浪徒
孤街浪徒 2021-01-22 22:59

I feel like I\'m missing something stupid...

I\'m using the PHP SDK for the new Gmail API to fetch an attachment from an email. I can get the attachment content, but un

3条回答
  •  爱一瞬间的悲伤
    2021-01-22 23:24

    There may be an issue with decoding the attachment data. Try using this string replace on the attachment data before writing it to a file.

      
        $data = $g->gmail->users_messages_attachments->get($email_account, $email_message_id, $p->getBody()->getAttachmentId());
    
        // Converting to standard RFC 4648 base64-encoding
        // see http://en.wikipedia.org/wiki/Base64#Implementations_and_history
        $data = strtr($data, array('-' => '+', '_' => '/'));
    
        $fh = fopen(DOC_ROOT."/file.pdf", "w+");
        fwrite($fh, base64_decode($data->data));
        fclose($fh);
    
    

提交回复
热议问题