-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
79 lines (69 loc) · 2.93 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
69
70
71
72
73
74
75
76
77
78
79
import imaplib
import email
import time
import requests
from email.header import make_header, decode_header
"""
Leave one email with given subject and do not remove it! Leave it unread.
"""
class Mailer:
def __init__(self):
self.response = None
self.messages = None
self.last_subject = "DO NOT REMOVE"
self.username = "username"
self.password = "password"
self.webhook = "webhook_url"
self.bot_username = "Username"
try:
self.imap = imaplib.IMAP4_SSL("imap.gmail.com")
self.result = self.imap.login(self.username, self.password)
except Exception as e:
print(f"Exception occurred: {e}")
time.sleep(2)
try:
payload = {'username': {self.bot_username}, "content": f"**ERROR : ** {e}"}
requests.post(self.webhook, data=payload)
except Exception as e:
exit(f"Exception occurred: {e}")
def checkMail(self):
self.imap.select('Inbox', True)
self.response, self.messages = self.imap.search(None, "UnSeen")
self.messages = self.messages[0].split()
self.messages = list(map(int, self.messages))
return self.messages[-1]
def sendInfo(self):
count = 0
while True:
try:
latest_email_number = self.checkMail()
except Exception as e:
print(f"Exception occurred: {e}")
time.sleep(2)
try:
payload = {'username': {self.bot_username}, "content": f"**ERROR : ** {e}"}
requests.post(self.webhook, data=payload)
except Exception as e:
exit(f"Exception occurred: {e}")
res, msg = self.imap.fetch(str(latest_email_number - count), "(RFC822)")
for response in msg:
if isinstance(response, tuple):
msg = email.message_from_bytes(response[1])
if msg["Subject"] == self.last_subject:
print(self.last_subject + " is the last new email. Finishing now")
return
else:
subject = make_header(decode_header(msg["Subject"]))
sender = str(make_header(decode_header(msg["From"]))).replace('"', '')
print(f'FROM: {sender} \t SUBJECT: {subject}'.expandtabs(70))
content = f"**FROM: {sender}** \nSUBJECT: {subject}"
payload = {'username': {self.bot_username}, "content": {content}}
time.sleep(2)
try:
requests.post(self.webhook, data=payload)
except Exception as e:
print(f"Exception occurred: {e}")
pass
count = count + 1
notify = Mailer()
notify.sendInfo()