Files
imap_recursor/imap_recursor.py

119 lines
3.2 KiB
Python
Raw Permalink Normal View History

2018-05-04 21:23:43 +02:00
#!/usr/bin/env python
#
2018-05-04 22:44:01 +02:00
'''
File name: imap_recursor.py
License: GNU LESSER GENERAL PUBLIC LICENSE
Author: Kevin Chollet
Date created: 04/05/2018
Date last modified: 04/05/2018
Python Version: 3.2.3
Sources : sample used : http://www.voidynullness.net/blog/2013/07/25/gmail-email-with-python-via-imap/
'''
2018-05-04 21:23:43 +02:00
import sys
import imaplib
import email
2018-05-04 21:31:32 +02:00
import argparse
2018-05-04 22:44:01 +02:00
import subprocess
2018-05-04 21:23:43 +02:00
2018-05-04 23:11:35 +02:00
def process_mailbox(imap_ressource, command, delete="false", mar="false"):
if mar == "true" :
# If MarkAsRead is setted, we will manage only the unread messages
result, data = imap_ressource.search(None, "ALL", "(UNSEEN)")
else :
result, data = imap_ressource.search(None, "ALL")
if result != '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():
2018-05-04 23:11:35 +02:00
result, data = imap_ressource.fetch(num, '(RFC822)')
if result != '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 23:11:35 +02:00
# Launching subprocess
2018-05-04 22:44:01 +02:00
proc = subprocess.Popen(command.split(' '), stdin=subprocess.PIPE)
proc.stdin.write(data[0][1])
2018-05-04 23:11:35 +02:00
proc.communicate()
if proc.returncode == 0:
if mar == "true" :
imap_ressource.store(num, '+FLAGS', '\Seen')
if delete == "true" :
imap_ressource.store(num, '+FLAGS', '\Deleted')
imap_ressource.expunge()
2018-05-04 22:44:01 +02:00
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 22:44:01 +02:00
parser.add_argument('-e', '--exec',
action="store", dest="execute",
help="Command to execute" )
2018-05-04 23:11:35 +02:00
parser.add_argument('-d', '--delete',
action="store", dest="delete",
help="delete email after processing", default="false", choices=['false', 'true'])
parser.add_argument('-r', '--markasread',
action="store", dest="mar",
help="mark email as readen after processing", default="true", choices=['false', 'true'])
2018-05-04 21:57:50 +02:00
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 23:11:35 +02:00
process_mailbox(imap_ressource, options.execute, mar = options.mar, delete=options.delete)
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()