Skip to content

Instantly share code, notes, and snippets.

@robulouski
Last active April 10, 2024 12:58
Show Gist options
  • Save robulouski/7442321 to your computer and use it in GitHub Desktop.
Save robulouski/7442321 to your computer and use it in GitHub Desktop.
Very simple Python script to dump all emails in an IMAP folder to files.
#!/usr/bin/env python
#
# Very simple Python script to dump all emails in an IMAP folder to files.
# This code is released into the public domain.
#
# RKI Nov 2013
#
import sys
import imaplib
import getpass
IMAP_SERVER = 'imap.gmail.com'
EMAIL_ACCOUNT = "[email protected]"
EMAIL_FOLDER = "Top Secret/PRISM Documents"
OUTPUT_DIRECTORY = 'C:/src/tmp'
PASSWORD = getpass.getpass()
def process_mailbox(M):
"""
Dump all emails in the folder to files in output directory.
"""
rv, data = M.search(None, "ALL")
if rv != 'OK':
print "No messages found!"
return
for num in data[0].split():
rv, data = M.fetch(num, '(RFC822)')
if rv != 'OK':
print "ERROR getting message", num
return
print "Writing message ", num
f = open('%s/%s.eml' %(OUTPUT_DIRECTORY, num), 'wb')
f.write(data[0][1])
f.close()
def main():
M = imaplib.IMAP4_SSL(IMAP_SERVER)
M.login(EMAIL_ACCOUNT, PASSWORD)
rv, data = M.select(EMAIL_FOLDER)
if rv == 'OK':
print "Processing mailbox: ", EMAIL_FOLDER
process_mailbox(M)
M.close()
else:
print "ERROR: Unable to open mailbox ", rv
M.logout()
if __name__ == "__main__":
main()
@paloha
Copy link

paloha commented Oct 1, 2020

I have updated it to Python3 and extended it to support an UPLOAD mode.
Check https://gist.github.com/paloha/a5dc2843a342b1f8b814c92a098ca2a2

@kingsalman99
Copy link

I have updated it to Python3 and extended it to support an UPLOAD mode.
Check https://gist.github.com/paloha/a5dc2843a342b1f8b814c92a098ca2a2

can u give me tutorial for use ur script? i got error because

@paloha
Copy link

paloha commented Dec 22, 2020

@kingsalman99 what tutorial would you like to have? You just need to create a tmp folder, set your credentials at the beginning of the script and run it with python. What error did you get? Also be sure you refer to your IMAP folders correctly. I had a structure INBOX.FolderToBeDownloaded or INBOX.FolderToUpload, this might be different in your case. You should be able to list the folders with imaplib. This answer might help: https://stackoverflow.com/questions/44230855/python-imaplib-selecting-folders

@kingsalman99
Copy link

solved thanks

@julkhami
Copy link

How do you get the emails out of the folder? I can specify a folder with "M.select('folder-name')" but I don't know how to iterate over emails in that folder. Thanks very much

@ynikitenko
Copy link

@julkhami - you do it with search. See line 25:

rv, data = M.search(None, "ALL")

@linuxmail
Copy link

hi,

I had issues with Python3 and I've got help :-) https://gist.github.com/KevinHonka/616d723dbc88cb466470b3f3cf79dca0

@linuxmail
Copy link

@robulouski thank you very much for the script !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment