It reads the label as a command line argument and drops the
attachments of associated emails into the current directory. Feel
free to enhance it as you see fit.
I use it for grading I do for an undergraduate class where I get sent
hundreds of emails a week with attachments that I have to grade.
Get libgmail here:
http://libgmail.sourceforge.net/
And python here:
http://www.python.org
<code>
#!/usr/bin/python
import libgmail, sys, email, mimetypes, os
label = sys.argv[1]
ga = libgmail.GmailAccount("us...@gmail.com", "password")
ga.login()
folder = ga.getMessagesByLabel(label)
for thread in folder:
for msg in thread:
rawmsg = email.message_from_string(msg.source)
counter = 1
for part in rawmsg.walk():
if part.get_content_maintype() == 'multipart':
continue
filename = part.get_filename()
if not filename:
ext =
mimetypes.guess_extension(part.get_type())
if not ext:
ext = '.bin'
filename = 'part-%03d%s' % (counter,
ext)
counter += 1
print filename
fp = open(os.path.join(os.curdir,filename),
'wb')
fp.write(part.get_payload(decode=True))
fp.close()
</code>