【问题标题】:decoding mail attachments in PHP from Google Mail API从 Google Mail API 解码 PHP 中的邮件附件
【发布时间】:2015-10-23 20:43:28
【问题描述】:

我将用于 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);

只是吐出二进制数据。

我错过了什么?

【问题讨论】:

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


    【解决方案1】:

    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('-' => '+', '_' => '/'));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-28
      • 2015-11-18
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 2015-06-18
      • 1970-01-01
      相关资源
      最近更新 更多