-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_email2.py
50 lines (39 loc) · 1019 Bytes
/
get_email2.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
#-*- coding: utf-8 -*-
import poplib
import cStringIO, StringIO
import rfc822
import email
import base64
import string
host = 'pop3.163.com'
password = 'demaxiya321...'
pop = poplib.POP3(host)
pop.set_debuglevel(1)
pop.user(username)
pop.pass_(password)
numMessages = len(pop.list()[1])
print 'num of numMessages', numMessages
hdr, text, octet = pop.retr(numMessages)
buf = cStringIO.StringIO()
for j in text:
print >>buf, j
buf.seek(0)
text = string.join(text, '\n')
file = StringIO.StringIO(text)
message = rfc822.Message(file)
for k, v in message.items():
if k == 'from':
print 'From: ', v
else:
pass
msg = email.message_from_file(buf)
for part in msg.walk():
contenttype = part.get_content_type()
filename = part.get_filename()
print 'contenttype: ', contenttype
print 'filename: ', filename
if filename and contenttype=='image/octet-stream':
f = open('mail%d.%s.attach' % (1, filename), 'wb')
f.write(base64.decodestring(part.get_payload()))
f.close()