-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmailclient_1_1.py
355 lines (328 loc) · 9.46 KB
/
mailclient_1_1.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#coding=utf-8
import poplib
import email
import sys
import os
import getpass
from email.parser import Parser
from email.header import decode_header
from email.utils import parseaddr
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
print "Welcome To MailClient"
#Chose sendOrReceive
def sendOrReceive():
workType = raw_input("Please Chose Work Type:[send/receive]\n")
if workType.lower().startswith("send"):
print "You Chose Send"
return 1
elif workType.lower().startswith("receive"):
print "You Chose Receive"
return 2
else:
print "Your Input Is Wrong,Please Input Aganin"
workType = sendOrReceive()
return workType
#Send Information
def sendInfo():
while True:
email_host = raw_input("Please Input Your Email_host:\n")
if email_host.lower().startswith("qq"):
email_host = "smtp.qq.com"
elif email_host.lower().startswith("163"):
email_host = "smtp.163.com"
else:
pass
if connSendServer(email_host):
break
while True:
from_email = raw_input("Please Input From_email:\n")
password = getpass.getpass("Please Input Your Password:\n")
smtpObj = smtplib.SMTP_SSL(email_host)
if sendLogin(smtpObj,from_email,password):
break
to_email = raw_input("Please Input To_email:\n")
return (email_host,from_email,to_email,password)
#Receive Information
def receiveInfo():
while True:
email_host = raw_input("Please Input Your Email_host:\n")
if email_host.lower().startswith("qq"):
email_host = "pop.qq.com"
elif email_host.lower().startswith("163"):
email_host = "pop.163.com"
else:
pass
if connReceServer(email_host):
break
while True:
email = raw_input("Please Input Your Email:\n")
password = getpass.getpass("Please Input Your Password:\n")
popObj = poplib.POP3_SSL(email_host)
if receLogin(popObj,email,password):
break
return (email_host,email,password)
#Connect Receive Server
def connReceServer(email_host):
try:
try:
popObj = poplib.POP3_SSL(email_host)
except:
popObj = poplib.POP3(email_host)
print popObj.getwelcome()+"\n"
return 1
except:
print "There is something wrong with your email_host,Please Try Aganin"
return 0
#Connect Send Server
def connSendServer(email_host):
try:
smtpObj = smtplib.SMTP_SSL(email_host)
return 1
except:
print "There is something wrong with your email_host,Please Try Aganin"
return 0
#send login
def sendLogin(smtpObj,from_email,password):
try:
smtpObj.login(from_email,password)
return 1
except:
print "Your Username Or Password Is Wrong,Please Try Aganin"
return 0
#receive login
def receLogin(popObj,email,password):
try:
popObj.user(email)
popObj.pass_(password)
return 1
except:
print "Your Username Or Password Is Wrong,Please Try Aganin"
return 0
#chose send type
def sendType():
sendtype = raw_input("Please Input Your Send Type:[plain/html/multi]\n")
if sendtype.lower().startswith("plain"):
return 1
elif sendtype.lower().startswith("html"):
return 2
elif sendtype.lower().startswith("multi"):
return 3
else:
print "Your Chose Is Wrong,Please Try Aganin"
return 0
#Send Content
def sendMessage():
subject = raw_input("Please Input Your Email Subject\n")
content = raw_input("Please Input Your Email Content:\n")
return (subject,content)
#show mail attachment
def showAttachment(msg):
maintype=msg.get_content_type()
if maintype.lower().startswith('multipart'):
for part in msg.get_payload():
showAttachment(part)
elif maintype.lower().endswith("base64"):
if not msg["Content-Disposition"]:
pass
else:
print "-"*50
print "This mail has an Attachment"
filename = msg["Content-Disposition"].split("\"")[-2]
print "File Name: "+filename
print ""
else:
pass
#download attachment
def downloadAttachment(msg):
maintype=msg.get_content_type()
if maintype.lower().startswith('multipart'):
for part in msg.get_payload():
downloadAttachment(part)
elif maintype.lower().endswith("base64"):
if not msg["Content-Disposition"]:
pass
else:
download = raw_input("Do You Want To Download The Attachment?[yes|no]\n")
if download.lower().startswith("no"):
print "OK,Do Not Download"
pass
else:
filename = msg["Content-Disposition"].split("\"")[-2]
downloadfile = open(filename,"wb")
downloadfile.write(msg.get_payload(decode=True))
downloadfile.close()
print "Download Successful"
#show mail subject
def showSubject(msg):
try:
subdecode = decode_header(msg["Subject"])[0][1]
if not subdecode==None:
print decode_header(msg["Subject"])[0][0].decode(decode_header(msg["Subject"])[0][1]).encode("utf-8")
else:
print unicode(decode_header(msg["Subject"])[0][0],"utf-8")
print ""
except:
print ""
pass
#show mail from and to
def showMoreInfo(msg):
try:
from_email_decode = decode_header(msg["from"])[0][1]
if not from_email_decode==None:
print "From: " + decode_header(msg["from"])[0][0].decode(decode_header(msg["from"])[0][1]).encode("utf-8")
else:
print "From: " + unicode(decode_header(msg["from"])[0][0],"utf-8")
to_email_decode = decode_header(msg["to"])[0][1]
if not to_email_decode==None:
print "To : " + decode_header(msg["to"])[0][0].decode(decode_header(msg["to"])[0][1]).encode("utf-8")
else:
print "To : " + unicode(decode_header(msg["to"])[0][0],"utf-8")
except:
print ""
pass
#show mail content
def showContent(msg):
contentType = msg.get_content_type()
if contentType.lower().startswith("multipart"):
for i in msg.get_payload():
showContent(i)
elif contentType.lower().endswith("base64"):
pass
else:
print msg.get_payload(decode=True)
#delete single mail
def deleteMail(popObj):
try:
deletenum = input("Please Input The Number Mail You Want To Delete: \n")
except:
print "Your Input Is Wrong"
return
index = int(deletenum)
try:
msg.dele(index)
print "Delete Number %d Mail Successful"%index
except:
print "Delete Mail Failed"
#read single mail
def readMail(popObj):
try:
index = input("Please Input The Number Your Want To Read :\n")
except:
print "Your Input Is Wrong"
return
resp, mails, octets = popObj.list()
if index > len(mails):
print "Your Input Is Wrong"
return
print "\n\n\nThe Number %d Mail is: \n"%index
resp, lines, octets = popObj.retr(index)
msg_content = '\r\n'.join(lines)
msg = Parser().parsestr(msg_content)
showMoreInfo(msg)
print "Subject :"
showSubject(msg)
print "Content :"
showContent(msg)
showAttachment(msg)
downloadAttachment(msg)
#show more subject
def showMoreSubject(popObj):
try:
beginnum = int(input("Please Input The Number Begin:\n"))
endnum = int(input("Please Input The Number End :\n"))
except:
print "Your Input Is Wrong\n"
return
resp, mails, octets = popObj.list()
if endnum > len(mails)+1:
endnum = len(mails)+1
for index in range(beginnum,endnum):
resp, lines, octets = popObj.retr(index)
msg_content = '\r\n'.join(lines)
msg = Parser().parsestr(msg_content)
print "This Is No.%s Mail Subject :"%index
showSubject(msg)
showAttachment(msg)
#quit
def Quit():
print "Thanks For Your Using ."
print "You Can Report Bugs to 1106911190@qq.com"
print "PRESS ENTER TO QUIT"
raw_input()
sys.exit()
if __name__ == '__main__':
workType = sendOrReceive()
if workType == 1:
email_host,from_email,to_email,password = sendInfo()
smtpObj = smtplib.SMTP_SSL(email_host)
while True:
sendtype = sendType()
if sendtype:
break
messageSubject,messageContent = sendMessage()
if not sendtype==3:
if sendtype==1:
message = MIMEText(messageContent,_subtype='plain',_charset='utf-8')
else:
message = MIMEText(messageContent,_subtype='html',_charset='utf-8')
else:
multiName = raw_input("Please Input Your Attachment Name:\n")
message = MIMEMultipart()
attr = MIMEText(open(multiName,"rb").read(),"base64","utf-8")
attr["Content-Type"] = 'application/octet-stream'
attr["Content-Disposition"] = 'attachment; filename="%s"'%(multiName)
message.attach(attr)
content = MIMEText(messageContent,"plain","utf-8")
message.attach(content)
message["Subject"] = messageSubject
message["From"] = from_email
message["To"] = to_email
smtpObj.login(from_email,password)
try:
smtpObj.sendmail(from_email,to_email,message.as_string())
print "Sending Successful"
except:
print "Sending Failes"
smtpObj.close()
Quit()
elif workType == 2:
email_host,email,password = receiveInfo()
popObj = poplib.POP3_SSL(email_host)
popObj.user(email)
popObj.pass_(password)
status = popObj.stat()
print "MailBox has %d message for a total of %s bytes"%(status[0],status[1])
resp, mails, octets = popObj.list()
for index in range(len(mails)-10,len(mails)+1):
resp, lines, octets = popObj.retr(index)
msg_content = '\r\n'.join(lines)
msg = Parser().parsestr(msg_content)
print "This Is No.%s Mail Subject :"%index
showSubject(msg)
showAttachment(msg)
print "\n\n\nThe Lastest Mail is: \n"
index = len(mails)
resp, lines, octets = popObj.retr(index)
msg_content = '\r\n'.join(lines)
msg = Parser().parsestr(msg_content)
showMoreInfo(msg)
print "Subject :"
showSubject(msg)
print "Content :"
showContent(msg)
showAttachment(msg)
downloadAttachment(msg)
while True:
continueType = raw_input("Please Input The Next You Want To Do:[read|delete|quit|more]\n")
if continueType.lower().startswith("read"):
readMail(popObj)
elif continueType.lower().startswith("delete"):
deleteMail(popObj)
elif continueType.lower().startswith("more"):
showMoreSubject(popObj)
else:
break
popObj.quit()
Quit()