-
Notifications
You must be signed in to change notification settings - Fork 0
/
abec.py
69 lines (54 loc) · 1.64 KB
/
abec.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
69
import os
import imaplib
from models.account import Account
from models.models import Envelope, MessageData
import imap.fetch as fetch
email_address = os.environ.get('EMAIL')
password = os.environ.get('PASSWORD')
def first_time_set_up(ans):
if ans:
return True
return False
def initalise_account():
return Account(email_address, email_address, password)
def create_envelope(mail):
frm = mail.get('from')
subject = mail.get('subject')
date = mail.get('date')
reply_to = mail.get('reply')
to = mail.get('to')
cc = mail.get('cc')
bcc = mail.get('bcc')
mail_id = mail.get('message-id')
envelope = Envelope(
to, frm, subject, date, reply_to, cc, bcc, mail_id
)
return envelope
def create_body(mail):
body = mail.get('text')
return body
def create_message_body(mail):
body = mail.get('plain')
message_body = MessageData(body)
return message_body
def access_account(account):
try:
with imaplib.IMAP4_SSL('imap.googlemail.com') as M:
M.login(account.email_address, account.password)
M.select('INBOX')
typ, data = M.search(None, 'ALL')
for mail in data[0].split():
headers = fetch.get_headers(M, mail)
body = fetch.get_body(M, mail)
print(headers)
print(body)
M.close()
M.logout()
except ConnectionRefusedError as cre:
print(cre)
except imaplib.IMAP4.error as e:
print(e)
if __name__ == '__main__':
if first_time_set_up(True):
account = initalise_account()
access_account(account)