552 message size exceeds fixed limit là gì

This module does not work or is not available on WebAssembly platforms`wasm32-emscripten` and wasm32-wasi. See for more information.

class smtplib.SMTP(host='', port=0, local_hostname=None, [timeout, ]source_address=None)

An instance encapsulates an SMTP connection. It has methods that support a full repertoire of SMTP and ESMTP operations. If the optional host and port parameters are given, the SMTP method is called with those parameters during initialization. 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 . If the call returns anything other than a success code, an 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, 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

data = authobject(challenge=None)

2, for the socket to bind to as its source address before connecting. If omitted (or if host or port are

data = authobject(challenge=None)

3 and/or

data = authobject(challenge=None)

4 respectively) the OS default behavior will be used.

For normal use, you should only require the initialization/connect, , and methods. An example is included below.

The class supports the statement. When used like this, the SMTP

data = authobject(challenge=None)

9 command is issued automatically when the

data = authobject(challenge=None)

8 statement exits. E.g.:

> from smtplib import SMTP
with SMTP("domain.org") as smtp: ... smtp.noop() ... (250, b'Ok')

All commands will raise an

import smtplib def prompt(prompt):

return input(prompt).strip()
fromaddr = prompt("From: ") toaddrs = prompt("To: ").split() print("Enter message, end with ^D (Unix) or ^Z (Windows):")

Add the From: and To: headers at the start!

msg = ("From: %s\r\nTo: %s\r\n\r\n"

   % (fromaddr, ", ".join(toaddrs)))
while True:
try:
    line = input()
except EOFError:
    break
if not line:
    break
msg = msg + line
print("Message length is", len(msg)) server = smtplib.SMTP('localhost') server.set_debuglevel(1) server.sendmail(fromaddr, toaddrs, msg) server.quit()

1 with arguments

import smtplib def prompt(prompt):

return input(prompt).strip()
fromaddr = prompt("From: ") toaddrs = prompt("To: ").split() print("Enter message, end with ^D (Unix) or ^Z (Windows):")

Add the From: and To: headers at the start!

msg = ("From: %s\r\nTo: %s\r\n\r\n"

   % (fromaddr, ", ".join(toaddrs)))
while True:
try:
    line = input()
except EOFError:
    break
if not line:
    break
msg = msg + line
print("Message length is", len(msg)) server = smtplib.SMTP('localhost') server.set_debuglevel(1) server.sendmail(fromaddr, toaddrs, msg) server.quit()

2 and

import smtplib def prompt(prompt):

return input(prompt).strip()
fromaddr = prompt("From: ") toaddrs = prompt("To: ").split() print("Enter message, end with ^D (Unix) or ^Z (Windows):")

Add the From: and To: headers at the start!

msg = ("From: %s\r\nTo: %s\r\n\r\n"

   % (fromaddr, ", ".join(toaddrs)))
while True:
try:
    line = input()
except EOFError:
    break
if not line:
    break
msg = msg + line
print("Message length is", len(msg)) server = smtplib.SMTP('localhost') server.set_debuglevel(1) server.sendmail(fromaddr, toaddrs, msg) server.quit()

3, where

import smtplib def prompt(prompt):

return input(prompt).strip()
fromaddr = prompt("From: ") toaddrs = prompt("To: ").split() print("Enter message, end with ^D (Unix) or ^Z (Windows):")

Add the From: and To: headers at the start!

msg = ("From: %s\r\nTo: %s\r\n\r\n"

   % (fromaddr, ", ".join(toaddrs)))
while True:
try:
    line = input()
except EOFError:
    break
if not line:
    break
msg = msg + line
print("Message length is", len(msg)) server = smtplib.SMTP('localhost') server.set_debuglevel(1) server.sendmail(fromaddr, toaddrs, msg) server.quit()

3 is the bytes about to be sent to the remote host.

Changed in version 3.3: Support for the statement was added.

Changed in version 3.3: source_address argument was added.

New in version 3.5: The SMTPUTF8 extension (RFC 6531) is now supported.

Changed in version 3.9: If the timeout parameter is set to be zero, it will raise a 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 instance behaves exactly the same as instances of . should be used for situations where SSL is required from the beginning of the connection and using `smtplib`0 is 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. The optional arguments local_hostname, timeout and source_address have the same meaning as they do in the class. context, also optional, can contain a and allows configuring various aspects of the secure connection. Please read for best practices.

Changed in version 3.3: context was added.

Changed in version 3.3: The source_address argument was added.

Changed in version 3.9: If the timeout parameter is set to be zero, it will raise a to prevent the creation of a non-blocking socket

Changed in version 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 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.

Changed in version 3.9: The optional timeout parameter was added.

A nice selection of exceptions is defined as well:

exception smtplib.SMTPException

Subclass of that is the base exception class for all the other exceptions provided by this module.

Changed in version 3.4: SMTPException became subclass of

exception smtplib.SMTPServerDisconnected

This exception is raised when the server unexpectedly disconnects, or when an attempt is made to use the 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. The error code is stored in the `smtplib`9 attribute of the error, and the`wasm32-emscripten`0 attribute is set to the error message.

exception smtplib.SMTPSenderRefused

Sender address refused. In addition to the attributes set by on all exceptions, this sets ‘sender’ to the string that the SMTP server refused.

exception smtplib.SMTPRecipientsRefused

All recipient addresses refused. The errors for each recipient are accessible through the attribute `wasm32-emscripten`2, which is a dictionary of exactly the same sort as returns.

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 `wasm32-emscripten`4 message.

exception smtplib.SMTPNotSupportedError

The command or option attempted is not supported by the server.

New in version 3.5.

exception smtplib.SMTPAuthenticationError

SMTP authentication went wrong. Most probably the server didn’t accept the username/password combination provided.

See also

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 instance has the following methods:

SMTP.set_debuglevel(level)

Set the debug output level. A value of 1 or `wasm32-emscripten`6 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.

Changed in version 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 to the server is lost while waiting for the reply, will be raised.

SMTP.connect(host\='localhost', port\=0)

Connect to a host on a given port. The defaults are to connect to the local host at the standard SMTP port (25). If the hostname ends with a colon (`wasm32-emscripten`8) followed by a number, that suffix will be stripped off and the number interpreted as the port number to use. This method is automatically invoked by the constructor if a host is specified during instantiation. Returns a 2-tuple of the response code and message sent by the server in its connection response.

Raises an `wasm32-emscripten`9 with arguments

import smtplib def prompt(prompt):

return input(prompt).strip()
fromaddr = prompt("From: ") toaddrs = prompt("To: ").split() print("Enter message, end with ^D (Unix) or ^Z (Windows):")

Add the From: and To: headers at the start!

msg = ("From: %s\r\nTo: %s\r\n\r\n"

   % (fromaddr, ", ".join(toaddrs)))
while True:
try:
    line = input()
except EOFError:
    break
if not line:
    break
msg = msg + line
print("Message length is", len(msg)) server = smtplib.SMTP('localhost') server.set_debuglevel(1) server.sendmail(fromaddr, toaddrs, msg) server.quit()

2, `wasm32-wasi`1, `wasm32-wasi`2.

SMTP.helo(name\='')

Identify yourself to the SMTP server using `wasm32-emscripten`4. The hostname argument defaults to the fully qualified domain name of the local host. The message returned by the server is stored as the `wasm32-wasi`4 attribute of the object.

In normal operation it should not be necessary to call this method explicitly. It will be implicitly called by the when necessary.

SMTP.ehlo(name\='')

Identify yourself to an ESMTP server using `wasm32-wasi`6. The hostname argument defaults to the fully qualified domain name of the local host. Examine the response for ESMTP option and store them for use by . Also sets several informational attributes: the message returned by the server is stored as the `wasm32-wasi`8 attribute, `wasm32-wasi`9 is set to `wasm32-emscripten`6 or `SMTP`1 depending on whether the server supports ESMTP, and `SMTP`2 will be a dictionary containing the names of the SMTP service extensions this server supports, and their parameters (if any).

Unless you wish to use before sending mail, it should not be necessary to call this method explicitly. It will be implicitly called by when necessary.

SMTP.ehlo_or_helo_if_needed()

This method calls and/or if there has been no previous `wasm32-wasi`6 or `wasm32-emscripten`4 command this session. It tries ESMTP `wasm32-wasi`6 first.

The server didn’t reply properly to the `wasm32-emscripten`4 greeting.

SMTP.has_extn(name)

Return if name is in the set of SMTP service extensions returned by the server, otherwise. Case is ignored.

SMTP.verify(address)

Check the validity of an address on this server using SMTP `connect()`4. Returns a tuple consisting of code 250 and a full RFC 822 address (including human name) if the user address is valid. Otherwise returns an SMTP error code of 400 or greater and an error string.

Note

Many sites disable SMTP `connect()`4 in order to foil spammers.

SMTP.login(user, password, *, initial_response_ok\=True)

Log in on an SMTP server that requires authentication. The arguments are the username and the password to authenticate with. If there has been no previous`wasm32-wasi`6 or `wasm32-emscripten`4 command this session, this method tries ESMTP `wasm32-wasi`6 first. This method will return normally if the authentication was successful, or may raise the following exceptions:

The server didn’t reply properly to the `wasm32-emscripten`4 greeting.

The server didn’t accept the username/password combination.

The `socket.getfqdn()`3 command is not supported by the server.

No suitable authentication method was found.

Each of the authentication methods supported by are tried in turn if they are advertised as supported by the server. See for a list of supported authentication methods. initial_response_ok is passed through to .

Optional keyword argument initial_response_ok specifies whether, for authentication methods that support it, an “initial response” as specified in RFC 4954 can be sent along with the `socket.getfqdn()`3 command, rather than requiring a challenge/response.

Changed in version 3.5: may be raised, and the initial_response_ok parameter was added.

SMTP.auth(mechanism, authobject, *, initial_response_ok\=True)

Issue an SMTP `socket.getfqdn()`3 command for the specified authentication mechanism, and handle the challenge response via authobject.

mechanism specifies which authentication mechanism is to be used as argument to the `socket.getfqdn()`3 command; the valid values are those listed in the `connect()`3 element of `SMTP`2.

authobject must be a callable object taking an optional single argument:

data = authobject(challenge=None)

If optional keyword argument initial_response_ok is true,`connect()`5 will be called first with no argument. It can return theRFC 4954 “initial response” ASCII `connect()`6 which will be encoded and sent with the `socket.getfqdn()`3 command as below. If the `connect()`5 does not support an initial response (e.g. because it requires a challenge), it should return`connect()`9 when called with

data = authobject(challenge=None)

00. If initial_response_ok is false, then `connect()`5 will not be called first with `connect()`9.

If the initial response check returns `connect()`9, or if initial_response_ok is false, `connect()`5 will be called to process the server’s challenge response; the challenge argument it is passed will be a

data = authobject(challenge=None)

05. It should return ASCII `connect()`6 data that will be base64 encoded and sent to the server.

The SMTP class provides

data = authobject(challenge=None)

08 for the

data = authobject(challenge=None)

09,

data = authobject(challenge=None)

10, and

data = authobject(challenge=None)

11 mechanisms; they are named

data = authobject(challenge=None)

12,

data = authobject(challenge=None)

13, and

data = authobject(challenge=None)

14 respectively. They all require that the

data = authobject(challenge=None)

15 and

data = authobject(challenge=None)

16 properties of the SMTP instance are set to appropriate values.

User code does not normally need to call `connect()`3 directly, but can instead call the method, which will try each of the above mechanisms in turn, in the order listed. `connect()`3 is exposed to facilitate the implementation of authentication methods not (or not yet) supported directly by .

New in version 3.5.

SMTP.starttls(*, context\=None)

Put the SMTP connection in TLS (Transport Layer Security) mode. All SMTP commands that follow will be encrypted. You should then call again.

If keyfile and certfile are provided, they are used to create an .

Optional context parameter is an object; This is an alternative to using a keyfile and a certfile and if specified both keyfile and certfile should be `connect()`9.

If there has been no previous `wasm32-wasi`6 or `wasm32-emscripten`4 command this session, this method tries ESMTP `wasm32-wasi`6 first.

Changed in version 3.12: The deprecated keyfile and certfile parameters have been removed.

The server didn’t reply properly to the `wasm32-emscripten`4 greeting.

The server does not support the STARTTLS extension.

SSL/TLS support is not available to your Python interpreter.

Changed in version 3.3: context was added.

Changed in version 3.4: The method now supports hostname check with

data = authobject(challenge=None)

33 and Server Name Indicator (see ).

Changed in version 3.5: The error raised for lack of STARTTLS support is now the subclass instead of the base .

SMTP.sendmail(from_addr, to_addrs, msg, mail_options\=(), rcpt_options\=())

Send mail. The required arguments are an RFC 822 from-address string, a list of RFC 822 to-address strings (a bare string will be treated as a list with 1 address), and a message string. The caller may pass a list of ESMTP options (such as

data = authobject(challenge=None)

  1. to be used in

data = authobject(challenge=None)

38 commands as mail_options. ESMTP options (such as

data = authobject(challenge=None)

39 commands) that should be used with all

data = authobject(challenge=None)

40 commands can be passed as rcpt_options. (If you need to use different ESMTP options to different recipients you have to use the low-level methods such as

data = authobject(challenge=None)

41,

data = authobject(challenge=None)

42 and

data = authobject(challenge=None)

43 to send the message.)

Note

The from_addr and to_addrs parameters are used to construct the message envelope used by the transport agents.

data = authobject(challenge=None)

44 does not modify the message headers in any way.

msg may be a string containing characters in the ASCII range, or a byte string. A string is encoded to bytes using the ascii codec, and lone

data = authobject(challenge=None)

45 and

data = authobject(challenge=None)

46 characters are converted to

data = authobject(challenge=None)

47 characters. A byte string is not modified.

If there has been no previous `wasm32-wasi`6 or `wasm32-emscripten`4 command this session, this method tries ESMTP `wasm32-wasi`6 first. If the server does ESMTP, message size and each of the specified options will be passed to it (if the option is in the feature set the server advertises). If `wasm32-wasi`6 fails, `wasm32-emscripten`4 will be tried and ESMTP options suppressed.

This method will return normally if the mail is accepted for at least one recipient. Otherwise it will raise an exception. That is, if this method does not raise an exception, then someone should get your mail. If this method does not raise an exception, 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.

If

data = authobject(challenge=None)

53 is included in mail_options, and the server supports it, from_addr and to_addrs may contain non-ASCII characters.

This method may raise the following exceptions:

All recipients were refused. Nobody got the mail. The `wasm32-emscripten`2 attribute of the exception object is a dictionary with information about the refused recipients (like the one returned when at least one recipient was accepted).

The server didn’t reply properly to the `wasm32-emscripten`4 greeting.

The server didn’t accept the from_addr.

The server replied with an unexpected error code (other than a refusal of a recipient).

data = authobject(challenge=None)

53 was given in the mail_options but is not supported by the server.

Unless otherwise noted, the connection will be open even after an exception is raised.

Changed in version 3.2: msg may be a byte string.

Changed in version 3.5:

data = authobject(challenge=None)

53 support added, and may be raised if

data = authobject(challenge=None)

53 is specified but the server does not support it.

SMTP.send_message(msg, from_addr\=None, to_addrs\=None, mail_options\=(), rcpt_options\=())

This is a convenience method for calling with the message represented by an object. The arguments have the same meaning as for , except that msg is a

data = authobject(challenge=None)

68 object.

If from_addr is `connect()`9 or to_addrs is `connect()`9,

data = authobject(challenge=None)

71 fills those arguments with addresses extracted from the headers of msg as specified in RFC 5322: from_addr is set to the field if it is present, and otherwise to the field. to_addrs combines the values (if any) of the , , and fields from msg. If exactly one set of headers appear in the message, the regular headers are ignored and the headers are used instead. If the message contains more than one set of headers, a is raised, since there is no way to unambiguously detect the most recent set of headers.

data = authobject(challenge=None)

71 serializes msg using with

data = authobject(challenge=None)

47 as the linesep, and calls to transmit the resulting message. Regardless of the values of from_addr and to_addrs,

data = authobject(challenge=None)

71 does not transmit any or headers that may appear in msg. If any of the addresses in from_addr and to_addrs contain non-ASCII characters and the server does not advertise

data = authobject(challenge=None)

53 support, an

data = authobject(challenge=None)

79 error is raised. Otherwise the

data = authobject(challenge=None)

68 is serialized with a clone of its with the attribute set to `wasm32-emscripten`6, and

data = authobject(challenge=None)

53 and

data = authobject(challenge=None)

85 are added to mail_options.

New in version 3.2.

New in version 3.5: Support for internationalized addresses (

data = authobject(challenge=None)

53).

SMTP.quit()

Terminate the SMTP session and close the connection. Return the result of the SMTP

data = authobject(challenge=None)

9 command.

Low-level methods corresponding to the standard SMTP/ESMTP commands

data = authobject(challenge=None)

88,

data = authobject(challenge=None)

89,

data = authobject(challenge=None)

90,

data = authobject(challenge=None)

91,

data = authobject(challenge=None)

40, and

data = authobject(challenge=None)

93 are also supported. Normally these do not need to be called directly, so they are not documented here. For details, consult the module code.