blob: 3c18bd1a3142fea3e1f5b34a2fe391bb54a3f5c2 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{poplib} ---
Fred Drakee2effbd1999-04-22 16:21:09 +00002 POP3 protocol client}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drakee2effbd1999-04-22 16:21:09 +00004\declaremodule{standard}{poplib}
Fred Drakeb91e9341998-07-23 17:59:49 +00005\modulesynopsis{POP3 protocol client (requires sockets).}
6
Fred Drake38e5d272000-04-03 20:13:55 +00007%By Andrew T. Csillag
8%Even though I put it into LaTeX, I cannot really claim that I wrote
9%it since I just stole most of it from the poplib.py source code and
10%the imaplib ``chapter''.
Eric S. Raymond5ac97952001-01-11 04:19:52 +000011%Revised by ESR, January 2000
Fred Drake38e5d272000-04-03 20:13:55 +000012
Fred Drakea4684041998-04-24 20:49:02 +000013\indexii{POP3}{protocol}
14
15This module defines a class, \class{POP3}, which encapsulates a
Fred Drake280f7252001-08-14 11:42:13 +000016connection to an POP3 server and implements the protocol as defined in
Thomas Woutersf8316632000-07-16 19:01:10 +000017\rfc{1725}. The \class{POP3} class supports both the minimal and
Fred Drakea4684041998-04-24 20:49:02 +000018optional command sets.
19
Eric S. Raymond5ac97952001-01-11 04:19:52 +000020Note that POP3, though widely supported, is obsolescent. The
21implementation quality of POP3 servers varies widely, and too many are
22quite poor. If your mailserver supports IMAP, you would be better off
Fred Drakea7c9ac62001-05-09 03:49:48 +000023using the \code{\refmodule{imaplib}.\class{IMAP4}} class, as IMAP
24servers tend to be better implemented.
Eric S. Raymond5ac97952001-01-11 04:19:52 +000025
Fred Drakea4684041998-04-24 20:49:02 +000026A single class is provided by the \module{poplib} module:
27
28\begin{classdesc}{POP3}{host\optional{, port}}
29This class implements the actual POP3 protocol. The connection is
30created when the instance is initialized.
31If \var{port} is omitted, the standard POP3 port (110) is used.
32\end{classdesc}
33
Fred Drakee2effbd1999-04-22 16:21:09 +000034One exception is defined as an attribute of the \module{poplib} module:
Fred Drakea4684041998-04-24 20:49:02 +000035
36\begin{excdesc}{error_proto}
37Exception raised on any errors. The reason for the exception is
38passed to the constructor as a string.
39\end{excdesc}
40
Fred Drakea7c9ac62001-05-09 03:49:48 +000041\begin{seealso}
42 \seemodule{imaplib}{The standard Python IMAP module.}
43 \seetitle{http://www.tuxedo.org/~esr/fetchail/fetchmail-FAQ.html}{
44 The FAQ for the fetchmail POP/IMAP client collects information
45 on POP3 server variations and RFC noncompliance that may be
46 useful if you need to write an application based on poplib.}
47\end{seealso}
48
Fred Drakea4684041998-04-24 20:49:02 +000049
Fred Drakee2effbd1999-04-22 16:21:09 +000050\subsection{POP3 Objects \label{pop3-objects}}
Fred Drakea4684041998-04-24 20:49:02 +000051
52All POP3 commands are represented by methods of the same name,
Fred Drakee2effbd1999-04-22 16:21:09 +000053in lower-case; most return the response text sent by the server.
Fred Drakea4684041998-04-24 20:49:02 +000054
55An \class{POP3} instance has the following methods:
56
57
Fred Drakea16433b2001-12-05 22:37:21 +000058\begin{methoddesc}{set_debuglevel}{level}
59Set the instance's debugging level. This controls the amount of
60debugging output printed. The default, \code{0}, produces no
61debugging output. A value of \code{1} produces a moderate amount of
62debugging output, generally a single line per request. A value of
63\code{2} or higher produces the maximum amount of debugging output,
64logging each line sent and received on the control connection.
65\end{methoddesc}
66
Fred Drakea4684041998-04-24 20:49:02 +000067\begin{methoddesc}{getwelcome}{}
68Returns the greeting string sent by the POP3 server.
69\end{methoddesc}
70
Fred Drakea4684041998-04-24 20:49:02 +000071\begin{methoddesc}{user}{username}
Thomas Woutersf8316632000-07-16 19:01:10 +000072Send user command, response should indicate that a password is required.
Fred Drakea4684041998-04-24 20:49:02 +000073\end{methoddesc}
74
75\begin{methoddesc}{pass_}{password}
76Send password, response includes message count and mailbox size.
77Note: the mailbox on the server is locked until \method{quit()} is
78called.
79\end{methoddesc}
80
81\begin{methoddesc}{apop}{user, secret}
82Use the more secure APOP authentication to log into the POP3 server.
83\end{methoddesc}
84
85\begin{methoddesc}{rpop}{user}
86Use RPOP authentication (similar to UNIX r-commands) to log into POP3 server.
87\end{methoddesc}
88
89\begin{methoddesc}{stat}{}
90Get mailbox status. The result is a tuple of 2 integers:
91\code{(\var{message count}, \var{mailbox size})}.
92\end{methoddesc}
93
94\begin{methoddesc}{list}{\optional{which}}
95Request message list, result is in the form
Fred Drake3a4ceb71999-07-07 14:04:38 +000096\code{(\var{response}, ['mesg_num octets', ...])}. If \var{which} is
Fred Drakea4684041998-04-24 20:49:02 +000097set, it is the message to list.
98\end{methoddesc}
99
100\begin{methoddesc}{retr}{which}
Eric S. Raymond5ac97952001-01-11 04:19:52 +0000101Retrieve whole message number \var{which}, and set its seen flag.
102Result is in form \code{(\var{response}, ['line', ...], \var{octets})}.
Fred Drakea4684041998-04-24 20:49:02 +0000103\end{methoddesc}
104
105\begin{methoddesc}{dele}{which}
Eric S. Raymond5ac97952001-01-11 04:19:52 +0000106Flag message number \var{which} for deletion. On most servers
107deletions are not actually performed until QUIT (the major exception is
108Eudora QPOP, which deliberately violates the RFCs by doing pending
109deletes on any disconnect).
Fred Drakea4684041998-04-24 20:49:02 +0000110\end{methoddesc}
111
112\begin{methoddesc}{rset}{}
113Remove any deletion marks for the mailbox.
114\end{methoddesc}
115
116\begin{methoddesc}{noop}{}
117Do nothing. Might be used as a keep-alive.
118\end{methoddesc}
119
120\begin{methoddesc}{quit}{}
121Signoff: commit changes, unlock mailbox, drop connection.
122\end{methoddesc}
123
124\begin{methoddesc}{top}{which, howmuch}
125Retrieves the message header plus \var{howmuch} lines of the message
126after the header of message number \var{which}. Result is in form
Fred Drake3a4ceb71999-07-07 14:04:38 +0000127\code{(\var{response}, ['line', ...], \var{octets})}.
Eric S. Raymond5ac97952001-01-11 04:19:52 +0000128
129The POP3 TOP command this method uses, unlike the RETR command,
130doesn't set the message's seen flag; unfortunately, TOP is poorly
131specified in the RFCs and is frequently broken in off-brand servers.
132Test this method by hand against the POP3 servers you will use before
133trusting it.
Fred Drakea4684041998-04-24 20:49:02 +0000134\end{methoddesc}
135
136\begin{methoddesc}{uidl}{\optional{which}}
137Return message digest (unique id) list.
Fred Drakedbc2d081999-05-13 18:48:14 +0000138If \var{which} is specified, result contains the unique id for that
139message in the form \code{'\var{response}\ \var{mesgnum}\ \var{uid}},
Fred Drake3a4ceb71999-07-07 14:04:38 +0000140otherwise result is list \code{(\var{response}, ['mesgnum uid', ...],
141\var{octets})}.
Fred Drakea4684041998-04-24 20:49:02 +0000142\end{methoddesc}
143
144
Fred Drakee2effbd1999-04-22 16:21:09 +0000145\subsection{POP3 Example \label{pop3-example}}
Fred Drakea4684041998-04-24 20:49:02 +0000146
147Here is a minimal example (without error checking) that opens a
148mailbox and retrieves and prints all messages:
149
150\begin{verbatim}
Guido van Rossumaac399b1998-12-08 16:30:10 +0000151import getpass, poplib
Fred Drakea4684041998-04-24 20:49:02 +0000152
153M = poplib.POP3('localhost')
154M.user(getpass.getuser())
Guido van Rossumaac399b1998-12-08 16:30:10 +0000155M.pass_(getpass.getpass())
Fred Drakea4684041998-04-24 20:49:02 +0000156numMessages = len(M.list()[1])
157for i in range(numMessages):
158 for j in M.retr(i+1)[1]:
Guido van Rossumaac399b1998-12-08 16:30:10 +0000159 print j
Fred Drakea4684041998-04-24 20:49:02 +0000160\end{verbatim}
161
162At the end of the module, there is a test section that contains a more
163extensive example of usage.