-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathemail_client.py
255 lines (194 loc) · 7.1 KB
/
email_client.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
#/usr/bin/python
# -*- coding: utf-8 -*-
import re, os
import socket
import ssl
from email.mime.text import MIMEText
from email.utils import formatdate
from email.base64mime import encode as encode_base64
CRLF = "\r\n" #define crlf
class SMTPException(Exception): #define exception class
pass
class SMTPServerDisconnected(SMTPException):
pass
class SMTPResponseException(SMTPException):
def __init__(self, code, msg):
self.smtp_code = code
self.smtp_error = msg
self.args = (code, msg)
class MyEmailClient():
def __init__(self): #instantiate socket
self.file = None
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def user_input(self): #user input informations of mail
self.username = raw_input('user_id:')
self.password = raw_input('password:')
self.host = raw_input('server_host:')
self.port = raw_input('server_port:')
self.fro = raw_input('email_from:')
self.to = raw_input('email_to:')
self.sub = raw_input('subject:')
self.text_path = raw_input('the path of text:') #the text contains contents of the e-mail
def write_mail(self): #instantiate and define MIMEText
self.msg = MIMEText(self.getcontent(self.text_path), 'plain', 'utf-8')
self.msg['From'] = self.fro
self.msg['Subject'] = self.sub
self.msg['To'] = self.to
self.msg['Date'] = formatdate(localtime=True)
def getcontent(self, path): #get mail content from text
f = os.path.abspath(path)
content = open(f, 'rb')
data = content.read()
try:
data = data.decode('gbk')
except:
data = data.decode('gbk','ignore')
content.close()
return data
def quotedata(self, data): #quote mail data by regular expression
return re.sub(r'(?m)^\.', '..', re.sub(r'(?:\r\n|\n|\r(?!\n))', CRLF, data))
def putcmd(self, cmd, args=''): #socket send
if args == '':
string = '%s%s' % (cmd, CRLF)
else:
string = '%s %s%s' % (cmd, args, CRLF)
try:
self.sock.sendall(string)
except socket.error:
raise SMTPServerDisconnected('Server not connected')
def getreply(self): #socket receive
resp = []
if self.file is None:
self.file = self.sock.makefile('rb')
while 1:
try:
line = self.file.readline()
except socket.error as e:
self.close()
raise SMTPServerDisconnected("Connection unexpectedly closed: " + str(e))
if line == '':
self.close()
raise SMTPServerDisconnected("Connection unexpectedly closed")
resp.append(line[4:].strip())
code = line[:3]
try:
errcode = int(code)
except ValueError:
errcode = -1
break
if line[3:4] != '-':
break
errmsg = "\n".join(resp)
return (errcode, errmsg)
def connect(self): #socket connect
host = self.host
port = int(self.port)
addr = (host, port)
self.sock.connect(addr)
(code, msg) = self.getreply()
if not (200 <= code <=299):
raise SMTPServerDisconnected('Server not connected')
return (code, msg)
def helo(self): #send helo command
self.putcmd('helo', self.host)
(code, msg) = self.getreply()
if not (200 <= code <=299):
raise SMTPResponseException(code, resp)
return (code, msg)
def ehlo(self): #send ehlo command
self.putcmd('ehlo', self.host)
(code, msg) = self.getreply()
if code == -1 and len(msg) == 0:
self.close()
raise SMTPServerDisconnected("Server not connected")
if code != 250:
raise SMTPResponseException(code, resp)
return (code, msg)
def starttls(self): #put the connection to the SMTP server into TLS mode
self.putcmd('starttls')
(code, msg) = self.getreply()
if code != 220:
raise SMTPResponseException(code, msg)
else:
self.sock = ssl.wrap_socket(self.sock) #wrap socket
self.file = SSLFakeFile(self.sock) #ssl-readline, ssl-send
return (code, msg)
def login(self): #log in on the SMTP server
self.putcmd('auth login')
(code, msg) = self.getreply()
if code != 334:
raise SMTPResponseException(code, msg)
self.putcmd(encode_base64(self.username, eol=''))
(code, msg) = self.getreply()
if code != 334:
raise SMTPResponseException(code, msg)
self.putcmd(encode_base64(self.password, eol=''))
(code, msg) = self.getreply()
if code not in (235, 503):
# 235 == 'Authentication successful'
# 503 == 'Error: already authenticated'
raise SMTPResponseException(code, msg)
return (code, msg)
def send_mail(self): #a complete process of SMTP protocol
self.write_mail()
self.connect()
self.helo()
self.ehlo()
self.starttls()
self.ehlo()
self.login()
self.putcmd('mail', "FROM:%s" % self.msg['From']) #send mail command
(code, msg) = self.getreply()
if code != 250:
raise SMTPResponseException(code, msg)
self.putcmd('rcpt', "TO:%s" % self.msg['To']) #send rcpt command
(code, msg) = self.getreply()
if code != 250:
raise SMTPResponseException(code, msg)
self.putcmd('data') #send data command
(code, msg) = self.getreply()
if code != 354:
raise SMTPResponseException(code, msg)
else:
data = self.quotedata(self.msg.as_string())
if data[-2:] != CRLF:
data = data + CRLF
data = data + '.' + CRLF
self.sock.sendall(data)
(code, msg) = self.getreply()
if code != 250:
raise SMTPResponseException(code, msg)
print "succeed to send email"
return True
def close(self): #close socket connection
if self.file:
self.file.close()
self.file = None
if self.sock:
self.sock.close()
self.sock = None
def quit(self): #terminate the SMTP session
self.putcmd('quit')
self.close()
return True
class SSLFakeFile: #wrap a SSLObject
def __init__(self, sslobj):
self.sslobj = sslobj
def readline(self): #ssl-readline
str = ""
chr = None
while chr != "\n":
chr = self.sslobj.read(1)
if not chr:
break
str += chr
return str
def send(self, str): #ssl-send
self.sslobj.send(str)
def close(self):
pass
if __name__ == '__main__':
my_email = MyEmailClient()
my_email.user_input()
my_email.send_mail()
my_email.quit()