On this page
smtplib
Prev: site | Up: stdlib_module | Next: socket
Rel: email, json, mailbox, mimetypes, base64
Ref: docs.python.org — smtplib
Tags: #public #python3 #stdlib
Import
import smtplib
Overview
SMTP and ESMTP client (SMTP, SMTP_SSL) for transmitting email.message.EmailMessage objects (built with email) over TLS-encrypted mail relays. Always enforce TLS (SMTP_SSL or starttls(context=ssl.create_default_context())) and explicit timeouts.
Quick Start
from email.message import EmailMessage
import smtplib
import ssl
msg = EmailMessage()
msg["Subject"] = "Status"
msg["From"] = "noreply@example.org"
msg["To"] = "user@example.org"
msg.set_content("All checks passed.")
ctx = ssl.create_default_context()
assert hasattr(smtplib, "SMTP_SSL") and ctx.check_hostname is True
Implementation Walkthrough
1. Sending an Email over STARTTLS (Port 587) or Implicit TLS (Port 465)
from email.message import EmailMessage
import smtplib
import ssl
def send_mail(host: str, port: int, user: str, password: str, msg: EmailMessage) -> None:
ctx = ssl.create_default_context()
with smtplib.SMTP(host, port, timeout=15) as server:
server.ehlo()
server.starttls(context=ctx)
server.ehlo()
server.login(user, password)
server.send_message(msg)
Gotchas & Boundaries
- Prefer
server.send_message(msg)oversendmail():send_message()extracts headers, handles UTF-8 encoding, and formats MIME boundaries cleanly. - Always pass
context=ssl.create_default_context()tostarttls()orSMTP_SSL()so server certificates and hostnames are verified. - Always set
timeout=: SMTP servers frequently stall or tar-pit connections.
Common Functions
- smtplib.quoteaddr() —
quoteaddr(addrstring)— Quote a subset of the email addresses defined by RFC 821. - smtplib.quotedata() —
quotedata(data)— Quote data for email.
Classes & Objects
- smtplib.SMTP — This class manages a connection to an SMTP or ESMTP server. (Methods:
.auth(),.auth_cram_md5(),.auth_login(),.auth_plain(),.close(),.connect()) - smtplib.SMTP_SSL — This is a subclass derived from SMTP that connects over an SSL encrypted socket (to use this class you need a socket module that was compiled with SSL support). If host is not specified, '' (the local (Methods:
.auth(),.auth_cram_md5(),.auth_login(),.auth_plain(),.close(),.connect()) - smtplib.SMTPAuthenticationError — Authentication error. (Methods:
.add_note(),.with_traceback()) - smtplib.SMTPConnectError — Error during connection establishment. (Methods:
.add_note(),.with_traceback()) - smtplib.SMTPDataError — The SMTP server didn't accept the data. (Methods:
.add_note(),.with_traceback()) - smtplib.SMTPException — Base class for all exceptions raised by this module. (Methods:
.add_note(),.with_traceback()) - smtplib.SMTPHeloError — The server refused our HELO reply. (Methods:
.add_note(),.with_traceback()) - smtplib.SMTPNotSupportedError — The command or option is not supported by the SMTP server. (Methods:
.add_note(),.with_traceback()) - smtplib.SMTPRecipientsRefused — All recipient addresses refused. (Methods:
.add_note(),.with_traceback()) - smtplib.SMTPResponseException — Base class for all exceptions that include an SMTP error code. (Methods:
.add_note(),.with_traceback()) - smtplib.SMTPSenderRefused — Sender address refused. (Methods:
.add_note(),.with_traceback()) - smtplib.SMTPServerDisconnected — Not connected to any SMTP server. (Methods:
.add_note(),.with_traceback())
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/site
- tuts/python3/stdlib_module/smtplib/quoteaddr
- tuts/python3/stdlib_module/smtplib/quotedata
- tuts/python3/stdlib_module/smtplib/SMTP
- tuts/python3/stdlib_module/smtplib/SMTP_SSL
- tuts/python3/stdlib_module/smtplib/SMTPAuthenticationError
- tuts/python3/stdlib_module/smtplib/SMTPConnectError
- tuts/python3/stdlib_module/smtplib/SMTPDataError
- tuts/python3/stdlib_module/smtplib/SMTPException
- tuts/python3/stdlib_module/smtplib/SMTPHeloError
- tuts/python3/stdlib_module/smtplib/SMTPNotSupportedError
- tuts/python3/stdlib_module/smtplib/SMTPRecipientsRefused
- tuts/python3/stdlib_module/smtplib/SMTPResponseException
- tuts/python3/stdlib_module/smtplib/SMTPSenderRefused
- tuts/python3/stdlib_module/smtplib/SMTPServerDisconnected
- tuts/python3/stdlib_module/socket
- tuts/python3/stdlib_module/stdlib_module
Related notes
- tuts/python3/stdlib_module/site Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/smtplib/quoteaddr Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/smtplib/quotedata Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/smtplib/SMTP Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/smtplib/SMTP_SSL Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/smtplib/SMTPAuthenticationError Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/smtplib/SMTPConnectError Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/smtplib/SMTPDataError Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/smtplib/SMTPException Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/smtplib/SMTPHeloError Linked here; links to this page; shared tags: #stdlib