-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceivemail.py
47 lines (31 loc) · 1010 Bytes
/
receivemail.py
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
import imaplib
import functions
import re
def getMails(pwd, count=20):
SERVER, PORT, USER, PASSWORD = functions.parseCredentials(pwd=pwd)
print("Connecting to {server}".format(server=SERVER))
# connect to host using SSL
imap = imaplib.IMAP4_SSL(SERVER)
## login to server
imap.login(USER, PASSWORD)
imap.select('Inbox')
tmp, data = imap.search(None, 'ALL')
sub = re.compile("Subject:.*")
c = 0
mails = []
for num in reversed(data[0].split()):
tmp, data = imap.fetch(num, '(RFC822)')
# print('Message: {0}\n'.format(num))
context = data[0][1].decode("utf-8")
tmp_sub = sub.findall(context)
# print(context)
if tmp_sub != []:
tmp_sub = tmp_sub[-1][8:]
# print(tmp_sub)
mails.append(functions.deletSpaces(functions.parseEmailHeader(tmp_sub)))
c += 1
if c >= count:
break
# print(data[0][1])
imap.close()
return mails