Sez The Dragon:

Ok, I fixed the quotes, along with a bug or two.
	Also added another exception.
This commit is contained in:
Guido van Rossum 1998-01-29 17:26:45 +00:00
parent bbe323e52c
commit fc40a8316a
1 changed files with 25 additions and 24 deletions

View File

@ -35,6 +35,7 @@ SMTP_PORT = 25
CRLF="\r\n" CRLF="\r\n"
# used for exceptions # used for exceptions
SMTPServerDisconnected="Server not connected"
SMTPSenderRefused="Sender address refused" SMTPSenderRefused="Sender address refused"
SMTPRecipientsRefused="All Recipients refused" SMTPRecipientsRefused="All Recipients refused"
SMTPDataError="Error transmoitting message data" SMTPDataError="Error transmoitting message data"
@ -89,8 +90,11 @@ class SMTP:
def send(self, str): def send(self, str):
"""Send `str' to the server.""" """Send `str' to the server."""
if self.debuglevel > 0: print 'send:', `str` if self.debuglevel > 0: print 'send:', `str`
self.sock.send(str) if self.sock:
self.sock.send(str)
else:
raise SMTPServerDisconnected
def putcmd(self, cmd, args=""): def putcmd(self, cmd, args=""):
"""Send a command to the server. """Send a command to the server.
""" """
@ -104,9 +108,8 @@ class SMTP:
- server response code (e.g. '250', or such, if all goes well) - server response code (e.g. '250', or such, if all goes well)
Note: returns -1 if it can't read responce code. Note: returns -1 if it can't read responce code.
- server response string corresponding to response code - server response string corresponding to response code
(note : multiline responces converted to a single, multiline (note : multiline responces converted to a single,
string) multiline string)
""" """
resp=[] resp=[]
self.file = self.sock.makefile('rb') self.file = self.sock.makefile('rb')
@ -125,7 +128,7 @@ class SMTP:
errmsg = string.join(resp,"\n") errmsg = string.join(resp,"\n")
if self.debuglevel > 0: if self.debuglevel > 0:
print 'reply: retcode (%s); Msg: %s' % (errcode,errmsg) print 'reply: retcode (%s); Msg: %s' % (errcode,errmsg)
return errcode, errmsg return errcode, errmsg
def docmd(self, cmd, args=""): def docmd(self, cmd, args=""):
@ -148,7 +151,7 @@ class SMTP:
return code return code
def help(self): def help(self):
""" SMTP 'help' command. Returns help text from server """ """ SMTP 'help' command. Returns help text from server """
self.putcmd("help") self.putcmd("help")
(code,msg)=self.getreply() (code,msg)=self.getreply()
return msg return msg
@ -180,9 +183,9 @@ class SMTP:
# ps, I don't know why I have to do it this way... doing: # ps, I don't know why I have to do it this way... doing:
# quotepat=re.compile(r"^[.]",re.M) # quotepat=re.compile(r"^[.]",re.M)
# msg=re.sub(quotepat,"..",msg) # msg=re.sub(quotepat,"..",msg)
# should work, but it dosen't (it doubles the number of any # should work, but it dosen't (it doubles the number of any
# contiguous series of .'s at the beginning of a line, # contiguous series of .'s at the beginning of a line,
# instead of just adding one. ) #instead of just adding one. )
quotepat=re.compile(r"^[.]+",re.M) quotepat=re.compile(r"^[.]+",re.M)
def m(pat): def m(pat):
return "."+pat.group(0) return "."+pat.group(0)
@ -207,24 +210,22 @@ class SMTP:
- to_addrs : a list of addresses to send this mail to - to_addrs : a list of addresses to send this mail to
- msg : the message to send. - msg : the message to send.
This method will return normally if the mail is accepted for at This method will return normally if the mail is accepted for at least
least one recipiant .Otherwise it will throw an exception (either one recipiant.
SMTPSenderRefused,SMTPRecipientsRefused, or SMTPDataError) Otherwise it will throw an exception (either SMTPSenderRefused,
SMTPRecipientsRefused, or SMTPDataError)
That is, if this method does not throw an excception, then someone That is, if this method does not throw an exception, then someone
should get your mail. should get your mail.
It returns a dictionary , with one entry for each recipient that It returns a dictionary , with one entry for each recipient that was
was refused. refused.
example: example:
>>> import smtplib >>> import smtplib
>>> s=smtplib.SMTP("localhost") >>> s=smtplib.SMTP("localhost")
>>> tolist= [ "one@one.org", >>> tolist=["one@one.org","two@two.org","three@three.org","four@four.org"]
... "two@two.org",
... "three@three.org",
... "four@four.org"]
>>> msg = ''' >>> msg = '''
... From: Me@my.org ... From: Me@my.org
... Subject: testin'... ... Subject: testin'...
@ -234,10 +235,10 @@ class SMTP:
{ "three@three.org" : ( 550 ,"User unknown" ) } { "three@three.org" : ( 550 ,"User unknown" ) }
>>> s.quit() >>> s.quit()
In the above example, the message was accepted for delivery to three In the above example, the message was accepted for delivery to
of the four addresses, and one was rejected, with the error code 550. three of the four addresses, and one was rejected, with the error
If all addresses are accepted, then the method will return an code 550. If all addresses are accepted, then the method
empty dictionary. will return an empty dictionary.
""" """
if not self.helo_resp: if not self.helo_resp:
@ -250,7 +251,7 @@ class SMTP:
for each in to_addrs: for each in to_addrs:
(code,resp)=self.rcpt(each) (code,resp)=self.rcpt(each)
if (code <> 250) and (code <> 251): if (code <> 250) and (code <> 251):
senderr[each]=(code,resp) senderrs[each]=(code,resp)
if len(senderrs)==len(to_addrs): if len(senderrs)==len(to_addrs):
#th' server refused all our recipients #th' server refused all our recipients
self.rset() self.rset()