-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailbot.py
58 lines (41 loc) · 1.39 KB
/
mailbot.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
# import email package
import poplib
# import time package for waiting
import time
# import package that extracts urls from strings
# !!!!!!!!!!!IMPORTANT!!!!!!!!!!!!!
# type 'pip install urlextract' to install urlextract
from urlextract import URLExtract
# server, email, password, check frequency
def connectAndGetLinks(emailServer, emailUser, emailPassword, interval):
# Connect to email server
server = poplib.POP3(emailServer)
server.user(emailUser)
server.pass_(emailPassword)
# Remember mail count before new email
mail_count = server.stat()[0]
server.quit()
while True:
print(".")
# Wait certain amount of time to check new mail
time.sleep(interval)
# Connect to email server again
server = poplib.POP3(emailServer)
server.user(emailUser)
server.pass_(emailPassword)
# Get newest email count
new_mail_count = server.stat()[0]
# If new email is found, break out of loop
if new_mail_count - mail_count >= 1:
break
# Saves the email as string into whole_email variable
whole_email = ""
for j in server.retr(new_mail_count)[1]:
whole_email += str(j)
# Extract all the urls found in the string
extractor = URLExtract()
urls = extractor.find_urls(whole_email)
# Quit email server
server.quit()
# Return array of urls
return urls