-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimap_recursor.py
More file actions
executable file
·118 lines (89 loc) · 3.24 KB
/
Copy pathimap_recursor.py
File metadata and controls
executable file
·118 lines (89 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python
#
'''
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/
'''
import sys
import imaplib
import email
import argparse
import subprocess
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':
print("No messages found!")
return
for num in data[0].split():
result, data = imap_ressource.fetch(num, '(RFC822)')
if result != 'OK':
print("ERROR getting message", num)
return
# Launching subprocess
proc = subprocess.Popen(command.split(' '), stdin=subprocess.PIPE)
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()
#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)
parser.add_argument('-S', '--ssl',
action="store", dest="ssl",
help="using ssl or not (imaps vs imap)", default="true", choices=['false', 'true'])
parser.add_argument('-f', '--folder',
action="store", dest="folder",
help="Folder to explore", default="INBOX")
parser.add_argument('-e', '--exec',
action="store", dest="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()
if options.ssl == "true" :
imap_ressource = imaplib.IMAP4_SSL(options.server)
else :
imap_ressource = imaplib.IMAP4(options.server)
try:
result, data = imap_ressource.login(options.username, options.password)
except imaplib.IMAP4.error:
print("LOGIN FAILED!!! ")
sys.exit(1)
print(result, data)
result, mailboxes = imap_ressource.list()
if result == 'OK':
print("Mailboxes:")
print(mailboxes)
result, data = imap_ressource.select(options.folder)
if result == 'OK':
print("Processing mailbox...\n")
process_mailbox(imap_ressource, options.execute, mar = options.mar, delete=options.delete)
imap_ressource.close()
else:
print("ERROR: Unable to open mailbox ", rv)
imap_ressource.logout()