smtplib --- SMTP protocol client — Dokumentasi Python 3.15.2
Navigasi index modul | berikut | sebelum | Python » 3.15.2 documentation » The Python standard library » Protokol Internet dan Dukungan » smtplib --- SMTP protocol client | Tema | smtplib --- SMTP protocol client ¶ Source code: Lib/smtplib.py The smtplib module defines an SMTP client session object that can be used to send mail to any internet machine with an SMTP or ESMTP listener daemon. For details of SMTP and ESMTP operation, consult RFC 821 (Simple Mail Transfer Protocol) and RFC 1869 (SMTP Service Extensions).
Availability : not WASI. This module does not work or is not available on WebAssembly. See WebAssembly platforms for more information. class smtplib. SMTP ( host = '' , port = 0 , local_hostname = None , [ timeout , ] source_address = None ) ¶ An SMTP instance encapsulates an SMTP connection. It has methods that support a full repertoire of SMTP and ESMTP operations. If the host parameter is set to a true value, SMTP.connect() is called with host and port automatically when the object is created; otherwise, connect() must be called manually.
If specified, local_hostname is used as the FQDN of the local host in the HELO/EHLO command. Otherwise, the local hostname is found using socket.getfqdn() . If the connect() call returns anything other than a success code, an SMTPConnectError is raised. The optional timeout parameter specifies a timeout in seconds for blocking operations like the connection attempt (if not specified, the global default timeout setting will be used).
If the timeout expires, TimeoutError is raised. The optional source_address parameter allows binding to some specific source address in a machine with multiple network 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, sendmail() , and SMTP.quit() methods.
An example is included below. The SMTP class supports the with statement. When used like this, the SMTP QUIT command is issued automatically when the with statement exits. E.g.: >>> from smtplib import SMTP >>> with SMTP ( "domain.org" ) as smtp : ... smtp . noop () ... (250, b'Ok') >>> All commands will raise an auditing event smtplib.SMTP.send with arguments self and data , where data is the bytes about to be sent to the remote host. default_port ¶ The default port used for SMTP connections (25).
Berubah pada versi 3.3: Support for the with statement was added. Berubah pada versi 3.3: source_address argument was added. Added in version 3.5: The SMTPUTF8 extension ( RFC 6531 ) is now supported. Berubah pada versi 3.9: If the timeout parameter is set to be zero, it will raise a ValueError to prevent the creation of a non-blocking socket. class smtplib. SMTP_SSL ( host = '' , port = 0 , local_hostname = None , * , [ timeout , ] context = None , source_address = None ) ¶ An SMTP_SSL instance behaves exactly the same as instances of SMTP .
SMTP_SSL should be used for situations where SSL is required from the beginning of the connection and using SMTP.starttls() is not appropriate. If the host parameter is set to a true value, SMTP.connect() is called with host and port automatically when the object is created; otherwise, SMTP.connect() must be called manually. The optional arguments local_hostname , timeout and source_address have the same meaning as they do in the SMTP class. context , also optional, can contain a SSLContext and allows configuring various aspects of the secure connection.
Please read Security considerations for best practices. default_port ¶ The default port used for SMTP-over-SSL connections (465). Berubah pada versi 3.3: context was added. Berubah pada versi 3.3: The source_address argument was added. Berubah pada versi 3.4: The class now supports hostname check with ssl.SSLContext.check_hostname and Server Name Indication (see ssl.HAS_SNI ). Berubah pada versi 3.9: If the timeout parameter is set to be zero, it will raise a ValueError to prevent the creation of a non-blocking socket Berubah pada versi 3.12: The deprecated keyfile and certfile parameters have been removed. class smtplib.
LMTP ( host = '' , port = LMTP_PORT , local_hostname = None , source_address = None [ , timeout ] ) ¶ 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, so our connect() method must support that as well as a regular host:port server. The optional arguments local_hostname and source_address have the same meaning as they do in the SMTP class.
To specify a Unix socket, you must use an absolute path for host , starting with a '/'. Authentication is supported, using the regular SMTP mechanism. When using a Unix socket, LMTP generally don't support or require any authentication, but your mileage might vary. Berubah pada versi 3.9: The optional timeout parameter was added. A nice selection of exceptions is defined as well: exception smtplib. SMTPException ¶ Subclass of OSError that is the base exception class for all the other exceptions provided by this module.
Berubah pada versi 3.4: SMTPException became subclass of OSError exception smtplib. SMTPServerDisconnected ¶ This exception is raised when the server unexpectedly disconnects, or when an attempt is made to use the SMTP instance before connecting it to a server. exception smtplib. SMTPResponseException ¶ 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. smtp_code ¶ Kode errornya smtp_error ¶ The error message. exception smtplib.
SMTPSenderRefused ¶ Sender address refused. In addition to the attributes set by on all SMTPResponseException exceptions, this sets 'sender' to the string that the SMTP server refused. exception smtplib. SMTPRecipientsRefused ¶ All recipient addresses refused. recipients ¶ A dictionary of exactly the same sort as returned by SMTP.sendmail() containing the errors for each recipient. exception smtplib. SMTPDataError ¶ The SMTP server refused to accept the message data. exception smtplib.
SMTPConnectError ¶ Error occurred during establishment of a connection with the server. exception smtplib. SMTPHeloError ¶ The server refused our HELO message. exception smtplib. SMTPNotSupportedError ¶ The command or option attempted is not supported by the server. Added in version 3.5. exception smtplib. SMTPAuthenticationError ¶ SMTP authentication went wrong. Most probably the server didn't accept the username/password combination provided.
Lihat juga RFC 821 - Simple Mail Transfer Protocol Protocol definition for SMTP. This document covers the model, operating procedure, and protocol details for SMTP. RFC 1869 - SMTP Service Extensions Definition of the ESMTP extensions for SMTP. This describes a framework for extending SMTP with new commands, supporting dynamic discovery of the commands provided by the server, and defines a few additional commands. SMTP Objects ¶ An SMTP instance has the following methods: SMTP. set_debuglevel ( level ) ¶ Set the debug output level.
A value of 1 or True for level results in debug messages for connection and for all messages sent to and received from the server. A value of 2 for level results in these messages being timestamped. Berubah pada versi 3.5: Added debuglevel 2. SMTP. docmd ( cmd , args = '' ) ¶ Send a command cmd to the server. The optional argument args is simply concatenated to the command, separated by a space. This returns a 2-tuple composed of a numeric response code and the actual response line (multiline responses are joined into one long line.) In normal operation it should not be necessary to call this method explicitly.
It is used to implement other methods and may be useful for testing private extensions. If the connection
The rest of this page is on the live site.