我正在为新的 Gmail API 使用 PHP SDK。如何从电子邮件中提取附件?
Here是 API 文档,但在本例中,缺少 PHP 示例。虽然我看到有人用 PHP 实现了这一点。
编辑:
这是我目前所知道的,不确定是否正确:
$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
然后在写入文件之前,将数据转换为标准 RFC 4648 base64 编码:
$data = strtr($data, array('-' => '+', '_' => '/'));
$myfile = fopen("excel.xlsx", "w+");;
fwrite($myfile, base64_decode($data));
fclose($myfile);
现在可以了!
关于php - Google 的 GMail API 下载附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38876996/