PHP API中的Gmail API下载附件

时间:2016-07-20 02:52:42

标签: php codeigniter api gmail-api

我一直在尝试使用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));
}

1 个答案:

答案 0 :(得分:3)

您需要获取附件的元数据,并将其作为标题信息添加到您下载的页面上。为此,您需要在下载时同时拥有与所选附件相关的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,您可能会发现它很有用。