3 '''SMTP/ESMTP client class.
5 This should follow RFC 821 (SMTP) and RFC 1869 (ESMTP).
9 Please remember, when doing ESMTP, that the names of the SMTP service
10 extensions are NOT the same thing as the option keywords for the RCPT
16 >>> s=smtplib.SMTP("localhost")
18 This is Sendmail version 8.8.4
20 HELO EHLO MAIL RCPT DATA
21 RSET NOOP QUIT HELP VRFY
23 For more info use "HELP <topic>".
24 To report bugs in the implementation send email to
25 sendmail-bugs@sendmail.org.
26 For local information send email to Postmaster at your site.
28 >>> s.putcmd("vrfy","someone@here")
30 (250, "Somebody OverHere <somebody@here.my.org>")
34 # Author: The Dragon De Monsyne <dragondm@integral.org>
35 # ESMTP support, test code and doc fixes added by
36 # Eric S. Raymond <esr@thyrsus.com>
37 # Better RFC 821 compliance (MAIL and RCPT, and CRLF in data)
38 # by Carey Evans <c.evans@clear.net.nz>, for picky mail servers.
40 # This was modified from the Python 1.5 library HTTP lib.
47 __all__
= ["SMTPException","SMTPServerDisconnected","SMTPResponseException",
48 "SMTPSenderRefused","SMTPRecipientsRefused","SMTPDataError",
49 "SMTPConnectError","SMTPHeloError","quoteaddr","quotedata",
55 # Exception classes used by this module.
56 class SMTPException(Exception):
57 """Base class for all exceptions raised by this module."""
59 class SMTPServerDisconnected(SMTPException
):
60 """Not connected to any SMTP server.
62 This exception is raised when the server unexpectedly disconnects,
63 or when an attempt is made to use the SMTP instance before
64 connecting it to a server.
67 class SMTPResponseException(SMTPException
):
68 """Base class for all exceptions that include an SMTP error code.
70 These exceptions are generated in some instances when the SMTP
71 server returns an error code. The error code is stored in the
72 `smtp_code' attribute of the error, and the `smtp_error' attribute
73 is set to the error message.
76 def __init__(self
, code
, msg
):
79 self
.args
= (code
, msg
)
81 class SMTPSenderRefused(SMTPResponseException
):
82 """Sender address refused.
83 In addition to the attributes set by on all SMTPResponseException
84 exceptions, this sets `sender' to the string that the SMTP refused.
87 def __init__(self
, code
, msg
, sender
):
91 self
.args
= (code
, msg
, sender
)
93 class SMTPRecipientsRefused(SMTPException
):
94 """All recipient addresses refused.
95 The errors for each recipient are accessible through the attribute
96 'recipients', which is a dictionary of exactly the same sort as
97 SMTP.sendmail() returns.
100 def __init__(self
, recipients
):
101 self
.recipients
= recipients
102 self
.args
= ( recipients
,)
105 class SMTPDataError(SMTPResponseException
):
106 """The SMTP server didn't accept the data."""
108 class SMTPConnectError(SMTPResponseException
):
109 """Error during connection establishment."""
111 class SMTPHeloError(SMTPResponseException
):
112 """The server refused our HELO reply."""
116 """Quote a subset of the email addresses defined by RFC 821.
118 Should be able to handle anything rfc822.parseaddr can handle.
122 m
=rfc822
.parseaddr(addr
)[1]
123 except AttributeError:
126 #something weird here.. punt -ddm
132 """Quote data for email.
134 Double leading '.', and change Unix newline '\\n', or Mac '\\r' into
135 Internet CRLF end-of-line.
137 return re
.sub(r
'(?m)^\.', '..',
138 re
.sub(r
'(?:\r\n|\n|\r(?!\n))', CRLF
, data
))
142 """This class manages a connection to an SMTP or ESMTP server.
144 SMTP objects have the following attributes:
146 This is the message given by the server in response to the
147 most recent HELO command.
150 This is the message given by the server in response to the
151 most recent EHLO command. This is usually multiline.
154 This is a True value _after you do an EHLO command_, if the
155 server supports ESMTP.
158 This is a dictionary, which, if the server supports ESMTP,
159 will _after you do an EHLO command_, contain the names of the
160 SMTP service extensions this server supports, and their
163 Note, all extension names are mapped to lower case in the
166 See each method's docstrings for details. In general, there is a
167 method of the same name to perform each SMTP command. There is also a
168 method called 'sendmail' that will do an entire mail transaction.
176 def __init__(self
, host
= '', port
= 0):
177 """Initialize a new instance.
179 If specified, `host' is the name of the remote host to which to
180 connect. If specified, `port' specifies the port to which to connect.
181 By default, smtplib.SMTP_PORT is used. An SMTPConnectError is raised
182 if the specified `host' doesn't respond correctly.
185 self
.esmtp_features
= {}
187 (code
, msg
) = self
.connect(host
, port
)
189 raise SMTPConnectError(code
, msg
)
191 def set_debuglevel(self
, debuglevel
):
192 """Set the debug output level.
194 A non-false value results in debug messages for connection and for all
195 messages sent to and received from the server.
198 self
.debuglevel
= debuglevel
200 def connect(self
, host
='localhost', port
= 0):
201 """Connect to a host on a given port.
203 If the hostname ends with a colon (`:') followed by a number, and
204 there is no port specified, that suffix will be stripped off and the
205 number interpreted as the port number to use.
207 Note: This method is automatically invoked by __init__, if a host is
208 specified during instantiation.
214 host
, port
= host
[:i
], host
[i
+1:]
215 try: port
= int(port
)
217 raise socket
.error
, "nonnumeric port"
218 if not port
: port
= SMTP_PORT
219 self
.sock
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
220 if self
.debuglevel
> 0: print 'connect:', (host
, port
)
222 self
.sock
.connect((host
, port
))
226 (code
,msg
)=self
.getreply()
227 if self
.debuglevel
>0 : print "connect:", msg
231 """Send `str' to the server."""
232 if self
.debuglevel
> 0: print 'send:', `
str`
236 while sendptr
< len(str):
237 sendptr
= sendptr
+ self
.sock
.send(str[sendptr
:])
239 raise SMTPServerDisconnected('Server not connected')
241 raise SMTPServerDisconnected('please run connect() first')
243 def putcmd(self
, cmd
, args
=""):
244 """Send a command to the server."""
246 str = '%s%s' % (cmd
, CRLF
)
248 str = '%s %s%s' % (cmd
, args
, CRLF
)
252 """Get a reply from the server.
254 Returns a tuple consisting of:
256 - server response code (e.g. '250', or such, if all goes well)
257 Note: returns -1 if it can't read response code.
259 - server response string corresponding to response code (multiline
260 responses are converted to a single, multiline string).
262 Raises SMTPServerDisconnected if end-of-file is reached.
265 if self
.file is None:
266 self
.file = self
.sock
.makefile('rb')
268 line
= self
.file.readline()
271 raise SMTPServerDisconnected("Connection unexpectedly closed")
272 if self
.debuglevel
> 0: print 'reply:', `line`
273 resp
.append(line
[4:].strip())
275 # Check that the error code is syntactically correct.
276 # Don't attempt to read a continuation line if it is broken.
282 # Check if multiline response.
286 errmsg
= "\n".join(resp
)
287 if self
.debuglevel
> 0:
288 print 'reply: retcode (%s); Msg: %s' % (errcode
,errmsg
)
289 return errcode
, errmsg
291 def docmd(self
, cmd
, args
=""):
292 """Send a command, and return its response code."""
293 self
.putcmd(cmd
,args
)
294 return self
.getreply()
297 def helo(self
, name
=''):
298 """SMTP 'helo' command.
299 Hostname to send for this command defaults to the FQDN of the local
303 self
.putcmd("helo", name
)
305 self
.putcmd("helo", socket
.getfqdn())
306 (code
,msg
)=self
.getreply()
310 def ehlo(self
, name
=''):
311 """ SMTP 'ehlo' command.
312 Hostname to send for this command defaults to the FQDN of the local
316 self
.putcmd("ehlo", name
)
318 self
.putcmd("ehlo", socket
.getfqdn())
319 (code
,msg
)=self
.getreply()
320 # According to RFC1869 some (badly written)
321 # MTA's will disconnect on an ehlo. Toss an exception if
323 if code
== -1 and len(msg
) == 0:
324 raise SMTPServerDisconnected("Server not connected")
329 #parse the ehlo response -ddm
330 resp
=self
.ehlo_resp
.split('\n')
333 m
=re
.match(r
'(?P<feature>[A-Za-z0-9][A-Za-z0-9\-]*)',each
)
335 feature
=m
.group("feature").lower()
336 params
=m
.string
[m
.end("feature"):].strip()
337 self
.esmtp_features
[feature
]=params
340 def has_extn(self
, opt
):
341 """Does the server support a given SMTP service extension?"""
342 return self
.esmtp_features
.has_key(opt
.lower())
344 def help(self
, args
=''):
345 """SMTP 'help' command.
346 Returns help text from server."""
347 self
.putcmd("help", args
)
348 return self
.getreply()
351 """SMTP 'rset' command -- resets session."""
352 return self
.docmd("rset")
355 """SMTP 'noop' command -- doesn't do anything :>"""
356 return self
.docmd("noop")
358 def mail(self
,sender
,options
=[]):
359 """SMTP 'mail' command -- begins mail xfer session."""
361 if options
and self
.does_esmtp
:
362 optionlist
= ' ' + ' '.join(options
)
363 self
.putcmd("mail", "FROM:%s%s" % (quoteaddr(sender
) ,optionlist
))
364 return self
.getreply()
366 def rcpt(self
,recip
,options
=[]):
367 """SMTP 'rcpt' command -- indicates 1 recipient for this mail."""
369 if options
and self
.does_esmtp
:
370 optionlist
= ' ' + ' '.join(options
)
371 self
.putcmd("rcpt","TO:%s%s" % (quoteaddr(recip
),optionlist
))
372 return self
.getreply()
375 """SMTP 'DATA' command -- sends message data to server.
377 Automatically quotes lines beginning with a period per rfc821.
378 Raises SMTPDataError if there is an unexpected reply to the
379 DATA command; the return value from this method is the final
380 response code received when the all data is sent.
383 (code
,repl
)=self
.getreply()
384 if self
.debuglevel
>0 : print "data:", (code
,repl
)
386 raise SMTPDataError(code
,repl
)
393 (code
,msg
)=self
.getreply()
394 if self
.debuglevel
>0 : print "data:", (code
,msg
)
397 def verify(self
, address
):
398 """SMTP 'verify' command -- checks for address validity."""
399 self
.putcmd("vrfy", quoteaddr(address
))
400 return self
.getreply()
404 def expn(self
, address
):
405 """SMTP 'verify' command -- checks for address validity."""
406 self
.putcmd("expn", quoteaddr(address
))
407 return self
.getreply()
409 # some useful methods
410 def sendmail(self
, from_addr
, to_addrs
, msg
, mail_options
=[],
412 """This command performs an entire mail transaction.
415 - from_addr : The address sending this mail.
416 - to_addrs : A list of addresses to send this mail to. A bare
417 string will be treated as a list with 1 address.
418 - msg : The message to send.
419 - mail_options : List of ESMTP options (such as 8bitmime) for the
421 - rcpt_options : List of ESMTP options (such as DSN commands) for
422 all the rcpt commands.
424 If there has been no previous EHLO or HELO command this session, this
425 method tries ESMTP EHLO first. If the server does ESMTP, message size
426 and each of the specified options will be passed to it. If EHLO
427 fails, HELO will be tried and ESMTP options suppressed.
429 This method will return normally if the mail is accepted for at least
430 one recipient. It returns a dictionary, with one entry for each
431 recipient that was refused. Each entry contains a tuple of the SMTP
432 error code and the accompanying error message sent by the server.
434 This method may raise the following exceptions:
436 SMTPHeloError The server didn't reply properly to
438 SMTPRecipientsRefused The server rejected ALL recipients
440 SMTPSenderRefused The server didn't accept the from_addr.
441 SMTPDataError The server replied with an unexpected
442 error code (other than a refusal of
445 Note: the connection will be open even after an exception is raised.
450 >>> s=smtplib.SMTP("localhost")
451 >>> tolist=["one@one.org","two@two.org","three@three.org","four@four.org"]
454 ... Subject: testin'...
456 ... This is a test '''
457 >>> s.sendmail("me@my.org",tolist,msg)
458 { "three@three.org" : ( 550 ,"User unknown" ) }
461 In the above example, the message was accepted for delivery to three
462 of the four addresses, and one was rejected, with the error code
463 550. If all addresses are accepted, then the method will return an
467 if self
.helo_resp
is None and self
.ehlo_resp
is None:
468 if not (200 <= self
.ehlo()[0] <= 299):
469 (code
,resp
) = self
.helo()
470 if not (200 <= code
<= 299):
471 raise SMTPHeloError(code
, resp
)
474 # Hmmm? what's this? -ddm
475 # self.esmtp_features['7bit']=""
476 if self
.has_extn('size'):
477 esmtp_opts
.append("size=" + `
len(msg
)`
)
478 for option
in mail_options
:
479 esmtp_opts
.append(option
)
481 (code
,resp
) = self
.mail(from_addr
, esmtp_opts
)
484 raise SMTPSenderRefused(code
, resp
, from_addr
)
486 if type(to_addrs
) == types
.StringType
:
487 to_addrs
= [to_addrs
]
488 for each
in to_addrs
:
489 (code
,resp
)=self
.rcpt(each
, rcpt_options
)
490 if (code
!= 250) and (code
!= 251):
491 senderrs
[each
]=(code
,resp
)
492 if len(senderrs
)==len(to_addrs
):
493 # the server refused all our recipients
495 raise SMTPRecipientsRefused(senderrs
)
496 (code
,resp
) = self
.data(msg
)
499 raise SMTPDataError(code
, resp
)
500 #if we got here then somebody got our mail
505 """Close the connection to the SMTP server."""
515 """Terminate the SMTP session."""
520 # Test the sendmail method, which tests most of the others.
521 # Note: This always sends to localhost.
522 if __name__
== '__main__':
526 sys
.stdout
.write(prompt
+ ": ")
527 return sys
.stdin
.readline().strip()
529 fromaddr
= prompt("From")
530 toaddrs
= prompt("To").split(',')
531 print "Enter message, end with ^D:"
534 line
= sys
.stdin
.readline()
538 print "Message length is " + `
len(msg
)`
540 server
= SMTP('localhost')
541 server
.set_debuglevel(1)
542 server
.sendmail(fromaddr
, toaddrs
, msg
)