-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreadmail.py
64 lines (47 loc) · 1.58 KB
/
greadmail.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import imaplib, email, os
user = 'andersonaathomas@gmail.com'
password = 'REDACTED'
imap_url = 'imap.gmail.com'
attachment_dir ='C:\\Users\\REDACTED\\Desktop\\attachdir'
def auth(user,password,imap_url):
con = imaplib.IMAP4_SSL(imap_url)
con.login(user,password)
return con
con = auth(user,password,imap_url)
con.select("INBOX")
x = 10 #picks 10 emails, change for more.
result, data = con.fetch(f"{x}",'(RFC822)')
def get_body(msg):
if msg.is_multipart():
return get_body(msg.get_payload(0))
return msg.get_payload(None,True)
def search(key,value,con):
result, data = con.search(None,key,'"{}"'.format(value))
return data
def get_emails(result_bytes):
msgs = []
for num in result_bytes[0].split():
typ, data = con.fetch(num,'(RFC822)')
msgs.append(data)
return msgs
def get_attachments(msg):
for part in msg.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get("Content-Disposition") is None:
continue
fileName = part.get_filename()
if bool(fileName):
filePath = os.path.join(attachment_dir, fileName)
with open(filePath,'wb') as f:
f.write(part.get_payload(decode=True))
raw = email.message_from_bytes(data[0][-1])
mylist = list(str(get_body(raw)))
for x in range(len(get_body(raw))):
if x % 130 == 0:
mylist.insert(x,"\n")
print(''.join(mylist))
for msg in get_emails(search("FROM",'REDACTED@gmail.com',con)):
pass
# print("\n")
#print(get_body(email.message_from_bytes(msg[0][1])))