Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 40233ea

Browse files
committed
Patch by Piers Lauder: make exceptions classes.
Take opportunity to add more explanatory messages to exceptions.
1 parent 1d7b0fa commit 40233ea

1 file changed

Lines changed: 13 additions & 14 deletions

File tree

Lib/smtplib.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@
3737
>>> s.getreply()
3838
(250, "Somebody OverHere <[email protected]>")
3939
>>> s.quit()
40-
41-
Bugs/TODO:
42-
- Exceptions should be classes
43-
4440
'''
4541

4642
import socket
@@ -53,10 +49,11 @@
5349
CRLF="\r\n"
5450

5551
# used for exceptions
56-
SMTPServerDisconnected="Server not connected"
57-
SMTPSenderRefused="Sender address refused"
58-
SMTPRecipientsRefused="All Recipients refused"
59-
SMTPDataError="Error transmitting message data"
52+
class SMTPException(Exception): pass
53+
class SMTPServerDisconnected(SMTPException): pass
54+
class SMTPSenderRefused(SMTPException): pass
55+
class SMTPRecipientsRefused(SMTPException): pass
56+
class SMTPDataError(SMTPException): pass
6057

6158
def quoteaddr(addr):
6259
"""Quote a subset of the email addresses defined by RFC 821.
@@ -171,9 +168,9 @@ def send(self, str):
171168
try:
172169
self.sock.send(str)
173170
except socket.error:
174-
raise SMTPServerDisconnected
171+
raise SMTPServerDisconnected('Server not connected')
175172
else:
176-
raise SMTPServerDisconnected
173+
raise SMTPServerDisconnected('please run connect() first')
177174

178175
def putcmd(self, cmd, args=""):
179176
"""Send a command to the server."""
@@ -245,7 +242,7 @@ def ehlo(self, name=''):
245242
# MTA's will disconnect on an ehlo. Toss an exception if
246243
# that happens -ddm
247244
if code == -1 and len(msg) == 0:
248-
raise SMTPServerDisconnected
245+
raise SMTPServerDisconnected("Server not connected")
249246
self.ehlo_resp=msg
250247
if code<>250:
251248
return code
@@ -388,7 +385,7 @@ def sendmail(self, from_addr, to_addrs, msg, mail_options=[],
388385
(code,resp) = self.mail(from_addr, esmtp_opts)
389386
if code <> 250:
390387
self.rset()
391-
raise SMTPSenderRefused
388+
raise SMTPSenderRefused('%s: %s' % (from_addr, resp))
392389
senderrs={}
393390
if type(to_addrs) == types.StringType:
394391
to_addrs = [to_addrs]
@@ -399,11 +396,13 @@ def sendmail(self, from_addr, to_addrs, msg, mail_options=[],
399396
if len(senderrs)==len(to_addrs):
400397
# the server refused all our recipients
401398
self.rset()
402-
raise SMTPRecipientsRefused
399+
raise SMTPRecipientsRefused(string.join(
400+
map(lambda x:"%s: %s" % (x[0], x[1][1]), senderrs.items()),
401+
'; '))
403402
code=self.data(msg)
404403
if code <>250 :
405404
self.rset()
406-
raise SMTPDataError
405+
raise SMTPDataError('data transmission error: %s' % code)
407406
#if we got here then somebody got our mail
408407
return senderrs
409408

0 commit comments

Comments
 (0)