I have an IMAP folder somewhere containing thousands of attached files.

How to download all of these attachments?

  • A program focused on this task is preferred, not a human-facing email reading/composing program that happens to also download attachments if you click here and there
  • Command-line tool preferred
  • Free, ideally open source

2 Answers 2


You could do this with a python script, the following example would download and print the text from all messages, (courtesy of the python documents).

import getpass, imaplib

M = imaplib.IMAP4()
M.login(getpass.getuser(), getpass.getpass())
typ, data = M.search(None, 'ALL')
for num in data[0].split():
    typ, data = M.fetch(num, '(RFC822)')
    print 'Message %s\n%s\n' % (num, data[0][3])

But in your case you need to specify the attachments as in this SO answer, (second one looks to fill your needs).

  • Free
  • Command Line
  • Customisable
  • Open Source
  • Just about any platform from Raspberry Pi to a super computer cluster even OS-X or Windows

Mozilla Thunderbird has an attachment extractor addon which can be instructed to extract attachments from an entire IMAP folder at once. It also can remove attachments from e-mails after extraction to save space (which is the main reason I extract attachments).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.