【问题标题】:Gmail API downloading attachment in PHPGmail API 在 PHP 中下载附件
【发布时间】:2016-11-23 02:48:02
【问题描述】:

我一直在尝试使用 Gmail API 下载此邮件的附件。到目前为止,我可以得到这些信息:

Google_Service_Gmail_Message Object
(
[collection_key:protected] => labelIds
[internal_gapi_mappings:protected] => Array
    (
    )

[historyId] => 1571
[id] => 155e27d1fe295bbd
[internalDate] => 1468383698000
[labelIds] => Array
    (
        [0] => SENT
        [1] => INBOX
        [2] => IMPORTANT
    )

[payloadType:protected] => Google_Service_Gmail_MessagePart
[payloadDataType:protected] => 
[raw] => 
[sizeEstimate] => 19335
[snippet] => POPOY
[threadId] => 155e27d1fe295bbd
[modelData:protected] => Array
    (
        [payload] => Google_Service_Gmail_MessagePart Object
            (
                [collection_key:protected] => parts
                [internal_gapi_mappings:protected] => Array
                    (
                    )

                [bodyType:protected] => Google_Service_Gmail_MessagePartBody
                [bodyDataType:protected] => 
                [filename] => 
                [headersType:protected] => Google_Service_Gmail_MessagePartHeader
                [headersDataType:protected] => array
                [mimeType] => multipart/mixed
                [partId] => 
                [partsType:protected] => Google_Service_Gmail_MessagePart
                [partsDataType:protected] => array
                [modelData:protected] => Array
                    (
                        [headers] => Array
                            (
                                [0] => Array
                                    (
                                        [name] => MIME-Version
                                        [value] => 1.0
                                    )

                                [1] => Array
                                    (
                                        [name] => Received
                                        [value] => by 10.140.92.230 with HTTP; Tue, 12 Jul 2016 21:21:38 -0700 (PDT)
                                    )

                                [2] => Array
                                    (
                                        [name] => Date
                                        [value] => Wed, 13 Jul 2016 12:21:38 +0800
                                    )

                                [3] => Array
                                    (
                                        [name] => Delivered-To
                                        [value] => maingabbytest@gmail.com
                                    )

                                [4] => Array
                                    (
                                        [name] => Message-ID
                                        [value] => 
                                    )

                                [5] => Array
                                    (
                                        [name] => Subject
                                        [value] => test w/ attachment
                                    )

                                [6] => Array
                                    (
                                        [name] => From
                                        [value] => Chris Jason Barredo 
                                    )

                                [7] => Array
                                    (
                                        [name] => To
                                        [value] => Chris Jason Barredo 
                                    )

                                [8] => Array
                                    (
                                        [name] => Content-Type
                                        [value] => multipart/mixed; boundary=001a11c13ea0c1971805377cb89b
                                    )

                            )

                        [body] => Array
                            (
                                [size] => 0
                            )

                        [parts] => Array
                            (
                                [0] => Google_Service_Gmail_MessagePart Object
                                    (
                                        [collection_key:protected] => parts
                                        [internal_gapi_mappings:protected] => Array
                                            (
                                            )

                                        [bodyType:protected] => Google_Service_Gmail_MessagePartBody
                                        [bodyDataType:protected] => 
                                        [filename] => 
                                        [headersType:protected] => Google_Service_Gmail_MessagePartHeader
                                        [headersDataType:protected] => array
                                        [mimeType] => multipart/alternative
                                        [partId] => 
                                        [partsType:protected] => Google_Service_Gmail_MessagePart
                                        [partsDataType:protected] => array
                                        [modelData:protected] => Array
                                            (
                                                [headers] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [name] => Content-Type
                                                                [value] => multipart/alternative; boundary=001a11c13ea0c1970b05377cb899
                                                            )

                                                    )

                                                [body] => Array
                                                    (
                                                        [size] => 0
                                                    )

                                                [parts] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [partId] => 0.0
                                                                [mimeType] => text/plain
                                                                [filename] => 
                                                                [headers] => Array
                                                                    (
                                                                        [0] => Array
                                                                            (
                                                                                [name] => Content-Type
                                                                                [value] => text/plain; charset=UTF-8
                                                                            )

                                                                    )

                                                                [body] => Array
                                                                    (
                                                                        [size] => 7
                                                                        [data] => UE9QT1kNCg==
                                                                    )

                                                            )

                                                        [1] => Array
                                                            (
                                                                [partId] => 0.1
                                                                [mimeType] => text/html
                                                                [filename] => 
                                                                [headers] => Array
                                                                    (
                                                                        [0] => Array
                                                                            (
                                                                                [name] => Content-Type
                                                                                [value] => text/html; charset=UTF-8
                                                                            )

                                                                    )

                                                                [body] => Array
                                                                    (
                                                                        [size] => 28
                                                                        [data] => PGRpdiBkaXI9Imx0ciI-UE9QT1k8L2Rpdj4NCg==
                                                                    )

                                                            )

                                                    )

                                            )

                                        [processed:protected] => Array
                                            (
                                            )

                                    )

                                [1] => Google_Service_Gmail_MessagePart Object
                                    (
                                        [collection_key:protected] => parts
                                        [internal_gapi_mappings:protected] => Array
                                            (
                                            )

                                        [bodyType:protected] => Google_Service_Gmail_MessagePartBody
                                        [bodyDataType:protected] => 
                                        [filename] => popoy.jpeg
                                        [headersType:protected] => Google_Service_Gmail_MessagePartHeader
                                        [headersDataType:protected] => array
                                        [mimeType] => image/jpeg
                                        [partId] => 1
                                        [partsType:protected] => Google_Service_Gmail_MessagePart
                                        [partsDataType:protected] => array
                                        [modelData:protected] => Array
                                            (
                                                [headers] => Array
                                                    (
                                                        [0] => Google_Service_Gmail_MessagePartHeader Object
                                                            (
                                                                [internal_gapi_mappings:protected] => Array
                                                                    (
                                                                    )

                                                                [name] => Content-Type
                                                                [value] => image/jpeg; name="popoy.jpeg"
                                                                [modelData:protected] => Array
                                                                    (
                                                                    )

                                                                [processed:protected] => Array
                                                                    (
                                                                    )

                                                            )

                                                        [1] => Google_Service_Gmail_MessagePartHeader Object
                                                            (
                                                                [internal_gapi_mappings:protected] => Array
                                                                    (
                                                                    )

                                                                [name] => Content-Disposition
                                                                [value] => attachment; filename="popoy.jpeg"
                                                                [modelData:protected] => Array
                                                                    (
                                                                    )

                                                                [processed:protected] => Array
                                                                    (
                                                                    )

                                                            )

                                                        [2] => Google_Service_Gmail_MessagePartHeader Object
                                                            (
                                                                [internal_gapi_mappings:protected] => Array
                                                                    (
                                                                    )

                                                                [name] => Content-Transfer-Encoding
                                                                [value] => base64
                                                                [modelData:protected] => Array
                                                                    (
                                                                    )

                                                                [processed:protected] => Array
                                                                    (
                                                                    )

                                                            )

                                                        [3] => Google_Service_Gmail_MessagePartHeader Object
                                                            (
                                                                [internal_gapi_mappings:protected] => Array
                                                                    (
                                                                    )

                                                                [name] => X-Attachment-Id
                                                                [value] => f_iqkdq8dk0
                                                                [modelData:protected] => Array
                                                                    (
                                                                    )

                                                                [processed:protected] => Array
                                                                    (
                                                                    )

                                                            )

                                                    )

                                                [body] => Google_Service_Gmail_MessagePartBody Object
                                                    (
                                                        [internal_gapi_mappings:protected] => Array
                                                            (
                                                            )

                                                        [attachmentId] => ANGjdJ-18nbgORhdtkl3nD4dezCwSPODA0qgR7UsIeoGWQp8xIC4VFCvxP0xRUkMGQYm1uAqbMTP8L_wMgZFA1IEy5FXwgXeJOW9UmJGB87SuBIjNezVlXOY-6OhbxMktWTfgkiGx4Eb6dhFkrY-iWvbOWEjmWajiT54oj0SoaHQvNXGI-HmZZ2L6m62DHSPQCWn0qSRZsMcxddXSlczatQtCcjvMR7Hy9rhoNxe2Rd2bf_jU2PyHbsQxtvaWvp-lx_fnBrcWX5pjWhW5x3XWzrxhgRIF4xJfGa_fqV7g-G750vqbj5vRRAWX_cPvuU
                                                        [data] => 
                                                        [size] => 18308
                                                        [modelData:protected] => Array
                                                            (
                                                            )

                                                        [processed:protected] => Array
                                                            (
                                                            )

                                                    )

                                            )

                                        [processed:protected] => Array
                                            (
                                                [body] => 1
                                                [headers] => 1
                                            )

                                    )

                            )

                    )

                [processed:protected] => Array
                    (
                        [parts] => 1
                    )

            )

    )

[processed:protected] => Array
    (
        [payload] => 1
    )

)

到目前为止,当我尝试下载它时,我正在获取该文件,但是当我打开它时,我无法看到该文件。看起来像一个损坏的文件:

public function download(){

    $file = 'popoy.jpg';

    header('Content-Type: application/download');
    header('Content-Disposition: attachment; filename="'.$file.'"');
    header("Content-Length: " . filesize($file));
}

【问题讨论】:

    标签: php codeigniter api gmail-api


    【解决方案1】:

    您需要获取附件的元数据并将其作为标题信息添加到您进行下载的页面上。为此,您需要在下载时同时拥有与所选附件相关的 messageId 和 partId。您可以将它们作为获取参数发送到下载页面。

    下载代码是这样的:

    $files = [];
    $gmail = new Google_Service_Gmail($client);
    $attachmentHeaders = [];
    $message = $gmail->users_messages->get($userId, $messageId);
    $messageDetails = $message->getPayload();
    foreach ($messageDetails['parts'][$partId]['headers'] as $item) {
        $attachmentHeaders[$item->name] = $item->value;
    }
    $attachmentDetails = ['mimeType' => $messageDetails['parts'][$partId]['mimeType'], 'filename' => $messageDetails['parts'][$partId]['filename'] ,'headers' => $attachmentHeaders, 'attachmentId' => $messageDetails['parts'][$partId]['body']['attachmentId']];
    $attachment = $gmail->users_messages_attachments->get($userId, $messageId, $attachmentDetails['attachmentId']);
    $attachmentDetails['data'] = $this->base64UrlDecode($attachment->data);
    
    foreach ($attachmentDetails['headers'] as $key => $value) {
        header($key.':'.$value);
    }
    echo $attachmentDetails['data'];
    

    我创建了一个包装器,它涵盖了 gmail 的大部分功能,包括添加/预览附件。您可以检查它here,您可能会发现它很有用。

    【讨论】:

    • 我很高兴它有帮助!
    • 嗨 trajchevska 你在 gmail api 有什么想法吗?我可以用盒子上传器上传文件并用附件发送它,因为 gmail api 中的文档在 java 中,但我需要 php
    • 您可以在我上面分享的 gmail 包装器中找到一个示例。这是 github 源代码 - github.com/adevait/GmailPHP。在示例文件夹中查找 send.php。
    • 顺便说一句,非常感谢 trachevska,你帮了大忙,上帝保佑你
    猜你喜欢
    • 2014-11-08
    • 1970-01-01
    • 2016-03-28
    • 2022-11-11
    • 2018-11-03
    • 1970-01-01
    • 2018-12-15
    • 2016-02-22
    • 2014-10-01
    相关资源
    最近更新 更多