On this page
poplib
Prev: plistlib | Up: stdlib_module | Next: posix
Rel: email, json, mailbox, mimetypes, base64
Ref: docs.python.org — poplib
Tags: #public #python3 #stdlib
Import
import poplib
Overview
Low-level POP3 client (POP3, POP3_SSL) for retrieving and deleting messages from a POP3 mail drop. Use poplib only when integrating with legacy mail servers or simple single-inbox queues that don't offer IMAP or an HTTP API.
Quick Start
import email
from email import policy
import poplib
def read_pop3_inbox(host: str, user: str, credential: str) -> list[str]:
subjects: list[str] = []
conn = poplib.POP3_SSL(host, 995, timeout=30)
try:
conn.user(user)
conn.pass_(credential)
count, _size = conn.stat()
for num in range(1, count + 1):
_resp, lines, _octets = conn.retr(num)
msg = email.message_from_bytes(b"\r\n".join(lines), policy=policy.default)
subjects.append(msg["Subject"] or "")
finally:
conn.quit()
return subjects
Implementation Walkthrough
1. Peeking at Headers Without Downloading Full Bodies
top(which, howmuch) fetches message headers plus howmuch body lines—useful for filtering large attachments before calling retr():
import poplib
def peek_headers(conn: poplib.POP3_SSL, msg_num: int) -> bytes:
_resp, lines, _octets = conn.top(msg_num, 0)
return b"\r\n".join(lines)
Gotchas & Boundaries
- Deletions commit only on
quit(): Callingconn.dele(i)marks a message for deletion, but the server only removes it afterconn.quit()succeeds. POP3is not a context manager: Always wrap sessions intry ... finally: conn.quit()to avoid leaving the mailbox locked on the server.- Use
POP3_SSL(port 995) orstls(): PlainPOP3sends credentials in cleartext. - Line-length guard:
poplib._MAXLINEcaps response line length (default 20480) to defend against memory exhaustion from malformed servers.
Classes & Objects
- poplib.error_proto —
poplib.error_protoclass. (Methods:.add_note(),.with_traceback()) - poplib.POP3 — This class supports both the minimal and optional command sets. (Methods:
.apop(),.capa(),.close(),.dele(),.getwelcome(),.list()) - poplib.POP3_SSL — POP3 client class over SSL connection Instantiate with: POP3_SSL(hostname, port=995, context=None) hostname - the hostname of the pop3 over ssl server (Methods:
.apop(),.capa(),.close(),.dele(),.getwelcome(),.list())
Linked from
Related notes
- tuts/python3/stdlib_module/plistlib Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/poplib/error_proto Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/poplib/POP3 Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/poplib/POP3_SSL Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/posix Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/base64 Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/email Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/json Linked here; shared tags: #stdlib