-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAttachmentsDownload.py
64 lines (46 loc) · 1.61 KB
/
AttachmentsDownload.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
import poplib
from email import parser
from email.parser import Parser
from email.header import decode_header
from email.utils import parseaddr
# Input email address password and pop3 server
email = 'xxx'
password = 'xxx'
pop3_server = 'pop.gmail.com'
# Connecting to pop3 server
server = poplib.POP3_SSL(pop3_server)
# Debug messeage
# server.set_debuglevel(1)
#print(server.getwelcome())
# Idetification
server.user(email)
server.pass_(password)
# Num of emails and space left
resp, mails, octets = server.list()
messages = [server.retr(i) for i in range(1, len(server.list()[1]) + 1)]
messages = [b'\r\n'.join(mssg[1]).decode() for mssg in messages]
messages = [Parser().parsestr(mssg) for mssg in messages]
for message in messages:
subject = message.get('Subject')
if subject == 'Report Delivery Notification FTP Report':
#if from == '[email protected]':
for part in message.walk():
fileName = part.get_filename()
# Save the attachment
if fileName:
with open(fileName, 'wb') as fEx:
data = part.get_payload(decode=True)
fEx.write(data)
print("Attachment %s has been saved" % fileName)
server.quit()
#index = len(mails)
#resp, lines, octets = server.retr(index)
# lines store every line of the email
# Return the original email
#msg_content = b'\r\n'.join(lines).decode('utf-8')
# Parser email
#msg = Parser().parsestr(msg_content)
# delete the emails
# server.dele(index)
# 关闭连接:
# server.quit()