Google的GMail API下载附件 [英] Google's GMail API download attachments
问题描述
我将PHP SDK用于新的Gmail API.如何从电子邮件中提取附件?
I'm using the PHP SDK for the new Gmail API. How do I fetch an attachment from an email?
是API文档, 此处,但是在这种情况下,它缺少PHP的示例.尽管我见过人们用PHP做到这一点. Here is the API documentation, but in this instance, it's missing example for PHP. Although I have seen people achieving this with PHP. 这是我到目前为止所能获得的,不确定是否正确: This is what I have so far, unsure if it's correct:
首先,我们需要从附件对象中获取数据: Firstly we need to get the data from the attachment object: 然后在写入文件之前,将数据转换为标准RFC 4648 base64编码: Then before writing to file, convert the data to standard RFC 4648 base64-encoding: 现在可以使用! 这篇关于Google的GMail API下载附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!$attachmentData = $service->users_messages_attachments->get($emailAccount, $messageId, $attachmentId);
$myfile = fopen("excel.xlsx", "w");
fwrite($myfile, $attachmentData);
fclose($myfile);
推荐答案
$attachmentObj = $service->users_messages_attachments->get($emailAccount, $messageId, $attachmentId);
$data = $attachmentObj->getData(); //Get data from attachment object
$data = strtr($data, array('-' => '+', '_' => '/'));
$myfile = fopen("excel.xlsx", "w+");;
fwrite($myfile, base64_decode($data));
fclose($myfile);