-
Notifications
You must be signed in to change notification settings - Fork 42
/
mailDownload.py
38 lines (33 loc) · 1.22 KB
/
mailDownload.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
# -*- coding: utf-8 -*-
import imaplib
import email
def open_connection(host, user, password):
print 'Connecting to ', host
connection = imaplib.IMAP4_SSL(host)
print 'Login as ', user
connection.login(user, password)
return connection
if __name__ == '__main__':
c = open_connection('imap.gmail.com', 'mailaddress', 'password')
print c.list()
try:
c.select('INBOX', readonly=True)
type, msg_ids = c.search(None, 'ALL')
print type, msg_ids
for num in msg_ids[0].split(' '):
typ, msg_data = c.fetch(num, 'RFC822')
for response_part in msg_data:
if isinstance(response_part, tuple):
msg = email.message_from_string(response_part[1])
for part in msg.walk():
if part.get_content_type() == 'text/plain':
f = open("./username/" + num + r".txt", 'w')
f.write(part.get_payload(decode=True))
f.close()
print num
finally:
try:
c.close()
except:
pass
c.logout()