-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmachine_email.py
64 lines (55 loc) · 2.04 KB
/
machine_email.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
from settings import Settings
from transactions import make_transaction
s = Settings()
mail = imaplib.IMAP4_SSL(s.imap_server)
mail.login(s.email_address, s.email_password)
run = True
refresh_rate = 5
def parse_header(data):
data = data.replace('\r\n ', ' ')
out = [x.split(':', 1) for x in data.split('\r\n') if x]
return dict(out)
# write a function to check all the emails in the inbox and print them
def check_inbox():
mail.select('inbox')
_, ids = mail.search(None, '(UNSEEN)')
ids = ids[0].split()
for id in ids:
_, data = mail.fetch(id, '(BODY[HEADER])')
data = data[0][1].decode('utf-8')
h = parse_header(data)
#print(h)
print(h['Subject'])
print(h['From'])
print(h['Date'])
print("-----------------------------------------------------")
#print(h['Message-ID'])
#print(h['In-Reply-To'])
#print(h['References'])
#print(h['Content-Type'])
#print(h['Mime-Version'])
#print(h['Content-Transfer-Encoding'])
def check_venmos():
mail.select('inbox')
# _, ids = mail.search(None, '(;[p-? \\|OM [email protected] FROM [email protected] '
# f'SUBJECT "paid you" BODY "{s.user_phrase}" UNSEEN)')
# this formatted as an IMAP search command
_, ids = mail.search(None, '(OR FROM [email protected] OR FROM [email protected] FROM [email protected] '
f'SUBJECT "paid you" UNSEEN)')
ids = ids[0].split()
venmos = []
for id in ids:
_, data = mail.fetch(id, '(BODY[HEADER])')
data = data[0][1].decode('utf-8')
h = parse_header(data)
actor, amount = h['Subject'].split(' paid you $')
actor = actor.replace('Fwd: ', '').strip()
print(actor, amount)
venmos.append(make_transaction(actor.strip(), float(amount)))
return venmos
if __name__ == '__main__':
print("Venmo Payments:")
check_venmos()
print("-------------------ALL EMAIL-----------------------------")
check_inbox()