Skip to content

Commit b5196fa

Browse files
pythongh-137339: Clarify host and port parameter behavior in smtplib.SMTP{_SSL} initialization (python#137340)
This also documents the previously undocumented default_port parameter. Co-authored-by: Stan Ulbrych <[email protected]>
1 parent 2fb2b82 commit b5196fa

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

Doc/library/smtplib.rst

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ Protocol) and :rfc:`1869` (SMTP Service Extensions).
2424
.. class:: SMTP(host='', port=0, local_hostname=None[, timeout], source_address=None)
2525

2626
An :class:`SMTP` instance encapsulates an SMTP connection. It has methods
27-
that support a full repertoire of SMTP and ESMTP operations. If the optional
28-
*host* and *port* parameters are given, the SMTP :meth:`connect` method is
29-
called with those parameters during initialization. If specified,
30-
*local_hostname* is used as the FQDN of the local host in the HELO/EHLO
27+
that support a full repertoire of SMTP and ESMTP operations.
28+
29+
If the host parameter is set to a truthy value, :meth:`SMTP.connect` is called with
30+
host and port automatically when the object is created; otherwise, :meth:`!connect` must
31+
be called manually.
32+
33+
If specified, *local_hostname* is used as the FQDN of the local host in the HELO/EHLO
3134
command. Otherwise, the local hostname is found using
3235
:func:`socket.getfqdn`. If the :meth:`connect` call returns anything other
3336
than a success code, an :exc:`SMTPConnectError` is raised. The optional
@@ -62,6 +65,10 @@ Protocol) and :rfc:`1869` (SMTP Service Extensions).
6265
``smtplib.SMTP.send`` with arguments ``self`` and ``data``,
6366
where ``data`` is the bytes about to be sent to the remote host.
6467

68+
.. attribute:: SMTP.default_port
69+
70+
The default port used for SMTP connections (25).
71+
6572
.. versionchanged:: 3.3
6673
Support for the :keyword:`with` statement was added.
6774

@@ -80,15 +87,23 @@ Protocol) and :rfc:`1869` (SMTP Service Extensions).
8087

8188
An :class:`SMTP_SSL` instance behaves exactly the same as instances of
8289
:class:`SMTP`. :class:`SMTP_SSL` should be used for situations where SSL is
83-
required from the beginning of the connection and using :meth:`~SMTP.starttls`
84-
is not appropriate. If *host* is not specified, the local host is used. If
85-
*port* is zero, the standard SMTP-over-SSL port (465) is used. The optional
86-
arguments *local_hostname*, *timeout* and *source_address* have the same
90+
required from the beginning of the connection and using :meth:`SMTP.starttls` is
91+
not appropriate.
92+
93+
If the host parameter is set to a truthy value, :meth:`SMTP.connect` is called with host
94+
and port automatically when the object is created; otherwise, :meth:`!SMTP.connect` must
95+
be called manually.
96+
97+
The optional arguments *local_hostname*, *timeout* and *source_address* have the same
8798
meaning as they do in the :class:`SMTP` class. *context*, also optional,
8899
can contain a :class:`~ssl.SSLContext` and allows configuring various
89100
aspects of the secure connection. Please read :ref:`ssl-security` for
90101
best practices.
91102

103+
.. attribute:: SMTP_SSL.default_port
104+
105+
The default port used for SMTP-over-SSL connections (465).
106+
92107
.. versionchanged:: 3.3
93108
*context* was added.
94109

@@ -259,6 +274,9 @@ An :class:`SMTP` instance has the following methods:
259274
2-tuple of the response code and message sent by the server in its
260275
connection response.
261276

277+
If port is not changed from its default value of 0, the value of the :attr:`default_port`
278+
attribute is used.
279+
262280
.. audit-event:: smtplib.connect self,host,port smtplib.SMTP.connect
263281

264282

0 commit comments

Comments
 (0)