2018-05-04 21:23:43 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
#
|
|
|
|
|
# Very basic example of using Python and IMAP to iterate over emails in a
|
|
|
|
|
# gmail folder/label. This code is released into the public domain.
|
|
|
|
|
#
|
|
|
|
|
# http://www.voidynullness.net/blog/2013/07/25/gmail-email-with-python-via-imap/
|
|
|
|
|
#
|
|
|
|
|
import sys
|
|
|
|
|
import imaplib
|
|
|
|
|
import getpass
|
|
|
|
|
import email
|
|
|
|
|
import email.header
|
|
|
|
|
import datetime
|
2018-05-04 21:31:32 +02:00
|
|
|
import argparse
|
2018-05-04 21:23:43 +02:00
|
|
|
|
|
|
|
|
EMAIL_ACCOUNT = "notatallawhistleblowerIswear@gmail.com"
|
|
|
|
|
EMAIL_FOLDER = "Top Secret/PRISM Documents"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def process_mailbox(M):
|
|
|
|
|
"""
|
2018-05-04 21:26:27 +02:00
|
|
|
Do something with emails messages in the folder.
|
2018-05-04 21:23:43 +02:00
|
|
|
For the sake of this example, print some headers.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
msg = email.message_from_string(data[0][1])
|
|
|
|
|
decode = email.header.decode_header(msg['Subject'])[0]
|
|
|
|
|
subject = unicode(decode[0])
|
|
|
|
|
print 'Message %s: %s' % (num, subject)
|
|
|
|
|
print 'Raw Date:', msg['Date']
|
|
|
|
|
# Now convert to local date-time
|
|
|
|
|
date_tuple = email.utils.parsedate_tz(msg['Date'])
|
|
|
|
|
if date_tuple:
|
|
|
|
|
local_date = datetime.datetime.fromtimestamp(
|
|
|
|
|
email.utils.mktime_tz(date_tuple))
|
|
|
|
|
print "Local Date:", \
|
|
|
|
|
local_date.strftime("%a, %d %b %Y %H:%M:%S")
|
|
|
|
|
|
|
|
|
|
|
2018-05-04 21:31:32 +02:00
|
|
|
#Parsing arguments
|
|
|
|
|
parser = argparse.ArgumentParser(description='Recurse over each email of imap directory')
|
|
|
|
|
|
|
|
|
|
parser.add_argument('-u', '--username',
|
|
|
|
|
action="store", dest="username",
|
|
|
|
|
help="Username", required=True)
|
|
|
|
|
|
|
|
|
|
parser.add_argument('-p', '--password',
|
|
|
|
|
action="store", dest="password",
|
|
|
|
|
help="Password", required=True)
|
|
|
|
|
|
|
|
|
|
parser.add_argument('-s', '--server',
|
|
|
|
|
action="store", dest="server",
|
|
|
|
|
help="Imap server", required=True)
|
|
|
|
|
|
|
|
|
|
options = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
imap_ressource = imaplib.IMAP4_SSL(options.server)
|
2018-05-04 21:23:43 +02:00
|
|
|
|
|
|
|
|
try:
|
2018-05-04 21:31:32 +02:00
|
|
|
rv, data = imap_ressource.login(options.username, options.password)
|
2018-05-04 21:23:43 +02:00
|
|
|
except imaplib.IMAP4.error:
|
|
|
|
|
print "LOGIN FAILED!!! "
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
print rv, data
|
|
|
|
|
|
2018-05-04 21:31:32 +02:00
|
|
|
rv, mailboxes = imap_ressource.list()
|
2018-05-04 21:23:43 +02:00
|
|
|
if rv == 'OK':
|
|
|
|
|
print "Mailboxes:"
|
|
|
|
|
print mailboxes
|
|
|
|
|
|
2018-05-04 21:31:32 +02:00
|
|
|
rv, data = imap_ressource.select(EMAIL_FOLDER)
|
2018-05-04 21:23:43 +02:00
|
|
|
if rv == 'OK':
|
|
|
|
|
print "Processing mailbox...\n"
|
|
|
|
|
process_mailbox(M)
|
2018-05-04 21:31:32 +02:00
|
|
|
imap_ressource.close()
|
2018-05-04 21:23:43 +02:00
|
|
|
else:
|
|
|
|
|
print "ERROR: Unable to open mailbox ", rv
|
|
|
|
|
|
2018-05-04 21:31:32 +02:00
|
|
|
imap_ressource.logout()
|