# 11.9 `smtplib` -- SMTP protocol client

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).

`SMTP` ([host[, port]])
  A `SMTP` 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 `connect()` method is called with those parameters during initialization. An `SMTPConnectError` is raised if the specified host doesn't respond correctly.

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

A nice selection of exceptions is defined as well:

`SMTPException`
  Base exception class for all exceptions raised by this module.

`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.

`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 `smtp_code` attribute of the error, and the `smtp_error` attribute is set to the error message.

`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.

`SMTPRecipientsRefused`
  All recipient addresses refused. The errors for each recipient are accessable through the attribute `recipients`, which is a dictionary of exactly the same sort as `SMTP.sendmail()` returns.

`SMTPDataError`
  The SMTP server refused to accept the message data.

`SMTPConnectError`
  Error occurred during establishment of a connection with the server.

`SMTPHeloError`
  The server refused our "`HELO`" message.

See Also:

Internet RFC 821, Simple Mail Transfer Protocol. Available online at http://info.internet.isi.edu/in-notes/rfc/files/rfc821.txt.

Internet RFC 1869, SMTP Service Extensions. Available online at http://info.internet.isi.edu/in-notes/rfc/files/rfc1869.txt.

- 11.9.1 SMTP Objects
- 11.9.2 SMTP Example