Fetching attachments from gmail via either python or php

后端 未结 5 1030
终归单人心
终归单人心 2021-02-06 09:33

I have been trying to find information on how to retrieve attachments from a gmail account in either python or PHP, I\'m hoping that someone here can be of some help, thanks.

5条回答
  •  不知归路
    2021-02-06 10:02

    I recently worked on this topic and here is the code which works well. It also saves the details of the attachments in a word document with the following details:
    -> Date
    -> Time
    -> From
    -> Email ID
    -> Subject
    
    
    
    
    
    
    
    
    
    
    Nmsgs}",0);
        foreach($result as $overview){
        echo "#{$overview->msgno}({$overview->date})-From: {$overview->from}
        {$overview->subject}\n"}
        /* get mail message */
        $message = imap_fetchbody($inbox,$email_number,2);
        /* get mail structure */
        $structure =imap_fetchstructure($inbox, $email_number);
        //$functions = array('function1' => imap_fetchstructure($inbox,
        $email_number));
        //print_r(array_keys($functions));
        $attachments = array();
        //print_r(array_keys($attachments[$i]));
        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);
         /* 4 = QUOTED-PRINTABLE encoding */
         if($structure->parts[$i]->encoding == 3){ 
         $attachments[$i]['attachment'] = base64_decode($attachments[$i]  
               ['attachment']);
          }
          /* 3 = BASE64 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);
    
      ?>
    

提交回复
热议问题