Fetching attachments from gmail via either python or php

后端 未结 5 1044
终归单人心
终归单人心 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 09:51

    I found some code that works! this will download any attachments to a selected folder

    parts;
            foreach ($parts as $part) {
                if ($part->parameters[0]->attribute == "NAME") {
                    //Generate a filename with format DATE_RANDOM#_ATTACHMENTNAME.EXT
                    $savefilename = date("m-d-Y") . '_' . mt_rand(rand(), 6) . '_' . $part->parameters[0]->value;
                    save_attachment(imap_fetchbody($imap,$result,2),$savefilename,$savefilepath,$savethumbpath);
                    imap_fetchbody($imap,$result,2); //This marks message as read
                } 
            }
        }
    }
    
    imap_close($imap);
    
    function save_attachment( $content , $filename , $localfilepath, $thumbfilepath ) {
        if (imap_base64($content) != FALSE) {   
            $file = fopen($localfilepath.$filename, 'w');
            fwrite($file, imap_base64($content));
            fclose($file);
        }
    }
    ?>
    

提交回复
热议问题