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
|
|
|
|
|
|
|
|
|
|
|
|
|
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':
|
2018-05-04 21:34:02 +02:00
|
|
|
print("No messages found!")
|
2018-05-04 21:23:43 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
|
|
for num in data[0].split():
|
|
|
|
|
rv, data = M.fetch(num, '(RFC822)')
|
|
|
|
|
if rv != 'OK':
|
2018-05-04 21:34:02 +02:00
|
|
|
print("ERROR getting message", num)
|
2018-05-04 21:23:43 +02:00
|
|
|
return
|
|
|
|
|
|
2018-05-04 22:17:39 +02:00
|
|
|
print(data[0][1].decode("utf-8"))
|
|
|
|
|
"""msg = email.message_from_bytes(data[0][1])"""
|
2018-05-04 21:23:43 +02:00
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
2018-05-04 21:36:48 +02:00
|
|
|
parser.add_argument('-S', '--ssl',
|
|
|
|
|
action="store", dest="ssl",
|
|
|
|
|
help="using ssl or not (imaps vs imap)", default="true", choices=['false', 'true'])
|
|
|
|
|
|
2018-05-04 21:57:50 +02:00
|
|
|
parser.add_argument('-f', '--folder',
|
|
|
|
|
action="store", dest="folder",
|
|
|
|
|
help="Folder to explore", default="INBOX")
|
|
|
|
|
|
|
|
|
|
|
2018-05-04 21:31:32 +02:00
|
|
|
options = parser.parse_args()
|
|
|
|
|
|
2018-05-04 21:37:46 +02:00
|
|
|
if options.ssl == "true" :
|
2018-05-04 21:36:48 +02:00
|
|
|
imap_ressource = imaplib.IMAP4_SSL(options.server)
|
2018-05-04 21:37:46 +02:00
|
|
|
else :
|
2018-05-04 21:36:48 +02:00
|
|
|
imap_ressource = imaplib.IMAP4(options.server)
|
2018-05-04 21:23:43 +02:00
|
|
|
|
|
|
|
|
try:
|
2018-05-04 21:57:50 +02:00
|
|
|
result, data = imap_ressource.login(options.username, options.password)
|
2018-05-04 21:23:43 +02:00
|
|
|
except imaplib.IMAP4.error:
|
2018-05-04 21:34:02 +02:00
|
|
|
print("LOGIN FAILED!!! ")
|
2018-05-04 21:23:43 +02:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
2018-05-04 21:57:50 +02:00
|
|
|
print(result, data)
|
2018-05-04 21:23:43 +02:00
|
|
|
|
2018-05-04 21:57:50 +02:00
|
|
|
result, mailboxes = imap_ressource.list()
|
|
|
|
|
if result == 'OK':
|
2018-05-04 21:34:02 +02:00
|
|
|
print("Mailboxes:")
|
|
|
|
|
print(mailboxes)
|
2018-05-04 21:23:43 +02:00
|
|
|
|
2018-05-04 21:57:50 +02:00
|
|
|
result, data = imap_ressource.select(options.folder)
|
|
|
|
|
if result == 'OK':
|
2018-05-04 21:34:02 +02:00
|
|
|
print("Processing mailbox...\n")
|
2018-05-04 22:17:39 +02:00
|
|
|
process_mailbox(imap_ressource)
|
2018-05-04 21:31:32 +02:00
|
|
|
imap_ressource.close()
|
2018-05-04 21:23:43 +02:00
|
|
|
else:
|
2018-05-04 21:34:02 +02:00
|
|
|
print("ERROR: Unable to open mailbox ", rv)
|
2018-05-04 21:23:43 +02:00
|
|
|
|
2018-05-04 21:31:32 +02:00
|
|
|
imap_ressource.logout()
|