Download Attachments

313 views
Skip to first unread message

Jonah

unread,
Feb 6, 2007, 8:38:53 AM2/6/07
to GMail Power Users
Here is a script that can download all of the attachments in all
emails given by a particular label. It requires python and libgmail.
You could modify this script to work by search term or folder with
just a quick glance at the libgmail docs.

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>

Reply all
Reply to author
Forward
Message has been deleted
0 new messages