Google的GMail API下载附件 [英] Google's GMail API download attachments

查看:108
本文介绍了Google的GMail API下载附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将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:

$attachmentData = $service->users_messages_attachments->get($emailAccount, $messageId, $attachmentId);

$myfile = fopen("excel.xlsx", "w");
fwrite($myfile, $attachmentData);
fclose($myfile);

推荐答案

首先,我们需要从附件对象中获取数据:

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

然后在写入文件之前,将数据转换为标准RFC 4648 base64编码:

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);

现在可以使用!

这篇关于Google的GMail API下载附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆