2024-05-20 17:33:19 发布
网友
我一直在试图找到如何从一个gmail帐户中检索python或PHP附件的信息,我希望这里有人能提供一些帮助,谢谢。
相关:
您必须启用对GMail帐户的IMAP访问(设置→转发和POP/IMAP),然后使用imaplib.IMAP4_SSL来访问它。
imaplib.IMAP4_SSL
使用每条消息的原始文本作为email.message_from_string的参数,以便处理任何附件。
email.message_from_string
我找到了一些有用的代码!这将把所有附件下载到选定的文件夹
<?php $gmail_username = 'username@gmail.com'; $gmail_password = 'mypassword'; $imap = imap_open ("{imap.gmail.com:993/imap/ssl}INBOX", $gmail_username, $gmail_password) or die("can't connect: " . imap_last_error()); $savefilepath = 'path/to/images_folder/'; //absolute path to images directory $imagefilepath = 'images/'; //relative path to images directory $headers = imap_headers($imap); foreach ($headers as $mail) { $flags = substr($mail, 0, 4); //Check for unread msgs, get their UID, and queue them up if (strpos($flags, "U")) { preg_match('/[0-9]+/',$mail,$match); $new_msg[] = implode('',$match); } } if ($new_msg) { foreach ($new_msg as $result) { $structure = imap_fetchstructure($imap,$result); $parts = $structure->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); } } ?>
我把上面的代码修好并测试了它。它与PHP5一起工作。
<?php $gmail_username = 'email@gmail.com'; $gmail_password = 'password'; $imap = imap_open ("{imap.gmail.com:993/imap/ssl}INBOX", $gmail_username, $gmail_password) or die("can't connect: " . imap_last_error()); $savefilepath = '//Server/share/Local/Pathname/'; //absolute path to images directory $imagefilepath = '/Local/Pathname/'; //relative path to images directory $savethumbpath = '/Local/Pathname/'; //relative path to images directory $headers = imap_headers($imap); foreach ($headers as $mail) { $flags = substr($mail, 0, 4); //Check for unread msgs, get their UID, and queue them up if (strpos($flags, "U")) { preg_match('/[0-9]+/',$mail,$match); $new_msg[] = implode('',$match); } } if ($new_msg) { foreach ($new_msg as $result) { $structure = imap_fetchstructure($imap,$result); $parts = $structure->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") . '_' . rand() . '_' . $part->parameters[0]->value; save_attachment(imap_fetchbody($imap,$result,2),$savefilename,$savefilepath,$savethumbpath); imap_fetchbody($imap,$result,2); //This marks message as read } } } } /* grab emails */ $emails = imap_search($imap,'ALL'); /* if emails are returned, cycle through each... */ if($emails) { /* put the newest emails on top */ $total = imap_num_msg($imap); /* for every email... */ for( $i = $total; $i >= 1; $i--) { $headers = imap_header($imap, $i); $from = $headers->from[0]->mailbox . "@" . $headers->from[0]->host; echo $from . "\n"; imap_delete($imap,$i); imap_mail_move($imap,"$i:$i", "[Gmail]/Trash"); // Change or remove this line if you are not connecting to gmail. The path to the Trash folder in your Gmail may be different, capitalization is relevant. } }else{ echo "no emails"; } /* close the connection */ imap_expunge($imap); 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); } } ?>
您必须启用对GMail帐户的IMAP访问(设置→转发和POP/IMAP),然后使用
imaplib.IMAP4_SSL
来访问它。使用每条消息的原始文本作为
email.message_from_string
的参数,以便处理任何附件。我找到了一些有用的代码!这将把所有附件下载到选定的文件夹
我把上面的代码修好并测试了它。它与PHP5一起工作。
相关问题 更多 >
编程相关推荐