Google's GMail API download attachments

后端 未结 4 817
清歌不尽
清歌不尽 2021-01-05 13:37

I\'m using the PHP SDK for the new Gmail API. How do I fetch an attachment from an email?

Here is the API documentation, but in this instance, it\'s missing example

4条回答
  •  鱼传尺愫
    2021-01-05 14:12

    Try the code in this thread if it works on you.

    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";
    
    ?>
    

    For more information, check this related threads and SO question

    • Can't open downloaded attachments from Gmail API

    • Extracting Attachments From Emails With PHP

    • Gmail fetch attachment

提交回复
热议问题