| |
- exceptions.Exception(exceptions.BaseException)
-
- SMTPException
-
- SMTPRecipientsRefused
- SMTPResponseException
-
- SMTPAuthenticationError
- SMTPConnectError
- SMTPDataError
- SMTPHeloError
- SMTPSenderRefused
- SMTPServerDisconnected
- SMTP
-
- SMTP_SSL
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 response to the
most recent HELO command.
ehlo_resp
This is the message given by the server in response 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 extensions this server supports, and their
parameters (if any).
Note, all extension names are mapped to lower case in the
dictionary.
See each method's docstrings for details. In general, there is a
method of the same name to perform each SMTP command. There is also a
method called 'sendmail' that will do an entire mail transaction. |
|
Methods defined here:
- __init__(self, host='', port=0, local_hostname=None, timeout=<object object>)
- 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. If a host is specified the
connect method is called, and if it returns anything other than a
success code an SMTPConnectError is raised. If specified,
`local_hostname` is used as the FQDN of the local host for the
HELO/EHLO command. Otherwise, the local hostname is found using
socket.getfqdn().
- 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.
- ehlo_or_helo_if_needed(self)
- Call self.ehlo() and/or self.helo() if needed.
If there has been no previous EHLO or HELO command this session, this
method tries ESMTP EHLO first.
This method may raise the following exceptions:
SMTPHeloError The server didn't reply properly to
the helo greeting.
- expn(self, address)
- SMTP 'expn' command -- expands a mailing list.
- 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.
- login(self, user, password)
- Log in on an SMTP server that requires authentication.
The arguments are:
- user: The user name to authenticate with.
- password: The password for the authentication.
If there has been no previous EHLO or HELO command this session, this
method tries ESMTP EHLO first.
This method will return normally if the authentication was successful.
This method may raise the following exceptions:
SMTPHeloError The server didn't reply properly to
the helo greeting.
SMTPAuthenticationError The server didn't accept the username/
password combination.
SMTPException No suitable authentication method was
found.
- 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 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.
- starttls(self, keyfile=None, certfile=None)
- Puts the connection to the SMTP server into TLS mode.
If there has been no previous EHLO or HELO command this session, this
method tries ESMTP EHLO first.
If the server supports TLS, this will encrypt the rest of the SMTP
session. If you provide the keyfile and certfile parameters,
the identity of the SMTP server and client can be checked. This,
however, depends on whether the socket module really checks the
certificates.
This method may raise the following exceptions:
SMTPHeloError The server didn't reply properly to
the helo greeting.
- verify(self, address)
- SMTP 'verify' command -- checks for address validity.
- vrfy = verify(self, address)
Data and other attributes defined here:
- debuglevel = 0
- default_port = 25
- does_esmtp = 0
- ehlo_msg = 'ehlo'
- ehlo_resp = None
- file = None
- helo_resp = None
|
class SMTP_SSL(SMTP) |
|
This is a subclass derived from SMTP that connects over an SSL
encrypted socket (to use this class you need a socket module that was
compiled with SSL support). If host is not specified, '' (the local
host) is used. If port is omitted, the standard SMTP-over-SSL port
(465) is used. local_hostname has the same meaning as it does in the
SMTP class. keyfile and certfile are also optional - they can contain
a PEM formatted private key and certificate chain file for the SSL
connection. |
|
Methods defined here:
- __init__(self, host='', port=0, local_hostname=None, keyfile=None, certfile=None, timeout=<object object>)
Data and other attributes defined here:
- default_port = 465
Methods inherited from SMTP:
- 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.
- ehlo_or_helo_if_needed(self)
- Call self.ehlo() and/or self.helo() if needed.
If there has been no previous EHLO or HELO command this session, this
method tries ESMTP EHLO first.
This method may raise the following exceptions:
SMTPHeloError The server didn't reply properly to
the helo greeting.
- expn(self, address)
- SMTP 'expn' command -- expands a mailing list.
- 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.
- login(self, user, password)
- Log in on an SMTP server that requires authentication.
The arguments are:
- user: The user name to authenticate with.
- password: The password for the authentication.
If there has been no previous EHLO or HELO command this session, this
method tries ESMTP EHLO first.
This method will return normally if the authentication was successful.
This method may raise the following exceptions:
SMTPHeloError The server didn't reply properly to
the helo greeting.
SMTPAuthenticationError The server didn't accept the username/
password combination.
SMTPException No suitable authentication method was
found.
- 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 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.
- starttls(self, keyfile=None, certfile=None)
- Puts the connection to the SMTP server into TLS mode.
If there has been no previous EHLO or HELO command this session, this
method tries ESMTP EHLO first.
If the server supports TLS, this will encrypt the rest of the SMTP
session. If you provide the keyfile and certfile parameters,
the identity of the SMTP server and client can be checked. This,
however, depends on whether the socket module really checks the
certificates.
This method may raise the following exceptions:
SMTPHeloError The server didn't reply properly to
the helo greeting.
- verify(self, address)
- SMTP 'verify' command -- checks for address validity.
- vrfy = verify(self, address)
- SMTP 'verify' command -- checks for address validity.
Data and other attributes inherited from SMTP:
- debuglevel = 0
- does_esmtp = 0
- ehlo_msg = 'ehlo'
- ehlo_resp = None
- file = None
- helo_resp = None
| |