This commit is contained in:
Christian Heimes 2013-06-23 22:57:22 +02:00
commit 42471ad7dd
2 changed files with 53 additions and 42 deletions

View file

@ -24,17 +24,20 @@ Protocol) and :rfc:`1869` (SMTP Service Extensions).
A :class:`SMTP` instance encapsulates an SMTP connection. It has methods A :class:`SMTP` instance encapsulates an SMTP connection. It has methods
that support a full repertoire of SMTP and ESMTP operations. If the optional that support a full repertoire of SMTP and ESMTP operations. If the optional
host and port parameters are given, the SMTP :meth:`connect` method is called host and port parameters are given, the SMTP :meth:`connect` method is
with those parameters during initialization. If the :meth:`connect` call called with those parameters during initialization. If specified,
returns anything other than a success code, an :exc:`SMTPConnectError` is *local_hostname* is used as the FQDN of the local host in the HELO/EHLO
raised. The optional *timeout* parameter specifies a timeout in seconds for command. Otherwise, the local hostname is found using
blocking operations like the connection attempt (if not specified, the :func:`socket.getfqdn`. If the :meth:`connect` call returns anything other
global default timeout setting will be used). The optional source_address than a success code, an :exc:`SMTPConnectError` is raised. The optional
parameter allows to bind to some specific source address in a machine with *timeout* parameter specifies a timeout in seconds for blocking operations
multiple network interfaces, and/or to some specific source TCP port. It like the connection attempt (if not specified, the global default timeout
takes a 2-tuple (host, port), for the socket to bind to as its source setting will be used). The optional source_address parameter allows to bind
address before connecting. If omitted (or if host or port are ``''`` and/or to some specific source address in a machine with multiple network
0 respectively) the OS default behavior will be used. interfaces, and/or to some specific source TCP port. It takes a 2-tuple
(host, port), for the socket to bind to as its source address before
connecting. If omitted (or if host or port are ``''`` and/or 0 respectively)
the OS default behavior will be used.
For normal use, you should only require the initialization/connect, For normal use, you should only require the initialization/connect,
:meth:`sendmail`, and :meth:`~smtplib.quit` methods. :meth:`sendmail`, and :meth:`~smtplib.quit` methods.
@ -57,17 +60,21 @@ Protocol) and :rfc:`1869` (SMTP Service Extensions).
.. versionchanged:: 3.3 .. versionchanged:: 3.3
source_address argument was added. source_address argument was added.
.. class:: SMTP_SSL(host='', port=0, local_hostname=None, keyfile=None, certfile=None[, timeout], context=None, source_address=None) .. class:: SMTP_SSL(host='', port=0, local_hostname=None, keyfile=None, \
certfile=None [, timeout], context=None, \
source_address=None)
A :class:`SMTP_SSL` instance behaves exactly the same as instances of A :class:`SMTP_SSL` instance behaves exactly the same as instances of
:class:`SMTP`. :class:`SMTP_SSL` should be used for situations where SSL is :class:`SMTP`. :class:`SMTP_SSL` should be used for situations where SSL is
required from the beginning of the connection and using :meth:`starttls` is required from the beginning of the connection and using :meth:`starttls` is
not appropriate. If *host* is not specified, the local host is used. If not appropriate. If *host* is not specified, the local host is used. If
*port* is zero, the standard SMTP-over-SSL port (465) is used. *keyfile* *port* is zero, the standard SMTP-over-SSL port (465) is used. The optional
and *certfile* are also optional, and can contain a PEM formatted private key arguments *local_hostname* and *source_address* have the same meaning as
and certificate chain file for the SSL connection. *context* also optional, can contain they do in the :class:`SMTP` class. *keyfile* and *certfile* are also
a SSLContext, and is an alternative to keyfile and certfile; If it is specified both optional, and can contain a PEM formatted private key and certificate chain
keyfile and certfile must be None. The optional *timeout* file for the SSL connection. *context* also optional, can contain a
SSLContext, and is an alternative to keyfile and certfile; If it is
specified both keyfile and certfile must be None. The optional *timeout*
parameter specifies a timeout in seconds for blocking operations like the parameter specifies a timeout in seconds for blocking operations like the
connection attempt (if not specified, the global default timeout setting connection attempt (if not specified, the global default timeout setting
will be used). The optional source_address parameter allows to bind to some will be used). The optional source_address parameter allows to bind to some
@ -90,12 +97,12 @@ Protocol) and :rfc:`1869` (SMTP Service Extensions).
standard SMTP client. It's common to use Unix sockets for LMTP, so our standard SMTP client. It's common to use Unix sockets for LMTP, so our
:meth:`connect` method must support that as well as a regular host:port :meth:`connect` method must support that as well as a regular host:port
server. The optional arguments local_hostname and source_address have the server. The optional arguments local_hostname and source_address have the
same meaning as that of SMTP client. To specify a Unix socket, you must use same meaning as they do in the :class:`SMTP` class. To specify a Unix
an absolute path for *host*, starting with a '/'. socket, you must use an absolute path for *host*, starting with a '/'.
Authentication is supported, using the regular SMTP mechanism. When using a Unix Authentication is supported, using the regular SMTP mechanism. When using a
socket, LMTP generally don't support or require any authentication, but your Unix socket, LMTP generally don't support or require any authentication, but
mileage might vary. your mileage might vary.
A nice selection of exceptions is defined as well: A nice selection of exceptions is defined as well:

View file

@ -222,13 +222,14 @@ class SMTP:
If specified, `host' is the name of the remote host to which to If specified, `host' is the name of the remote host to which to
connect. If specified, `port' specifies the port to which to connect. connect. If specified, `port' specifies the port to which to connect.
By default, smtplib.SMTP_PORT is used. If a host is specified the By default, smtplib.SMTP_PORT is used. If a host is specified the
connect method is called, and if it returns anything other than connect method is called, and if it returns anything other than a
a success code an SMTPConnectError is raised. If specified, success code an SMTPConnectError is raised. If specified,
`local_hostname` is used as the FQDN of the local host. By default, `local_hostname` is used as the FQDN of the local host in the HELO/EHLO
the local hostname is found using socket.getfqdn(). The command. Otherwise, the local hostname is found using
`source_address` parameter takes a 2-tuple (host, port) for the socket socket.getfqdn(). The `source_address` parameter takes a 2-tuple (host,
to bind to as its source address before connecting. If the host is '' port) for the socket to bind to as its source address before
and port is 0, the OS default behavior will be used. connecting. If the host is '' and port is 0, the OS default behavior
will be used.
""" """
self.timeout = timeout self.timeout = timeout
@ -852,15 +853,17 @@ class SMTP:
if _have_ssl: if _have_ssl:
class SMTP_SSL(SMTP): class SMTP_SSL(SMTP):
""" This is a subclass derived from SMTP that connects over an SSL encrypted """ This is a subclass derived from SMTP that connects over an SSL
socket (to use this class you need a socket module that was compiled with SSL encrypted socket (to use this class you need a socket module that was
support). If host is not specified, '' (the local host) is used. If port is compiled with SSL support). If host is not specified, '' (the local
omitted, the standard SMTP-over-SSL port (465) is used. The optional host) is used. If port is omitted, the standard SMTP-over-SSL port
source_address takes a two-tuple (host,port) for socket to bind to. keyfile and certfile (465) is used. local_hostname and source_address have the same meaning
are also optional - they can contain a PEM formatted private key and as they do in the SMTP class. keyfile and certfile are also optional -
certificate chain file for the SSL connection. context also optional, can contain they can contain a PEM formatted private key and certificate chain file
a SSLContext, and is an alternative to keyfile and certfile; If it is specified both for the SSL connection. context also optional, can contain a
keyfile and certfile must be None. SSLContext, and is an alternative to keyfile and certfile; If it is
specified both keyfile and certfile must be None.
""" """
default_port = SMTP_SSL_PORT default_port = SMTP_SSL_PORT
@ -903,10 +906,11 @@ class LMTP(SMTP):
"""LMTP - Local Mail Transfer Protocol """LMTP - Local Mail Transfer Protocol
The LMTP protocol, which is very similar to ESMTP, is heavily based The LMTP protocol, which is very similar to ESMTP, is heavily based
on the standard SMTP client. It's common to use Unix sockets for LMTP, on the standard SMTP client. It's common to use Unix sockets for
so our connect() method must support that as well as a regular LMTP, so our connect() method must support that as well as a regular
host:port server. To specify a Unix socket, you must use an absolute host:port server. local_hostname and source_address have the same
path as the host, starting with a '/'. meaning as they do in the SMTP class. To specify a Unix socket,
you must use an absolute path as the host, starting with a '/'.
Authentication is supported, using the regular SMTP mechanism. When Authentication is supported, using the regular SMTP mechanism. When
using a Unix socket, LMTP generally don't support or require any using a Unix socket, LMTP generally don't support or require any