-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·68 lines (58 loc) · 1.83 KB
/
main.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
#!/usr/bin/python3
import sys
import imaplib
from imapclient import IMAPClient
from socket import gethostbyname, gaierror, create_connection
import email
from email import policy
from email.parser import BytesParser
## Conection
### If using gmail remember to enable insecure apps https://support.google.com/accounts/answer/6010255?hl=en
SERVER="imap.gmail.com"
USERNAME="[email protected]"
PASSWORD=""
MAILBOX="INBOX"
PROVIDER=""
WORD=""
try:
gethostbyname(SERVER)
#TODO: connect to port 993
except gaierror as e:
print('invalid server: '+SERVER)
print(str(e))
sys.exit()
server = IMAPClient(SERVER, use_uid=True)
try:
server.login(USERNAME,PASSWORD)
except imaplib.IMAP4.error as e:
print('invalid credentials')
print("Username: "+USERNAME)
print("Password: "+PASSWORD)
print(str(e))
server.logout()
sys.exit()
## Reading emails
### Folder select
try:
server.select_folder(MAILBOX)
except imaplib.IMAP4.error as e:
print("Unknown Mailbox: "+MAILBOX)
print(str(e))
server.logout()
sys.exit()
### Reading emails
messages = server.search(['FROM', PROVIDER])
print("%d messages found" % len(messages))
for msgid, data in server.fetch(messages, ['RFC822', 'BODY[TEXT]','ENVELOPE']).items():
envelope = data[b'ENVELOPE']
body = data[b'BODY[TEXT]']
print('ID #%d: "%s" received %s' % (msgid, envelope.subject.decode(), envelope.date))
if WORD in envelope.subject.decode():
print("email found id: "+str(msgid)+"\nmessage_id="+str(envelope.message_id))
email_message = email.message_from_bytes(data[b'RFC822'])
print(msgid, email_message.get('From'), email_message.get('Subject'))
if 'message-id' in email_message:
print('Message-ID:', email_message['message-id'])
print(body)
## TODO: extract just the HTML
server.logout()