2016-08-10 7 views
3

新しいGmail APIにPHP SDKを使用しています。電子メールから添付ファイルを取得するにはどうすればよいですか?GoogleのGmail API添付ファイルをダウンロード

HereはAPIのドキュメントですが、この例ではPHPの例がありません。私は人々がPHPでこれを達成するのを見ましたが。

EDIT:

これは、それが正しいだ場合、私は今のところ、不明な点が持っているものである:それはあなたに動作するかどう

$attachmentData = $service->users_messages_attachments->get($emailAccount, $messageId, $attachmentId); 

$myfile = fopen("excel.xlsx", "w"); 
fwrite($myfile, $attachmentData); 
fclose($myfile); 
+0

これまでに何を試しましたか?あなたのコードを私たちに教えてください。他の人が同じ結果を達成するのを見たことがあるなら、おそらくコードからインスピレーションを得ることができますか? – Tholle

+0

@Tholle私は自分のコードのスニペットを追加しました。それは明確ですか? – dzerow

答えて

4

まず、我々は添付ファイルのオブジェクトからデータを取得する必要があります:標準のRFC 4648のbase64エンコードにデータを変換し、ファイルに書き込む前に

その後
$attachmentObj = $service->users_messages_attachments->get($emailAccount, $messageId, $attachmentId); 
$data = $attachmentObj->getData(); //Get data from attachment object 

$data = strtr($data, array('-' => '+', '_' => '/')); 
$myfile = fopen("excel.xlsx", "w+");; 
fwrite($myfile, base64_decode($data)); 
fclose($myfile); 

それが機能するようになりました!

0

は、このthreadのコードを試してみてください。

<?php 


/** 
* Gmail attachment extractor. 
* 
* Downloads attachments from Gmail and saves it to a file. 
* Uses PHP IMAP extension, so make sure it is enabled in your php.ini, 
* extension=php_imap.dll 
* 
*/ 


set_time_limit(3000); 


/* connect to gmail with your credentials */ 
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX'; 
$username = 'YOUR_GMAIL_USERNAME'; # e.g [email protected] 
$password = 'YOUR_GMAIL_PASSWORD'; 


/* try to connect */ 
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error()); 


/* get all new emails. If set to 'ALL' instead 
* of 'NEW' retrieves all the emails, but can be 
* resource intensive, so the following variable, 
* $max_emails, puts the limit on the number of emails downloaded. 
* 
*/ 
$emails = imap_search($inbox,'ALL'); 

/* useful only if the above search is set to 'ALL' */ 
$max_emails = 16; 


/* if any emails found, iterate through each email */ 
if($emails) { 

    $count = 1; 

    /* put the newest emails on top */ 
    rsort($emails); 

    /* for every email... */ 
    foreach($emails as $email_number) 
    { 

     /* get information specific to this email */ 
     $overview = imap_fetch_overview($inbox,$email_number,0); 

     /* get mail message, not actually used here. 
      Refer to http://php.net/manual/en/function.imap-fetchbody.php 
      for details on the third parameter. 
     */ 
     $message = imap_fetchbody($inbox,$email_number,2); 

     /* get mail structure */ 
     $structure = imap_fetchstructure($inbox, $email_number); 

     $attachments = array(); 

     /* if any attachments found... */ 
     if(isset($structure->parts) && count($structure->parts)) 
     { 
      for($i = 0; $i < count($structure->parts); $i++) 
      { 
       $attachments[$i] = array(
        'is_attachment' => false, 
        'filename' => '', 
        'name' => '', 
        'attachment' => '' 
       ); 

       if($structure->parts[$i]->ifdparameters) 
       { 
        foreach($structure->parts[$i]->dparameters as $object) 
        { 
         if(strtolower($object->attribute) == 'filename') 
         { 
          $attachments[$i]['is_attachment'] = true; 
          $attachments[$i]['filename'] = $object->value; 
         } 
        } 
       } 

       if($structure->parts[$i]->ifparameters) 
       { 
        foreach($structure->parts[$i]->parameters as $object) 
        { 
         if(strtolower($object->attribute) == 'name') 
         { 
          $attachments[$i]['is_attachment'] = true; 
          $attachments[$i]['name'] = $object->value; 
         } 
        } 
       } 

       if($attachments[$i]['is_attachment']) 
       { 
        $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1); 

        /* 3 = BASE64 encoding */ 
        if($structure->parts[$i]->encoding == 3) 
        { 
         $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']); 
        } 
        /* 4 = QUOTED-PRINTABLE encoding */ 
        elseif($structure->parts[$i]->encoding == 4) 
        { 
         $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']); 
        } 
       } 
      } 
     } 

     /* iterate through each attachment and save it */ 
     foreach($attachments as $attachment) 
     { 
      if($attachment['is_attachment'] == 1) 
      { 
       $filename = $attachment['name']; 
       if(empty($filename)) $filename = $attachment['filename']; 

       if(empty($filename)) $filename = time() . ".dat"; 

       /* prefix the email number to the filename in case two emails 
       * have the attachment with the same file name. 
       */ 
       $fp = fopen("./" . $email_number . "-" . $filename, "w+"); 
       fwrite($fp, $attachment['attachment']); 
       fclose($fp); 
      } 

     } 

     if($count++ >= $max_emails) break; 
    } 

} 

/* close the connection */ 
imap_close($inbox); 

echo "Done"; 

?> 

詳細については、この関連スレッドをチェックして、そこで質問

+0

Google GMail APIを使用してこれを達成しようとしている説明、タグ、およびタグで指定しました。 – dzerow

関連する問題