Clean and ready to use
This commit is contained in:
@@ -16,27 +16,37 @@ import email
|
|||||||
import argparse
|
import argparse
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
def process_mailbox(M, command):
|
def process_mailbox(imap_ressource, command, delete="false", mar="false"):
|
||||||
"""
|
|
||||||
Do something with emails messages in the folder.
|
|
||||||
For the sake of this example, print some headers.
|
|
||||||
"""
|
|
||||||
|
|
||||||
rv, data = M.search(None, "ALL")
|
if mar == "true" :
|
||||||
if rv != 'OK':
|
# 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':
|
||||||
print("No messages found!")
|
print("No messages found!")
|
||||||
return
|
return
|
||||||
|
|
||||||
for num in data[0].split():
|
for num in data[0].split():
|
||||||
rv, data = M.fetch(num, '(RFC822)')
|
result, data = imap_ressource.fetch(num, '(RFC822)')
|
||||||
if rv != 'OK':
|
if result != 'OK':
|
||||||
print("ERROR getting message", num)
|
print("ERROR getting message", num)
|
||||||
return
|
return
|
||||||
|
|
||||||
print(data[0][1].decode("utf-8"))
|
# Launching subprocess
|
||||||
"""msg = email.message_from_bytes(data[0][1])"""
|
|
||||||
proc = subprocess.Popen(command.split(' '), stdin=subprocess.PIPE)
|
proc = subprocess.Popen(command.split(' '), stdin=subprocess.PIPE)
|
||||||
proc.stdin.write(data[0][1])
|
proc.stdin.write(data[0][1])
|
||||||
|
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()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -68,6 +78,14 @@ parser.add_argument('-e', '--exec',
|
|||||||
action="store", dest="execute",
|
action="store", dest="execute",
|
||||||
help="Command to execute" )
|
help="Command to execute" )
|
||||||
|
|
||||||
|
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'])
|
||||||
|
|
||||||
|
|
||||||
options = parser.parse_args()
|
options = parser.parse_args()
|
||||||
|
|
||||||
@@ -92,7 +110,7 @@ if result == 'OK':
|
|||||||
result, data = imap_ressource.select(options.folder)
|
result, data = imap_ressource.select(options.folder)
|
||||||
if result == 'OK':
|
if result == 'OK':
|
||||||
print("Processing mailbox...\n")
|
print("Processing mailbox...\n")
|
||||||
process_mailbox(imap_ressource,options.execute)
|
process_mailbox(imap_ressource, options.execute, mar = options.mar, delete=options.delete)
|
||||||
imap_ressource.close()
|
imap_ressource.close()
|
||||||
else:
|
else:
|
||||||
print("ERROR: Unable to open mailbox ", rv)
|
print("ERROR: Unable to open mailbox ", rv)
|
||||||
|
|||||||
Reference in New Issue
Block a user