slimta.smtp.client

An SMTP client library that supports PIPELINING commands.

class slimta.smtp.client.Client(socket[, address=None])

Bases: object

Class whose methods perform various SMTP commands on a given socket. The return value from each command is a Reply object. Commands that are pipelined may not have their replies filled until subsequent commands are executed.

The extensions attribute contains the Extensions object that are made available by the server.

Parameters:
  • socket – Connected socket to use for the client.
  • address – Remote address associated with the socket, which will default to a call to getpeername().
last_error = None

Reply of the last error received from the server.

extensions = None

Extensions object of the client, populated once the EHLO command returns its response.

has_reply_waiting()

Checks if the underlying socket has data waiting to be received, which means a reply is waiting to be read.

Return type:True or False
get_reply([command=b'[TIMEOUT]'])

Gets a reply from the server that was not triggered by the client sending a command. This is most useful for receiving timeout notifications.

Parameters:command – Optional command name to associate with the reply.
Returns:Reply object populated with the response.
get_banner()

Waits for the SMTP banner at the beginning of the connection.

Returns:Reply object populated with the response.
custom_command(command[, arg=None])

Sends a custom command to the SMTP server and waits for the reply.

Parameters:
  • command – The command to send.
  • arg – Optonal argument string to send with the command.
Returns:

Reply object populated with the response.

ehlo(ehlo_as)

Sends the EHLO command with identifier string and waits for the reply. When this method returns, the self.extensions object will also be populated with the SMTP extensions the server supports.

Parameters:ehlo_as – EHLO identifier string, usually an FQDN.
Returns:Reply object populated with the response.
helo(helo_as)

Sends the HELO command with identifier string and waits for the reply.

Parameters:helo_as – HELO identifier string, usually an FQDN.
Returns:Reply object populated with the response.
encrypt([context=None])

Encrypts the underlying socket. This call should only be used directly against servers that expect to be immediately encrypted. If encryption is negotiated with starttls() there is no need to call this method.

Parameters:context (SSLContext) – Used to wrap sockets with SSL encryption, rather than the default context.
starttls([context=None])

Sends the STARTTLS command with identifier string and waits for the reply. When the reply is received and the code is 220, the socket encryption is negotiated. This should be followed by a another call to ehlo().

Parameters:context (SSLContext) – Used to wrap sockets with SSL encryption, rather than the default context.
Returns:Reply object populated with the response.
auth(authcid, secret[, authzid=None[, mechanism=None]])

Negotiates authentication for the current SMTP session. This transaction may involve several back-and-forth packets to the server, depending on the SASL mechanism used, and this function will only return once all have completed.

Parameters:
  • authcid – The authentication identity, usually the username.
  • secret – The secret (i.e. password) string to send for the given authentication and authorization identities.
  • authzid – The authorization identity, if applicable.
  • mechanism (str) – SASL mechanism name to use instead of the best available.
Returns:

Reply object populated with the response.

mailfrom(address[, data_size=None[, auth=None]])

Sends the MAIL command with the address and possibly the message size. The message size is sent if the server supports the SIZE extension. If the server does not support PIPELINING, the returned reply object is populated immediately.

Parameters:
  • address – The sender address to send.
  • data_size – Optional size of the message body.
  • auth (str) – Optionally indicates the message was originally submitted using the given authentication. Use False if authentication was missing or unknown.
Returns:

Reply object that will be populated with the response once a non-pipelined command is called, or if the server does not support PIPELINING.

rcptto(address)

Sends the RCPT command with the address. If the server does not support PIPELINING, the returned reply object is populated immediately.

Parameters:
  • address – The sender address to send.
  • data_size – Optional size of the message body.
Returns:

Reply object that will be populated with the response once a non-pipelined command is called, or if the server does not support PIPELINING.

data()

Sends the DATA command and waits for the response. If the response from the server is a 354, the server is respecting message data and should be sent send_data() or send_empty_data().

Returns:Reply object populated with the response.
send_data([*data])

Processes and sends message data. At the end of the message data, the client will send a line with a single . to indicate the end of the message. If the server does not support PIPELINING, the returned reply object is populated immediately.

Parameters:data (bytes) – The message data parts.
Returns:Reply object that will be populated with the response once a non-pipelined command is called, or if the server does not support PIPELINING.
send_empty_data()

Sends a line with a single . to indicate an empty message. If the server does not support PIPELINING, the returned reply object is populated immediately.

Returns:Reply object that will be populated with the response once a non-pipelined command is called, or if the server does not support PIPELINING.
rset()

Sends a RSET command and waits for the response. The intent of the RSET command is to reset any mail() or rcpt() commands that are pending.

Returns:Reply object populated with the response.
quit()

Sends the QUIT command and waits for the response. After the response is received (should be 221) the socket should be closed.

Returns:Reply object populated with the response.
class slimta.smtp.client.LmtpClient(socket[, address=None])

Bases: slimta.smtp.client.Client

This sub-class has been modified to implement the LMTP protocol instead. The first primary difference is the ehlo() and helo() are no longer allowed and have been replaced with lhlo(). The second primary difference is that the send_data() and send_empty_data() methods now return a list of tuples containing the address from a successful rcptto() and a Reply object. This list is in the same order as the calls to rcptto().

ehlo(ehlo_as)

Sends the EHLO command with identifier string and waits for the reply. When this method returns, the self.extensions object will also be populated with the SMTP extensions the server supports.

Parameters:ehlo_as – EHLO identifier string, usually an FQDN.
Returns:Reply object populated with the response.
helo(helo_as)

Sends the HELO command with identifier string and waits for the reply.

Parameters:helo_as – HELO identifier string, usually an FQDN.
Returns:Reply object populated with the response.
lhlo(lhlo_as)

Sends the LHLO command with identifier string and waits for the reply. When this method returns, the self.extensions object will also be populated with the SMTP extensions the server supports.

Parameters:lhlo_as – LHLO identifier string, usually an FQDN.
Returns:Reply object populated with the response.
rcptto(address)

Sends the RCPT command with the address. If the server does not support PIPELINING, the returned reply object is populated immediately.

Parameters:
  • address – The sender address to send.
  • data_size – Optional size of the message body.
Returns:

Reply object that will be populated with the response once a non-pipelined command is called, or if the server does not support PIPELINING.

send_data([*data])

Processes and sends message data. At the end of the message data, the client will send a line with a single . to indicate the end of the message. If the server does not support PIPELINING, the returned reply object is populated immediately.

Parameters:data (bytes) – The message data parts.
Returns:Reply object that will be populated with the response once a non-pipelined command is called, or if the server does not support PIPELINING.
send_empty_data()

Sends a line with a single . to indicate an empty message. If the server does not support PIPELINING, the returned reply object is populated immediately.

Returns:Reply object that will be populated with the response once a non-pipelined command is called, or if the server does not support PIPELINING.
rset()

Sends a RSET command and waits for the response. The intent of the RSET command is to reset any mail() or rcpt() commands that are pending.

Returns:Reply object populated with the response.