| |
- exceptions.Exception
-
- SMTPException
-
- SMTPResponseException
-
- SMTPConnectError
- SMTPDataError
- SMTPHeloError
- SMTPRecipientsRefused
- SMTPSenderRefused
- SMTPServerDisconnected
- SMTP
class SMTP |
|
This class manages a connection to an SMTP or ESMTP server.
SMTP Objects:
SMTP objects have the following attributes:
helo_resp
This is the message given by the server in responce to the
most recent HELO command.
ehlo_resp
This is the message given by the server in responce to the
most recent EHLO command. This is usually multiline.
does_esmtp
This is a True value _after you do an EHLO command_, if the
server supports ESMTP.
esmtp_features
This is a dictionary, which, if the server supports ESMTP,
will _after you do an EHLO command_, contain the names of the
SMTP service extentions this server supports, and their
parameters (if any).
Note, all extention names are mapped to lower case in the
dictionary.
For method docs, see each method's docstrings. In general, there is
a method of the same name to perform each SMTP command, and there
is a method called 'sendmail' that will do an entire mail
transaction.
|
| |
- __init__(self, host='', port=0)
- Initialize a new instance.
- If specified, `host' is the name of the remote host to which to
- connect. If specified, `port' specifies the port to which to connect.
- By default, smtplib.SMTP_PORT is used. An SMTPConnectError is
- raised if the specified `host' doesn't respond correctly.
- close(self)
- Close the connection to the SMTP server.
- connect(self, host='localhost', port=0)
- Connect to a host on a given port.
- If the hostname ends with a colon (`:') followed by a number, and
- there is no port specified, that suffix will be stripped off and the
- number interpreted as the port number to use.
- Note: This method is automatically invoked by __init__, if a host is
- specified during instantiation.
- data(self, msg)
- SMTP 'DATA' command -- sends message data to server.
- Automatically quotes lines beginning with a period per rfc821.
- Raises SMTPDataError if there is an unexpected reply to the
- DATA command; the return value from this method is the final
- response code received when the all data is sent.
- docmd(self, cmd, args='')
- Send a command, and return its response code.
- ehlo(self, name='')
- SMTP 'ehlo' command.
- Hostname to send for this command defaults to the FQDN of the local
- host.
- expn(self, address)
- SMTP 'verify' command -- checks for address validity.
- getreply(self)
- Get a reply from the server.
- Returns a tuple consisting of:
- - server response code (e.g. '250', or such, if all goes well)
- Note: returns -1 if it can't read response code.
- - server response string corresponding to response code (multiline
- responses are converted to a single, multiline string).
- Raises SMTPServerDisconnected if end-of-file is reached.
- has_extn(self, opt)
- Does the server support a given SMTP service extension?
- helo(self, name='')
- SMTP 'helo' command.
- Hostname to send for this command defaults to the FQDN of the local
- host.
- help(self, args='')
- SMTP 'help' command.
- Returns help text from server.
- mail(self, sender, options=[])
- SMTP 'mail' command -- begins mail xfer session.
- noop(self)
- SMTP 'noop' command -- doesn't do anything :>
- putcmd(self, cmd, args='')
- Send a command to the server.
- quit(self)
- Terminate the SMTP session.
- rcpt(self, recip, options=[])
- SMTP 'rcpt' command -- indicates 1 recipient for this mail.
- rset(self)
- SMTP 'rset' command -- resets session.
- send(self, str)
- Send `str' to the server.
- sendmail(self, from_addr, to_addrs, msg, mail_options=[], rcpt_options=[])
- This command performs an entire mail transaction.
- The arguments are:
- - from_addr : The address sending this mail.
- - to_addrs : A list of addresses to send this mail to. A bare
- string will be treated as a list with 1 address.
- - msg : The message to send.
- - mail_options : List of ESMTP options (such as 8bitmime) for the
- mail command.
- - rcpt_options : List of ESMTP options (such as DSN commands) for
- all the rcpt commands.
- If there has been no previous EHLO or HELO command this session, this
- method tries ESMTP EHLO first. If the server does ESMTP, message size
- and each of the specified options will be passed to it. If EHLO
- fails, HELO will be tried and ESMTP options suppressed.
- This method will return normally if the mail is accepted for at least
- one recipient. It returns a dictionary, with one entry for each
- recipient that was refused. Each entry contains a tuple of
- the SMTP error code and the accompanying error message sent by
- the server.
- This method may raise the following exceptions:
- SMTPHeloError The server didn't reply properly to
- the helo greeting.
- SMTPRecipientsRefused The server rejected for ALL recipients
- (no mail was sent).
- SMTPSenderRefused The server didn't accept the from_addr.
- SMTPDataError The server replied with an unexpected
- error code (other than a refusal of
- a recipient).
- Note: the connection will be open even after an exception is raised.
- Example:
- >>> import smtplib
- >>> s=smtplib.SMTP("localhost")
- >>> tolist=["one@one.org","two@two.org","three@three.org","four@four.org"]
- >>> msg = '''
- ... From: Me@my.org
- ... Subject: testin'...
- ...
- ... This is a test '''
- >>> s.sendmail("me@my.org",tolist,msg)
- { "three@three.org" : ( 550 ,"User unknown" ) }
- >>> s.quit()
- In the above example, the message was accepted for delivery to three
- of the four addresses, and one was rejected, with the error code
- 550. If all addresses are accepted, then the method will return an
- empty dictionary.
- set_debuglevel(self, debuglevel)
- Set the debug output level.
- A non-false value results in debug messages for connection and for all
- messages sent to and received from the server.
- verify(self, address)
- SMTP 'verify' command -- checks for address validity.
- verify(self, address)
- SMTP 'verify' command -- checks for address validity.
|
class SMTPResponseException(SMTPException) |
|
Base class for all exceptions that include an SMTP error code.
These exceptions are generated in some instances when the SMTP
server returns an error code. The error code is stored in the
`smtp_code' attribute of the error, and the `smtp_error' attribute
is set to the error message.
|
| |
- __init__(self, code, msg)
- no doc string
|
|