2016-07-20 12 views
0

Ich habe versucht, die angehängte Datei von dieser Nachricht mit Google Mail-API herunterladen. Bisher kann ich diese Informationen erhalten:Google Mail API-Download-Anhang in 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 
    ) 

) 

Bisher, wenn ich es zum Download versuche ich die Datei immer bin, aber wenn ich es öffnen, ich bin nicht in der Lage, die Datei zu sehen. Sieht aus wie eine beschädigte Datei:

public function download(){ 

    $file = 'popoy.jpg'; 

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

Antwort

3

Sie müssen die Meta-Daten für die Befestigung und fügen Sie es als Kopf Infos auf der Seite, wo Sie den Download zu tun bekommen. Zu diesem Zweck müssen Sie zum Zeitpunkt des Downloads sowohl die Nachricht-ID als auch die Teil-ID haben, die sich auf den ausgewählten Anhang beziehen. Sie können sie als Get-Parameter an die Download-Seite senden.

Der Download-Code würde wie folgt sein:

$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']; 

ich einen Wrapper erstellt haben, die den größten Teil der Google Mail-Funktionen umfasst einschließlich des Hinzufügens/Vorschau-Anhänge. Sie können es überprüfen here, könnten Sie es nützlich finden.

+0

danke sir, das ist eine große Hilfe –

+0

Ich bin froh, dass es hilfreich war! – trajchevska

+0

Hallo Trajchevska haben Sie eine Idee hier in Google Mail api, wo ich eine Datei mit Box hochladen und senden Sie es mit Anhang, weil die Dokumentation in der Gmail API in Java ist, aber ich brauche PHP –