-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailHelper.py
145 lines (127 loc) · 3.73 KB
/
mailHelper.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#_*_ coding: utf-8 _*_
import poplib
#import email
from email.parser import Parser
from email.header import decode_header
#from email.utils import parseaddr
import base64
import os
def decode_str(s):
try:#下载pdf时候,文件名不用解码
value, charset = decode_header(s)[0]
if charset:
value = value.decode(charset)
except Exception:
return s
return value
def guess_charset(msg):
charset = msg.get_charset()
if charset is None:
#如果获取不到,再从Content-Type字段获取:
content_type = msg.get('Content-Type', '').lower()
pos = content_type.find('charset=')
if pos >= 0:
charset = content_type[pos + 8:].strip()
return charset
#indent用于缩进显示
def download_attachment(msg, indent = 0):
#先在当前目录创建保存本次作业的子目录
if job_number:
if os.path.exists(job_number) == False:
os.mkdir(job_number)
else:
if os.path.exists("temp") == False:
os.mkdir("temp")
#若indent不等于0,则表示该部分不包含邮件头
if indent == 0:
subject = msg.get("Subject", "")
if subject:
subject = decode_str(subject)
print "Subject: ", subject
#提取姓名和学号
if "-" in subject:
subject = subject.split('-')
else:
subject = subject.split('_')
if 3 != len(subject): #邮件主题格式不对就返回
return
times, s_number, s_name = subject
if job_number:
if times != job_number: #只下载指定的作业
return
if student_number:
if s_number != student_number: #只下载指定的作业
return
if(msg.is_multipart()):
#如果邮件对象是一个MIMEMultipart,
#get_payload()返回list,包含所有的子对象
parts = msg.get_payload()
for n, part in enumerate(parts):
#递归打印每一个子对象:
download_attachment(part, indent + 1)
else:
#邮件对象不是一个MIMEMUltipart,就依据content_type判断
content_type = msg.get_content_type()
#处理附件
if content_type not in ["text/plain", "text/html"]:
#print('%sAttachment: %s' % (' ' * indent, content_type))
filename = decode_str(msg.get_filename())
#filename = msg.get_filename()
print filename
if job_number:
f = open("%s/%s" % (job_number, filename), 'wb')
else:
f = open("temp/%s" % filename, 'wb')
f.write(base64.decodestring(msg.get_payload()))
f.close()
def analysis_mail(select):
global job_number
global student_number
job_number = ''
student_number = ''
if select == "1":
job_number = raw_input("please input the job number: ")
if select == "2":
student_number = raw_input("please input the student number: ")
email = 'your_email'
#这里输入的是授权密码,不是登陆密码
password = 'you_pass'
pop3_server = 'pop.126.com'
#connect to pop3 server
server = poplib.POP3(pop3_server)
#身份认证
server.user(email)
server.pass_(password)
#list()返回所有邮件的编号
resp, mails, octets = server.list()
#获取最新的一封邮件, 注意索引号从1开始
total_mail = len(mails)
while total_mail > 0:
#if total_mail !=4:
# total_mail = total_mail - 1
# continue
print total_mail
resp, lines, octets = server.retr(total_mail)
#lines存储了邮件的原始文本的每一行,可以获得整个邮件的原始文本
msg_content = '\r\n'.join(lines)
#稍后解析出邮件
msg = Parser().parsestr(msg_content)
download_attachment(msg)
total_mail = total_mail - 1
server.quit()
def print_menu():
print "Menu:"
print "1.download the whole homework by job number"
print "2.download specify homework by student number"
print "3.download all the homework"
print "4.exit"
if __name__ == "__main__":
print_menu()
select = raw_input("")
while 1:
if select == "4":
break
else:
analysis_mail(select)
print_menu()
select = raw_input("")