无需下载即可获取Gmail附件文件名 [英] Get the Gmail attachment filename without downloading it

查看:114
本文介绍了无需下载即可获取Gmail附件文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从Gmail帐户获取可能包含一些大型附件(大约30MB)的所有邮件。我只需要名称,而不是整个文件。我找到了一段代码来获取消息和附件的名字,但它下载了文件,然后读取它的名称:

 导入imaplib,电子邮件

#登录并选择收件箱
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('username','密码')
mail.select('inbox')

#get所有消息的uid
result,data = mail.uid('search',None,'ALL' )
uids = data [0] .split()

#读取最新消息
result,data = mail.uid('fetch',uids [-1], '(RFC822)')
m = email.message_from_string(data [0] [1])$ ​​b
$ b如果m.get_content_maintype()=='multipart':#multipart messages only $ b $如果part.get_content_maintype()=='multipart':continue
如果part.get('Content-Disposition' )是None:continue

#将附件保存在程序目录
filename中= part.get_filename()
fp = open(filename,'wb')
fp.write(part.get_payload(decode = True))
fp.close()
print'%s saved!'%filename

我必须每分钟做一次,这样我就可以不会下载数百MB的数据。我是网页脚本的新手,所以任何人都可以帮助我?我真的不需要使用imaplib,任何python lib都可以。



最好的问候

解决方案

而不是获取 RFC822 这是完整的内容,您可以指定 BODYSTRUCTURE $ b $ imaplib 产生的数据结构相当混乱,但您应该能够找到文件名,内容类型和消息的每个部分的大小,而不用下载整个东西。


I'm trying to get all the messages from a Gmail account that may contain some large attachments (about 30MB). I just need the names, not the whole files. I found a piece of code to get a message and the attachment's name, but it downloads the file and then read its name:

import imaplib, email

#log in and select the inbox
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('username', 'password')
mail.select('inbox')

#get uids of all messages
result, data = mail.uid('search', None, 'ALL') 
uids = data[0].split()

#read the lastest message
result, data = mail.uid('fetch', uids[-1], '(RFC822)')
m = email.message_from_string(data[0][1])

if m.get_content_maintype() == 'multipart': #multipart messages only
    for part in m.walk():
        #find the attachment part
        if part.get_content_maintype() == 'multipart': continue
        if part.get('Content-Disposition') is None: continue

        #save the attachment in the program directory
        filename = part.get_filename()
        fp = open(filename, 'wb')
        fp.write(part.get_payload(decode=True))
        fp.close()
        print '%s saved!' % filename

I have to do this once a minute, so I can't download hundreds of MB of data. I am a newbie into the web scripting, so could anyone help me? I don't actually need to use imaplib, any python lib will be ok for me.

Best regards

解决方案

Rather than fetch RFC822, which is the full content, you could specify BODYSTRUCTURE.

The resulting data structure from imaplib is pretty confusing, but you should be able to find the filename, content-type and sizes of each part of the message without downloading the entire thing.

这篇关于无需下载即可获取Gmail附件文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆