通过python或php从gmail获取附件

2024-05-20 17:33:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在试图找到如何从一个gmail帐户中检索python或PHP附件的信息,我希望这里有人能提供一些帮助,谢谢。

相关:


Tags: from信息附件downloadwith帐户allcan
3条回答

您必须启用对GMail帐户的IMAP访问(设置→转发和POP/IMAP),然后使用imaplib.IMAP4_SSL来访问它。

使用每条消息的原始文本作为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);
     }
}
?>

相关问题 更多 >