php - 从 Google Mail API 解码 PHP 中的邮件附件

标签 php google-api-php-client gmail-api

我将适用于 PHP 的 Google API 客户端库与 Gmail API 结合使用来下载和处理一些电子邮件。

我收到消息没有任何问题:

$optParamsGet = [];
$optParamsGet['format'] = 'full'; // Display message in payload
$message = $service->users_messages->get('me',$id,$optParamsGet);
$messagePayload = $message->getPayload();

然后我处理 getParts() 数组并找到第一个附件,这是一个没有名称的 .eml 文件

    [modelData:protected] => Array
        (
            [headers] => Array
                (
                    [0] => Array
                        (
                            [name] => Content-Type
                            [value] => message/rfc822
                        )
                    [1] => Array
                        (
                            [name] => Content-Disposition
                            [value] => attachment; creation-date="Fri, 24 Jul 2015 00:20:48 GMT"; modification-date="Fri, 24 Jul 2015 00:20:48 GMT"
                        )
                )
            [body] => Array
                (
                    [attachmentId] => ANGjdJ_TfJlMTssCNackUcQfT...mpppKiT8sSg
                    [size] => 37099
                )
        )

我的问题是:如何取消“attachmentId”字符串的编码? 我以为是base64编码,但是命令

echo base64_decode($parts[1]->body->attachmentId);

只是输出二进制数据。

我错过了什么?

最佳答案

attachmentId 不是您附件的数据,而是附件的标识符。要获取附件,您需要使用 messageId 和 AttachmentId 再执行一次请求,如下所示:

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

// Replace all '-' with '+' and '_' with '/' to make the url-safe
// base64 encoded data to regular base64.
$decodedData = strtr($attachmentData, array('-' => '+', '_' => '/'));

关于php - 从 Google Mail API 解码 PHP 中的邮件附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31758549/

相关文章:

php - 使用 Composer 时如何从供应商文件夹中调用类?

google-api - 通过具有特定页面设置(边距、方向)的 Drive API 创建文档

javascript - Google Oauth 使用 javascript : Token has expired 的跨客户端身份验证

在 Gmail API 中搜索具有特定 threadId 的邮件

php - 不使用 "Application Specific Password"从 gmail 获取邮件

php - ListView 未从具有重复行的数据库中正确填充

php - Drupal - 如何以编程方式更新 CCK NodeReference 字段?

PHP - 在函数内定义类

php - 如何在 PHP 中访问 Google Spreadsheets API?

python - 如何使用 Gmail API 在现有线程中创建草稿消息