17db96d56Sopenharmony_ci:mod:`smtplib` --- SMTP protocol client 27db96d56Sopenharmony_ci======================================= 37db96d56Sopenharmony_ci 47db96d56Sopenharmony_ci.. module:: smtplib 57db96d56Sopenharmony_ci :synopsis: SMTP protocol client (requires sockets). 67db96d56Sopenharmony_ci 77db96d56Sopenharmony_ci.. sectionauthor:: Eric S. Raymond <esr@snark.thyrsus.com> 87db96d56Sopenharmony_ci 97db96d56Sopenharmony_ci**Source code:** :source:`Lib/smtplib.py` 107db96d56Sopenharmony_ci 117db96d56Sopenharmony_ci.. index:: 127db96d56Sopenharmony_ci pair: SMTP; protocol 137db96d56Sopenharmony_ci single: Simple Mail Transfer Protocol 147db96d56Sopenharmony_ci 157db96d56Sopenharmony_ci-------------- 167db96d56Sopenharmony_ci 177db96d56Sopenharmony_ciThe :mod:`smtplib` module defines an SMTP client session object that can be used 187db96d56Sopenharmony_cito send mail to any internet machine with an SMTP or ESMTP listener daemon. For 197db96d56Sopenharmony_cidetails of SMTP and ESMTP operation, consult :rfc:`821` (Simple Mail Transfer 207db96d56Sopenharmony_ciProtocol) and :rfc:`1869` (SMTP Service Extensions). 217db96d56Sopenharmony_ci 227db96d56Sopenharmony_ci.. include:: ../includes/wasm-notavail.rst 237db96d56Sopenharmony_ci 247db96d56Sopenharmony_ci.. class:: SMTP(host='', port=0, local_hostname=None[, timeout], source_address=None) 257db96d56Sopenharmony_ci 267db96d56Sopenharmony_ci An :class:`SMTP` instance encapsulates an SMTP connection. It has methods 277db96d56Sopenharmony_ci that support a full repertoire of SMTP and ESMTP operations. If the optional 287db96d56Sopenharmony_ci *host* and *port* parameters are given, the SMTP :meth:`connect` method is 297db96d56Sopenharmony_ci called with those parameters during initialization. If specified, 307db96d56Sopenharmony_ci *local_hostname* is used as the FQDN of the local host in the HELO/EHLO 317db96d56Sopenharmony_ci command. Otherwise, the local hostname is found using 327db96d56Sopenharmony_ci :func:`socket.getfqdn`. If the :meth:`connect` call returns anything other 337db96d56Sopenharmony_ci than a success code, an :exc:`SMTPConnectError` is raised. The optional 347db96d56Sopenharmony_ci *timeout* parameter specifies a timeout in seconds for blocking operations 357db96d56Sopenharmony_ci like the connection attempt (if not specified, the global default timeout 367db96d56Sopenharmony_ci setting will be used). If the timeout expires, :exc:`TimeoutError` is 377db96d56Sopenharmony_ci raised. The optional *source_address* parameter allows binding 387db96d56Sopenharmony_ci to some specific source address in a machine with multiple network 397db96d56Sopenharmony_ci interfaces, and/or to some specific source TCP port. It takes a 2-tuple 407db96d56Sopenharmony_ci ``(host, port)``, for the socket to bind to as its source address before 417db96d56Sopenharmony_ci connecting. If omitted (or if *host* or *port* are ``''`` and/or ``0`` 427db96d56Sopenharmony_ci respectively) the OS default behavior will be used. 437db96d56Sopenharmony_ci 447db96d56Sopenharmony_ci For normal use, you should only require the initialization/connect, 457db96d56Sopenharmony_ci :meth:`sendmail`, and :meth:`SMTP.quit` methods. 467db96d56Sopenharmony_ci An example is included below. 477db96d56Sopenharmony_ci 487db96d56Sopenharmony_ci The :class:`SMTP` class supports the :keyword:`with` statement. When used 497db96d56Sopenharmony_ci like this, the SMTP ``QUIT`` command is issued automatically when the 507db96d56Sopenharmony_ci :keyword:`!with` statement exits. E.g.:: 517db96d56Sopenharmony_ci 527db96d56Sopenharmony_ci >>> from smtplib import SMTP 537db96d56Sopenharmony_ci >>> with SMTP("domain.org") as smtp: 547db96d56Sopenharmony_ci ... smtp.noop() 557db96d56Sopenharmony_ci ... 567db96d56Sopenharmony_ci (250, b'Ok') 577db96d56Sopenharmony_ci >>> 587db96d56Sopenharmony_ci 597db96d56Sopenharmony_ci .. audit-event:: smtplib.send self,data smtplib.SMTP 607db96d56Sopenharmony_ci 617db96d56Sopenharmony_ci All commands will raise an :ref:`auditing event <auditing>` 627db96d56Sopenharmony_ci ``smtplib.SMTP.send`` with arguments ``self`` and ``data``, 637db96d56Sopenharmony_ci where ``data`` is the bytes about to be sent to the remote host. 647db96d56Sopenharmony_ci 657db96d56Sopenharmony_ci .. versionchanged:: 3.3 667db96d56Sopenharmony_ci Support for the :keyword:`with` statement was added. 677db96d56Sopenharmony_ci 687db96d56Sopenharmony_ci .. versionchanged:: 3.3 697db96d56Sopenharmony_ci source_address argument was added. 707db96d56Sopenharmony_ci 717db96d56Sopenharmony_ci .. versionadded:: 3.5 727db96d56Sopenharmony_ci The SMTPUTF8 extension (:rfc:`6531`) is now supported. 737db96d56Sopenharmony_ci 747db96d56Sopenharmony_ci .. versionchanged:: 3.9 757db96d56Sopenharmony_ci If the *timeout* parameter is set to be zero, it will raise a 767db96d56Sopenharmony_ci :class:`ValueError` to prevent the creation of a non-blocking socket 777db96d56Sopenharmony_ci 787db96d56Sopenharmony_ci.. class:: SMTP_SSL(host='', port=0, local_hostname=None, keyfile=None, \ 797db96d56Sopenharmony_ci certfile=None [, timeout], context=None, \ 807db96d56Sopenharmony_ci source_address=None) 817db96d56Sopenharmony_ci 827db96d56Sopenharmony_ci An :class:`SMTP_SSL` instance behaves exactly the same as instances of 837db96d56Sopenharmony_ci :class:`SMTP`. :class:`SMTP_SSL` should be used for situations where SSL is 847db96d56Sopenharmony_ci required from the beginning of the connection and using :meth:`starttls` is 857db96d56Sopenharmony_ci not appropriate. If *host* is not specified, the local host is used. If 867db96d56Sopenharmony_ci *port* is zero, the standard SMTP-over-SSL port (465) is used. The optional 877db96d56Sopenharmony_ci arguments *local_hostname*, *timeout* and *source_address* have the same 887db96d56Sopenharmony_ci meaning as they do in the :class:`SMTP` class. *context*, also optional, 897db96d56Sopenharmony_ci can contain a :class:`~ssl.SSLContext` and allows configuring various 907db96d56Sopenharmony_ci aspects of the secure connection. Please read :ref:`ssl-security` for 917db96d56Sopenharmony_ci best practices. 927db96d56Sopenharmony_ci 937db96d56Sopenharmony_ci *keyfile* and *certfile* are a legacy alternative to *context*, and can 947db96d56Sopenharmony_ci point to a PEM formatted private key and certificate chain file for the 957db96d56Sopenharmony_ci SSL connection. 967db96d56Sopenharmony_ci 977db96d56Sopenharmony_ci .. versionchanged:: 3.3 987db96d56Sopenharmony_ci *context* was added. 997db96d56Sopenharmony_ci 1007db96d56Sopenharmony_ci .. versionchanged:: 3.3 1017db96d56Sopenharmony_ci source_address argument was added. 1027db96d56Sopenharmony_ci 1037db96d56Sopenharmony_ci .. versionchanged:: 3.4 1047db96d56Sopenharmony_ci The class now supports hostname check with 1057db96d56Sopenharmony_ci :attr:`ssl.SSLContext.check_hostname` and *Server Name Indication* (see 1067db96d56Sopenharmony_ci :data:`ssl.HAS_SNI`). 1077db96d56Sopenharmony_ci 1087db96d56Sopenharmony_ci .. deprecated:: 3.6 1097db96d56Sopenharmony_ci 1107db96d56Sopenharmony_ci *keyfile* and *certfile* are deprecated in favor of *context*. 1117db96d56Sopenharmony_ci Please use :meth:`ssl.SSLContext.load_cert_chain` instead, or let 1127db96d56Sopenharmony_ci :func:`ssl.create_default_context` select the system's trusted CA 1137db96d56Sopenharmony_ci certificates for you. 1147db96d56Sopenharmony_ci 1157db96d56Sopenharmony_ci .. versionchanged:: 3.9 1167db96d56Sopenharmony_ci If the *timeout* parameter is set to be zero, it will raise a 1177db96d56Sopenharmony_ci :class:`ValueError` to prevent the creation of a non-blocking socket 1187db96d56Sopenharmony_ci 1197db96d56Sopenharmony_ci.. class:: LMTP(host='', port=LMTP_PORT, local_hostname=None, \ 1207db96d56Sopenharmony_ci source_address=None[, timeout]) 1217db96d56Sopenharmony_ci 1227db96d56Sopenharmony_ci The LMTP protocol, which is very similar to ESMTP, is heavily based on the 1237db96d56Sopenharmony_ci standard SMTP client. It's common to use Unix sockets for LMTP, so our 1247db96d56Sopenharmony_ci :meth:`connect` method must support that as well as a regular host:port 1257db96d56Sopenharmony_ci server. The optional arguments local_hostname and source_address have the 1267db96d56Sopenharmony_ci same meaning as they do in the :class:`SMTP` class. To specify a Unix 1277db96d56Sopenharmony_ci socket, you must use an absolute path for *host*, starting with a '/'. 1287db96d56Sopenharmony_ci 1297db96d56Sopenharmony_ci Authentication is supported, using the regular SMTP mechanism. When using a 1307db96d56Sopenharmony_ci Unix socket, LMTP generally don't support or require any authentication, but 1317db96d56Sopenharmony_ci your mileage might vary. 1327db96d56Sopenharmony_ci 1337db96d56Sopenharmony_ci .. versionchanged:: 3.9 1347db96d56Sopenharmony_ci The optional *timeout* parameter was added. 1357db96d56Sopenharmony_ci 1367db96d56Sopenharmony_ci 1377db96d56Sopenharmony_ciA nice selection of exceptions is defined as well: 1387db96d56Sopenharmony_ci 1397db96d56Sopenharmony_ci 1407db96d56Sopenharmony_ci.. exception:: SMTPException 1417db96d56Sopenharmony_ci 1427db96d56Sopenharmony_ci Subclass of :exc:`OSError` that is the base exception class for all 1437db96d56Sopenharmony_ci the other exceptions provided by this module. 1447db96d56Sopenharmony_ci 1457db96d56Sopenharmony_ci .. versionchanged:: 3.4 1467db96d56Sopenharmony_ci SMTPException became subclass of :exc:`OSError` 1477db96d56Sopenharmony_ci 1487db96d56Sopenharmony_ci 1497db96d56Sopenharmony_ci.. exception:: SMTPServerDisconnected 1507db96d56Sopenharmony_ci 1517db96d56Sopenharmony_ci This exception is raised when the server unexpectedly disconnects, or when an 1527db96d56Sopenharmony_ci attempt is made to use the :class:`SMTP` instance before connecting it to a 1537db96d56Sopenharmony_ci server. 1547db96d56Sopenharmony_ci 1557db96d56Sopenharmony_ci 1567db96d56Sopenharmony_ci.. exception:: SMTPResponseException 1577db96d56Sopenharmony_ci 1587db96d56Sopenharmony_ci Base class for all exceptions that include an SMTP error code. These exceptions 1597db96d56Sopenharmony_ci are generated in some instances when the SMTP server returns an error code. The 1607db96d56Sopenharmony_ci error code is stored in the :attr:`smtp_code` attribute of the error, and the 1617db96d56Sopenharmony_ci :attr:`smtp_error` attribute is set to the error message. 1627db96d56Sopenharmony_ci 1637db96d56Sopenharmony_ci 1647db96d56Sopenharmony_ci.. exception:: SMTPSenderRefused 1657db96d56Sopenharmony_ci 1667db96d56Sopenharmony_ci Sender address refused. In addition to the attributes set by on all 1677db96d56Sopenharmony_ci :exc:`SMTPResponseException` exceptions, this sets 'sender' to the string that 1687db96d56Sopenharmony_ci the SMTP server refused. 1697db96d56Sopenharmony_ci 1707db96d56Sopenharmony_ci 1717db96d56Sopenharmony_ci.. exception:: SMTPRecipientsRefused 1727db96d56Sopenharmony_ci 1737db96d56Sopenharmony_ci All recipient addresses refused. The errors for each recipient are accessible 1747db96d56Sopenharmony_ci through the attribute :attr:`recipients`, which is a dictionary of exactly the 1757db96d56Sopenharmony_ci same sort as :meth:`SMTP.sendmail` returns. 1767db96d56Sopenharmony_ci 1777db96d56Sopenharmony_ci 1787db96d56Sopenharmony_ci.. exception:: SMTPDataError 1797db96d56Sopenharmony_ci 1807db96d56Sopenharmony_ci The SMTP server refused to accept the message data. 1817db96d56Sopenharmony_ci 1827db96d56Sopenharmony_ci 1837db96d56Sopenharmony_ci.. exception:: SMTPConnectError 1847db96d56Sopenharmony_ci 1857db96d56Sopenharmony_ci Error occurred during establishment of a connection with the server. 1867db96d56Sopenharmony_ci 1877db96d56Sopenharmony_ci 1887db96d56Sopenharmony_ci.. exception:: SMTPHeloError 1897db96d56Sopenharmony_ci 1907db96d56Sopenharmony_ci The server refused our ``HELO`` message. 1917db96d56Sopenharmony_ci 1927db96d56Sopenharmony_ci 1937db96d56Sopenharmony_ci.. exception:: SMTPNotSupportedError 1947db96d56Sopenharmony_ci 1957db96d56Sopenharmony_ci The command or option attempted is not supported by the server. 1967db96d56Sopenharmony_ci 1977db96d56Sopenharmony_ci .. versionadded:: 3.5 1987db96d56Sopenharmony_ci 1997db96d56Sopenharmony_ci 2007db96d56Sopenharmony_ci.. exception:: SMTPAuthenticationError 2017db96d56Sopenharmony_ci 2027db96d56Sopenharmony_ci SMTP authentication went wrong. Most probably the server didn't accept the 2037db96d56Sopenharmony_ci username/password combination provided. 2047db96d56Sopenharmony_ci 2057db96d56Sopenharmony_ci 2067db96d56Sopenharmony_ci.. seealso:: 2077db96d56Sopenharmony_ci 2087db96d56Sopenharmony_ci :rfc:`821` - Simple Mail Transfer Protocol 2097db96d56Sopenharmony_ci Protocol definition for SMTP. This document covers the model, operating 2107db96d56Sopenharmony_ci procedure, and protocol details for SMTP. 2117db96d56Sopenharmony_ci 2127db96d56Sopenharmony_ci :rfc:`1869` - SMTP Service Extensions 2137db96d56Sopenharmony_ci Definition of the ESMTP extensions for SMTP. This describes a framework for 2147db96d56Sopenharmony_ci extending SMTP with new commands, supporting dynamic discovery of the commands 2157db96d56Sopenharmony_ci provided by the server, and defines a few additional commands. 2167db96d56Sopenharmony_ci 2177db96d56Sopenharmony_ci 2187db96d56Sopenharmony_ci.. _smtp-objects: 2197db96d56Sopenharmony_ci 2207db96d56Sopenharmony_ciSMTP Objects 2217db96d56Sopenharmony_ci------------ 2227db96d56Sopenharmony_ci 2237db96d56Sopenharmony_ciAn :class:`SMTP` instance has the following methods: 2247db96d56Sopenharmony_ci 2257db96d56Sopenharmony_ci 2267db96d56Sopenharmony_ci.. method:: SMTP.set_debuglevel(level) 2277db96d56Sopenharmony_ci 2287db96d56Sopenharmony_ci Set the debug output level. A value of 1 or ``True`` for *level* results in 2297db96d56Sopenharmony_ci debug messages for connection and for all messages sent to and received from 2307db96d56Sopenharmony_ci the server. A value of 2 for *level* results in these messages being 2317db96d56Sopenharmony_ci timestamped. 2327db96d56Sopenharmony_ci 2337db96d56Sopenharmony_ci .. versionchanged:: 3.5 Added debuglevel 2. 2347db96d56Sopenharmony_ci 2357db96d56Sopenharmony_ci 2367db96d56Sopenharmony_ci.. method:: SMTP.docmd(cmd, args='') 2377db96d56Sopenharmony_ci 2387db96d56Sopenharmony_ci Send a command *cmd* to the server. The optional argument *args* is simply 2397db96d56Sopenharmony_ci concatenated to the command, separated by a space. 2407db96d56Sopenharmony_ci 2417db96d56Sopenharmony_ci This returns a 2-tuple composed of a numeric response code and the actual 2427db96d56Sopenharmony_ci response line (multiline responses are joined into one long line.) 2437db96d56Sopenharmony_ci 2447db96d56Sopenharmony_ci In normal operation it should not be necessary to call this method explicitly. 2457db96d56Sopenharmony_ci It is used to implement other methods and may be useful for testing private 2467db96d56Sopenharmony_ci extensions. 2477db96d56Sopenharmony_ci 2487db96d56Sopenharmony_ci If the connection to the server is lost while waiting for the reply, 2497db96d56Sopenharmony_ci :exc:`SMTPServerDisconnected` will be raised. 2507db96d56Sopenharmony_ci 2517db96d56Sopenharmony_ci 2527db96d56Sopenharmony_ci.. method:: SMTP.connect(host='localhost', port=0) 2537db96d56Sopenharmony_ci 2547db96d56Sopenharmony_ci Connect to a host on a given port. The defaults are to connect to the local 2557db96d56Sopenharmony_ci host at the standard SMTP port (25). If the hostname ends with a colon (``':'``) 2567db96d56Sopenharmony_ci followed by a number, that suffix will be stripped off and the number 2577db96d56Sopenharmony_ci interpreted as the port number to use. This method is automatically invoked by 2587db96d56Sopenharmony_ci the constructor if a host is specified during instantiation. Returns a 2597db96d56Sopenharmony_ci 2-tuple of the response code and message sent by the server in its 2607db96d56Sopenharmony_ci connection response. 2617db96d56Sopenharmony_ci 2627db96d56Sopenharmony_ci .. audit-event:: smtplib.connect self,host,port smtplib.SMTP.connect 2637db96d56Sopenharmony_ci 2647db96d56Sopenharmony_ci 2657db96d56Sopenharmony_ci.. method:: SMTP.helo(name='') 2667db96d56Sopenharmony_ci 2677db96d56Sopenharmony_ci Identify yourself to the SMTP server using ``HELO``. The hostname argument 2687db96d56Sopenharmony_ci defaults to the fully qualified domain name of the local host. 2697db96d56Sopenharmony_ci The message returned by the server is stored as the :attr:`helo_resp` attribute 2707db96d56Sopenharmony_ci of the object. 2717db96d56Sopenharmony_ci 2727db96d56Sopenharmony_ci In normal operation it should not be necessary to call this method explicitly. 2737db96d56Sopenharmony_ci It will be implicitly called by the :meth:`sendmail` when necessary. 2747db96d56Sopenharmony_ci 2757db96d56Sopenharmony_ci 2767db96d56Sopenharmony_ci.. method:: SMTP.ehlo(name='') 2777db96d56Sopenharmony_ci 2787db96d56Sopenharmony_ci Identify yourself to an ESMTP server using ``EHLO``. The hostname argument 2797db96d56Sopenharmony_ci defaults to the fully qualified domain name of the local host. Examine the 2807db96d56Sopenharmony_ci response for ESMTP option and store them for use by :meth:`has_extn`. 2817db96d56Sopenharmony_ci Also sets several informational attributes: the message returned by 2827db96d56Sopenharmony_ci the server is stored as the :attr:`ehlo_resp` attribute, :attr:`does_esmtp` 2837db96d56Sopenharmony_ci is set to ``True`` or ``False`` depending on whether the server supports 2847db96d56Sopenharmony_ci ESMTP, and :attr:`esmtp_features` will be a dictionary containing the names 2857db96d56Sopenharmony_ci of the SMTP service extensions this server supports, and their parameters 2867db96d56Sopenharmony_ci (if any). 2877db96d56Sopenharmony_ci 2887db96d56Sopenharmony_ci Unless you wish to use :meth:`has_extn` before sending mail, it should not be 2897db96d56Sopenharmony_ci necessary to call this method explicitly. It will be implicitly called by 2907db96d56Sopenharmony_ci :meth:`sendmail` when necessary. 2917db96d56Sopenharmony_ci 2927db96d56Sopenharmony_ci.. method:: SMTP.ehlo_or_helo_if_needed() 2937db96d56Sopenharmony_ci 2947db96d56Sopenharmony_ci This method calls :meth:`ehlo` and/or :meth:`helo` if there has been no 2957db96d56Sopenharmony_ci previous ``EHLO`` or ``HELO`` command this session. It tries ESMTP ``EHLO`` 2967db96d56Sopenharmony_ci first. 2977db96d56Sopenharmony_ci 2987db96d56Sopenharmony_ci :exc:`SMTPHeloError` 2997db96d56Sopenharmony_ci The server didn't reply properly to the ``HELO`` greeting. 3007db96d56Sopenharmony_ci 3017db96d56Sopenharmony_ci.. method:: SMTP.has_extn(name) 3027db96d56Sopenharmony_ci 3037db96d56Sopenharmony_ci Return :const:`True` if *name* is in the set of SMTP service extensions returned 3047db96d56Sopenharmony_ci by the server, :const:`False` otherwise. Case is ignored. 3057db96d56Sopenharmony_ci 3067db96d56Sopenharmony_ci 3077db96d56Sopenharmony_ci.. method:: SMTP.verify(address) 3087db96d56Sopenharmony_ci 3097db96d56Sopenharmony_ci Check the validity of an address on this server using SMTP ``VRFY``. Returns a 3107db96d56Sopenharmony_ci tuple consisting of code 250 and a full :rfc:`822` address (including human 3117db96d56Sopenharmony_ci name) if the user address is valid. Otherwise returns an SMTP error code of 400 3127db96d56Sopenharmony_ci or greater and an error string. 3137db96d56Sopenharmony_ci 3147db96d56Sopenharmony_ci .. note:: 3157db96d56Sopenharmony_ci 3167db96d56Sopenharmony_ci Many sites disable SMTP ``VRFY`` in order to foil spammers. 3177db96d56Sopenharmony_ci 3187db96d56Sopenharmony_ci 3197db96d56Sopenharmony_ci.. method:: SMTP.login(user, password, *, initial_response_ok=True) 3207db96d56Sopenharmony_ci 3217db96d56Sopenharmony_ci Log in on an SMTP server that requires authentication. The arguments are the 3227db96d56Sopenharmony_ci username and the password to authenticate with. If there has been no previous 3237db96d56Sopenharmony_ci ``EHLO`` or ``HELO`` command this session, this method tries ESMTP ``EHLO`` 3247db96d56Sopenharmony_ci first. This method will return normally if the authentication was successful, or 3257db96d56Sopenharmony_ci may raise the following exceptions: 3267db96d56Sopenharmony_ci 3277db96d56Sopenharmony_ci :exc:`SMTPHeloError` 3287db96d56Sopenharmony_ci The server didn't reply properly to the ``HELO`` greeting. 3297db96d56Sopenharmony_ci 3307db96d56Sopenharmony_ci :exc:`SMTPAuthenticationError` 3317db96d56Sopenharmony_ci The server didn't accept the username/password combination. 3327db96d56Sopenharmony_ci 3337db96d56Sopenharmony_ci :exc:`SMTPNotSupportedError` 3347db96d56Sopenharmony_ci The ``AUTH`` command is not supported by the server. 3357db96d56Sopenharmony_ci 3367db96d56Sopenharmony_ci :exc:`SMTPException` 3377db96d56Sopenharmony_ci No suitable authentication method was found. 3387db96d56Sopenharmony_ci 3397db96d56Sopenharmony_ci Each of the authentication methods supported by :mod:`smtplib` are tried in 3407db96d56Sopenharmony_ci turn if they are advertised as supported by the server. See :meth:`auth` 3417db96d56Sopenharmony_ci for a list of supported authentication methods. *initial_response_ok* is 3427db96d56Sopenharmony_ci passed through to :meth:`auth`. 3437db96d56Sopenharmony_ci 3447db96d56Sopenharmony_ci Optional keyword argument *initial_response_ok* specifies whether, for 3457db96d56Sopenharmony_ci authentication methods that support it, an "initial response" as specified 3467db96d56Sopenharmony_ci in :rfc:`4954` can be sent along with the ``AUTH`` command, rather than 3477db96d56Sopenharmony_ci requiring a challenge/response. 3487db96d56Sopenharmony_ci 3497db96d56Sopenharmony_ci .. versionchanged:: 3.5 3507db96d56Sopenharmony_ci :exc:`SMTPNotSupportedError` may be raised, and the 3517db96d56Sopenharmony_ci *initial_response_ok* parameter was added. 3527db96d56Sopenharmony_ci 3537db96d56Sopenharmony_ci 3547db96d56Sopenharmony_ci.. method:: SMTP.auth(mechanism, authobject, *, initial_response_ok=True) 3557db96d56Sopenharmony_ci 3567db96d56Sopenharmony_ci Issue an ``SMTP`` ``AUTH`` command for the specified authentication 3577db96d56Sopenharmony_ci *mechanism*, and handle the challenge response via *authobject*. 3587db96d56Sopenharmony_ci 3597db96d56Sopenharmony_ci *mechanism* specifies which authentication mechanism is to 3607db96d56Sopenharmony_ci be used as argument to the ``AUTH`` command; the valid values are 3617db96d56Sopenharmony_ci those listed in the ``auth`` element of :attr:`esmtp_features`. 3627db96d56Sopenharmony_ci 3637db96d56Sopenharmony_ci *authobject* must be a callable object taking an optional single argument: 3647db96d56Sopenharmony_ci 3657db96d56Sopenharmony_ci data = authobject(challenge=None) 3667db96d56Sopenharmony_ci 3677db96d56Sopenharmony_ci If optional keyword argument *initial_response_ok* is true, 3687db96d56Sopenharmony_ci ``authobject()`` will be called first with no argument. It can return the 3697db96d56Sopenharmony_ci :rfc:`4954` "initial response" ASCII ``str`` which will be encoded and sent with 3707db96d56Sopenharmony_ci the ``AUTH`` command as below. If the ``authobject()`` does not support an 3717db96d56Sopenharmony_ci initial response (e.g. because it requires a challenge), it should return 3727db96d56Sopenharmony_ci ``None`` when called with ``challenge=None``. If *initial_response_ok* is 3737db96d56Sopenharmony_ci false, then ``authobject()`` will not be called first with ``None``. 3747db96d56Sopenharmony_ci 3757db96d56Sopenharmony_ci If the initial response check returns ``None``, or if *initial_response_ok* is 3767db96d56Sopenharmony_ci false, ``authobject()`` will be called to process the server's challenge 3777db96d56Sopenharmony_ci response; the *challenge* argument it is passed will be a ``bytes``. It 3787db96d56Sopenharmony_ci should return ASCII ``str`` *data* that will be base64 encoded and sent to the 3797db96d56Sopenharmony_ci server. 3807db96d56Sopenharmony_ci 3817db96d56Sopenharmony_ci The ``SMTP`` class provides ``authobjects`` for the ``CRAM-MD5``, ``PLAIN``, 3827db96d56Sopenharmony_ci and ``LOGIN`` mechanisms; they are named ``SMTP.auth_cram_md5``, 3837db96d56Sopenharmony_ci ``SMTP.auth_plain``, and ``SMTP.auth_login`` respectively. They all require 3847db96d56Sopenharmony_ci that the ``user`` and ``password`` properties of the ``SMTP`` instance are 3857db96d56Sopenharmony_ci set to appropriate values. 3867db96d56Sopenharmony_ci 3877db96d56Sopenharmony_ci User code does not normally need to call ``auth`` directly, but can instead 3887db96d56Sopenharmony_ci call the :meth:`login` method, which will try each of the above mechanisms 3897db96d56Sopenharmony_ci in turn, in the order listed. ``auth`` is exposed to facilitate the 3907db96d56Sopenharmony_ci implementation of authentication methods not (or not yet) supported 3917db96d56Sopenharmony_ci directly by :mod:`smtplib`. 3927db96d56Sopenharmony_ci 3937db96d56Sopenharmony_ci .. versionadded:: 3.5 3947db96d56Sopenharmony_ci 3957db96d56Sopenharmony_ci 3967db96d56Sopenharmony_ci.. method:: SMTP.starttls(keyfile=None, certfile=None, context=None) 3977db96d56Sopenharmony_ci 3987db96d56Sopenharmony_ci Put the SMTP connection in TLS (Transport Layer Security) mode. All SMTP 3997db96d56Sopenharmony_ci commands that follow will be encrypted. You should then call :meth:`ehlo` 4007db96d56Sopenharmony_ci again. 4017db96d56Sopenharmony_ci 4027db96d56Sopenharmony_ci If *keyfile* and *certfile* are provided, they are used to create an 4037db96d56Sopenharmony_ci :class:`ssl.SSLContext`. 4047db96d56Sopenharmony_ci 4057db96d56Sopenharmony_ci Optional *context* parameter is an :class:`ssl.SSLContext` object; This is 4067db96d56Sopenharmony_ci an alternative to using a keyfile and a certfile and if specified both 4077db96d56Sopenharmony_ci *keyfile* and *certfile* should be ``None``. 4087db96d56Sopenharmony_ci 4097db96d56Sopenharmony_ci If there has been no previous ``EHLO`` or ``HELO`` command this session, 4107db96d56Sopenharmony_ci this method tries ESMTP ``EHLO`` first. 4117db96d56Sopenharmony_ci 4127db96d56Sopenharmony_ci .. deprecated:: 3.6 4137db96d56Sopenharmony_ci 4147db96d56Sopenharmony_ci *keyfile* and *certfile* are deprecated in favor of *context*. 4157db96d56Sopenharmony_ci Please use :meth:`ssl.SSLContext.load_cert_chain` instead, or let 4167db96d56Sopenharmony_ci :func:`ssl.create_default_context` select the system's trusted CA 4177db96d56Sopenharmony_ci certificates for you. 4187db96d56Sopenharmony_ci 4197db96d56Sopenharmony_ci :exc:`SMTPHeloError` 4207db96d56Sopenharmony_ci The server didn't reply properly to the ``HELO`` greeting. 4217db96d56Sopenharmony_ci 4227db96d56Sopenharmony_ci :exc:`SMTPNotSupportedError` 4237db96d56Sopenharmony_ci The server does not support the STARTTLS extension. 4247db96d56Sopenharmony_ci 4257db96d56Sopenharmony_ci :exc:`RuntimeError` 4267db96d56Sopenharmony_ci SSL/TLS support is not available to your Python interpreter. 4277db96d56Sopenharmony_ci 4287db96d56Sopenharmony_ci .. versionchanged:: 3.3 4297db96d56Sopenharmony_ci *context* was added. 4307db96d56Sopenharmony_ci 4317db96d56Sopenharmony_ci .. versionchanged:: 3.4 4327db96d56Sopenharmony_ci The method now supports hostname check with 4337db96d56Sopenharmony_ci :attr:`SSLContext.check_hostname` and *Server Name Indicator* (see 4347db96d56Sopenharmony_ci :data:`~ssl.HAS_SNI`). 4357db96d56Sopenharmony_ci 4367db96d56Sopenharmony_ci .. versionchanged:: 3.5 4377db96d56Sopenharmony_ci The error raised for lack of STARTTLS support is now the 4387db96d56Sopenharmony_ci :exc:`SMTPNotSupportedError` subclass instead of the base 4397db96d56Sopenharmony_ci :exc:`SMTPException`. 4407db96d56Sopenharmony_ci 4417db96d56Sopenharmony_ci 4427db96d56Sopenharmony_ci.. method:: SMTP.sendmail(from_addr, to_addrs, msg, mail_options=(), rcpt_options=()) 4437db96d56Sopenharmony_ci 4447db96d56Sopenharmony_ci Send mail. The required arguments are an :rfc:`822` from-address string, a list 4457db96d56Sopenharmony_ci of :rfc:`822` to-address strings (a bare string will be treated as a list with 1 4467db96d56Sopenharmony_ci address), and a message string. The caller may pass a list of ESMTP options 4477db96d56Sopenharmony_ci (such as ``8bitmime``) to be used in ``MAIL FROM`` commands as *mail_options*. 4487db96d56Sopenharmony_ci ESMTP options (such as ``DSN`` commands) that should be used with all ``RCPT`` 4497db96d56Sopenharmony_ci commands can be passed as *rcpt_options*. (If you need to use different ESMTP 4507db96d56Sopenharmony_ci options to different recipients you have to use the low-level methods such as 4517db96d56Sopenharmony_ci :meth:`mail`, :meth:`rcpt` and :meth:`data` to send the message.) 4527db96d56Sopenharmony_ci 4537db96d56Sopenharmony_ci .. note:: 4547db96d56Sopenharmony_ci 4557db96d56Sopenharmony_ci The *from_addr* and *to_addrs* parameters are used to construct the message 4567db96d56Sopenharmony_ci envelope used by the transport agents. ``sendmail`` does not modify the 4577db96d56Sopenharmony_ci message headers in any way. 4587db96d56Sopenharmony_ci 4597db96d56Sopenharmony_ci *msg* may be a string containing characters in the ASCII range, or a byte 4607db96d56Sopenharmony_ci string. A string is encoded to bytes using the ascii codec, and lone ``\r`` 4617db96d56Sopenharmony_ci and ``\n`` characters are converted to ``\r\n`` characters. A byte string is 4627db96d56Sopenharmony_ci not modified. 4637db96d56Sopenharmony_ci 4647db96d56Sopenharmony_ci If there has been no previous ``EHLO`` or ``HELO`` command this session, this 4657db96d56Sopenharmony_ci method tries ESMTP ``EHLO`` first. If the server does ESMTP, message size and 4667db96d56Sopenharmony_ci each of the specified options will be passed to it (if the option is in the 4677db96d56Sopenharmony_ci feature set the server advertises). If ``EHLO`` fails, ``HELO`` will be tried 4687db96d56Sopenharmony_ci and ESMTP options suppressed. 4697db96d56Sopenharmony_ci 4707db96d56Sopenharmony_ci This method will return normally if the mail is accepted for at least one 4717db96d56Sopenharmony_ci recipient. Otherwise it will raise an exception. That is, if this method does 4727db96d56Sopenharmony_ci not raise an exception, then someone should get your mail. If this method does 4737db96d56Sopenharmony_ci not raise an exception, it returns a dictionary, with one entry for each 4747db96d56Sopenharmony_ci recipient that was refused. Each entry contains a tuple of the SMTP error code 4757db96d56Sopenharmony_ci and the accompanying error message sent by the server. 4767db96d56Sopenharmony_ci 4777db96d56Sopenharmony_ci If ``SMTPUTF8`` is included in *mail_options*, and the server supports it, 4787db96d56Sopenharmony_ci *from_addr* and *to_addrs* may contain non-ASCII characters. 4797db96d56Sopenharmony_ci 4807db96d56Sopenharmony_ci This method may raise the following exceptions: 4817db96d56Sopenharmony_ci 4827db96d56Sopenharmony_ci :exc:`SMTPRecipientsRefused` 4837db96d56Sopenharmony_ci All recipients were refused. Nobody got the mail. The :attr:`recipients` 4847db96d56Sopenharmony_ci attribute of the exception object is a dictionary with information about the 4857db96d56Sopenharmony_ci refused recipients (like the one returned when at least one recipient was 4867db96d56Sopenharmony_ci accepted). 4877db96d56Sopenharmony_ci 4887db96d56Sopenharmony_ci :exc:`SMTPHeloError` 4897db96d56Sopenharmony_ci The server didn't reply properly to the ``HELO`` greeting. 4907db96d56Sopenharmony_ci 4917db96d56Sopenharmony_ci :exc:`SMTPSenderRefused` 4927db96d56Sopenharmony_ci The server didn't accept the *from_addr*. 4937db96d56Sopenharmony_ci 4947db96d56Sopenharmony_ci :exc:`SMTPDataError` 4957db96d56Sopenharmony_ci The server replied with an unexpected error code (other than a refusal of a 4967db96d56Sopenharmony_ci recipient). 4977db96d56Sopenharmony_ci 4987db96d56Sopenharmony_ci :exc:`SMTPNotSupportedError` 4997db96d56Sopenharmony_ci ``SMTPUTF8`` was given in the *mail_options* but is not supported by the 5007db96d56Sopenharmony_ci server. 5017db96d56Sopenharmony_ci 5027db96d56Sopenharmony_ci Unless otherwise noted, the connection will be open even after an exception is 5037db96d56Sopenharmony_ci raised. 5047db96d56Sopenharmony_ci 5057db96d56Sopenharmony_ci .. versionchanged:: 3.2 5067db96d56Sopenharmony_ci *msg* may be a byte string. 5077db96d56Sopenharmony_ci 5087db96d56Sopenharmony_ci .. versionchanged:: 3.5 5097db96d56Sopenharmony_ci ``SMTPUTF8`` support added, and :exc:`SMTPNotSupportedError` may be 5107db96d56Sopenharmony_ci raised if ``SMTPUTF8`` is specified but the server does not support it. 5117db96d56Sopenharmony_ci 5127db96d56Sopenharmony_ci 5137db96d56Sopenharmony_ci.. method:: SMTP.send_message(msg, from_addr=None, to_addrs=None, \ 5147db96d56Sopenharmony_ci mail_options=(), rcpt_options=()) 5157db96d56Sopenharmony_ci 5167db96d56Sopenharmony_ci This is a convenience method for calling :meth:`sendmail` with the message 5177db96d56Sopenharmony_ci represented by an :class:`email.message.Message` object. The arguments have 5187db96d56Sopenharmony_ci the same meaning as for :meth:`sendmail`, except that *msg* is a ``Message`` 5197db96d56Sopenharmony_ci object. 5207db96d56Sopenharmony_ci 5217db96d56Sopenharmony_ci If *from_addr* is ``None`` or *to_addrs* is ``None``, ``send_message`` fills 5227db96d56Sopenharmony_ci those arguments with addresses extracted from the headers of *msg* as 5237db96d56Sopenharmony_ci specified in :rfc:`5322`\: *from_addr* is set to the :mailheader:`Sender` 5247db96d56Sopenharmony_ci field if it is present, and otherwise to the :mailheader:`From` field. 5257db96d56Sopenharmony_ci *to_addrs* combines the values (if any) of the :mailheader:`To`, 5267db96d56Sopenharmony_ci :mailheader:`Cc`, and :mailheader:`Bcc` fields from *msg*. If exactly one 5277db96d56Sopenharmony_ci set of :mailheader:`Resent-*` headers appear in the message, the regular 5287db96d56Sopenharmony_ci headers are ignored and the :mailheader:`Resent-*` headers are used instead. 5297db96d56Sopenharmony_ci If the message contains more than one set of :mailheader:`Resent-*` headers, 5307db96d56Sopenharmony_ci a :exc:`ValueError` is raised, since there is no way to unambiguously detect 5317db96d56Sopenharmony_ci the most recent set of :mailheader:`Resent-` headers. 5327db96d56Sopenharmony_ci 5337db96d56Sopenharmony_ci ``send_message`` serializes *msg* using 5347db96d56Sopenharmony_ci :class:`~email.generator.BytesGenerator` with ``\r\n`` as the *linesep*, and 5357db96d56Sopenharmony_ci calls :meth:`sendmail` to transmit the resulting message. Regardless of the 5367db96d56Sopenharmony_ci values of *from_addr* and *to_addrs*, ``send_message`` does not transmit any 5377db96d56Sopenharmony_ci :mailheader:`Bcc` or :mailheader:`Resent-Bcc` headers that may appear 5387db96d56Sopenharmony_ci in *msg*. If any of the addresses in *from_addr* and *to_addrs* contain 5397db96d56Sopenharmony_ci non-ASCII characters and the server does not advertise ``SMTPUTF8`` support, 5407db96d56Sopenharmony_ci an :exc:`SMTPNotSupported` error is raised. Otherwise the ``Message`` is 5417db96d56Sopenharmony_ci serialized with a clone of its :mod:`~email.policy` with the 5427db96d56Sopenharmony_ci :attr:`~email.policy.EmailPolicy.utf8` attribute set to ``True``, and 5437db96d56Sopenharmony_ci ``SMTPUTF8`` and ``BODY=8BITMIME`` are added to *mail_options*. 5447db96d56Sopenharmony_ci 5457db96d56Sopenharmony_ci .. versionadded:: 3.2 5467db96d56Sopenharmony_ci 5477db96d56Sopenharmony_ci .. versionadded:: 3.5 5487db96d56Sopenharmony_ci Support for internationalized addresses (``SMTPUTF8``). 5497db96d56Sopenharmony_ci 5507db96d56Sopenharmony_ci 5517db96d56Sopenharmony_ci.. method:: SMTP.quit() 5527db96d56Sopenharmony_ci 5537db96d56Sopenharmony_ci Terminate the SMTP session and close the connection. Return the result of 5547db96d56Sopenharmony_ci the SMTP ``QUIT`` command. 5557db96d56Sopenharmony_ci 5567db96d56Sopenharmony_ci 5577db96d56Sopenharmony_ciLow-level methods corresponding to the standard SMTP/ESMTP commands ``HELP``, 5587db96d56Sopenharmony_ci``RSET``, ``NOOP``, ``MAIL``, ``RCPT``, and ``DATA`` are also supported. 5597db96d56Sopenharmony_ciNormally these do not need to be called directly, so they are not documented 5607db96d56Sopenharmony_cihere. For details, consult the module code. 5617db96d56Sopenharmony_ci 5627db96d56Sopenharmony_ci 5637db96d56Sopenharmony_ci.. _smtp-example: 5647db96d56Sopenharmony_ci 5657db96d56Sopenharmony_ciSMTP Example 5667db96d56Sopenharmony_ci------------ 5677db96d56Sopenharmony_ci 5687db96d56Sopenharmony_ciThis example prompts the user for addresses needed in the message envelope ('To' 5697db96d56Sopenharmony_ciand 'From' addresses), and the message to be delivered. Note that the headers 5707db96d56Sopenharmony_cito be included with the message must be included in the message as entered; this 5717db96d56Sopenharmony_ciexample doesn't do any processing of the :rfc:`822` headers. In particular, the 5727db96d56Sopenharmony_ci'To' and 'From' addresses must be included in the message headers explicitly. :: 5737db96d56Sopenharmony_ci 5747db96d56Sopenharmony_ci import smtplib 5757db96d56Sopenharmony_ci 5767db96d56Sopenharmony_ci def prompt(prompt): 5777db96d56Sopenharmony_ci return input(prompt).strip() 5787db96d56Sopenharmony_ci 5797db96d56Sopenharmony_ci fromaddr = prompt("From: ") 5807db96d56Sopenharmony_ci toaddrs = prompt("To: ").split() 5817db96d56Sopenharmony_ci print("Enter message, end with ^D (Unix) or ^Z (Windows):") 5827db96d56Sopenharmony_ci 5837db96d56Sopenharmony_ci # Add the From: and To: headers at the start! 5847db96d56Sopenharmony_ci msg = ("From: %s\r\nTo: %s\r\n\r\n" 5857db96d56Sopenharmony_ci % (fromaddr, ", ".join(toaddrs))) 5867db96d56Sopenharmony_ci while True: 5877db96d56Sopenharmony_ci try: 5887db96d56Sopenharmony_ci line = input() 5897db96d56Sopenharmony_ci except EOFError: 5907db96d56Sopenharmony_ci break 5917db96d56Sopenharmony_ci if not line: 5927db96d56Sopenharmony_ci break 5937db96d56Sopenharmony_ci msg = msg + line 5947db96d56Sopenharmony_ci 5957db96d56Sopenharmony_ci print("Message length is", len(msg)) 5967db96d56Sopenharmony_ci 5977db96d56Sopenharmony_ci server = smtplib.SMTP('localhost') 5987db96d56Sopenharmony_ci server.set_debuglevel(1) 5997db96d56Sopenharmony_ci server.sendmail(fromaddr, toaddrs, msg) 6007db96d56Sopenharmony_ci server.quit() 6017db96d56Sopenharmony_ci 6027db96d56Sopenharmony_ci.. note:: 6037db96d56Sopenharmony_ci 6047db96d56Sopenharmony_ci In general, you will want to use the :mod:`email` package's features to 6057db96d56Sopenharmony_ci construct an email message, which you can then send 6067db96d56Sopenharmony_ci via :meth:`~smtplib.SMTP.send_message`; see :ref:`email-examples`. 607