-
Notifications
You must be signed in to change notification settings - Fork 2
/
receive.py
68 lines (55 loc) · 2.03 KB
/
receive.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
65
66
67
68
# Importing libraries
import imaplib, email
from algo import decrypt_msg
user = '[email protected]'
password = 'password'
imap_url = 'imap.gmail.com'
# Function to get email content
def get_body(msg):
if msg.is_multipart():
return get_body(msg.get_payload(0))
else:
return msg.get_payload(None, True)
# Function to search emails
def search(key, value, con):
result, data = con.search(None, key, '"{}"'.format(value))
return data
# Function to get emails
def get_emails(result_bytes):
msgs = []
for num in result_bytes[0].split():
typ, data = con.fetch(num, '(RFC822)')
msgs.append(data)
return msgs
con = imaplib.IMAP4_SSL(imap_url)
con.login(user, password)
con.select('Inbox')
# Fetching emails from user
msgs = get_emails(search('FROM', user, con))
# Finding the required content from our msgs
for msg in msgs[::-1]:
for sent in msg:
if type(sent) is tuple:
# encoding set as utf-8
content = str(sent[1], 'utf-8')
data = str(content)
# Handling errors related to unicodenecode
try:
indexstart = data.find("ltr")
data2 = data[indexstart + 5: len(data)]
indexend = data2.find("</div>")
# printtng the required content which we need
# to extract from our email i.e our body
print("Raw email content")
print("\n-----------------------------")
print(data2[0: indexend])
print("-----------------------------")
content = data2.split("\n")[-2]
print("\nEncrypted message")
print(content)
print("\nDecrypting ...")
print("Decrypted message", u'\u2713')
print(decrypt_msg(content))
break
except UnicodeEncodeError as e:
pass