2016-07-20 46 views
0

Gmail APIを使用してこのメ​​ッセージから添付ファイルをダウンロードしようとしています。これまでのところ、私はこの情報を取得することができます:私はそれをダウンロードしようとしていたときにこれまでのところ、私は、ファイルをGmail APIの添付ファイルをPHPでダウンロードする

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] => [email protected] 
            ) 

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

答えて

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を確認すると便利です。

+0

大助けありがとうございました –

+0

私は助けてくれてうれしいです! – trajchevska

+0

こんにちはtrajchevska gmail apiのどこにいても、アップローダーでファイルをアップロードして添付ファイルで送信できますが、Gmailのapiのドキュメントはjavaですが、PHPが必要です。 –

関連する問題