Updated for 2.1b2 distribution.
[python/dscho.git] / Doc / lib / libpoplib.tex
blob89e7e0c8419bb7bfce83a35baeb2173dee65fe6c
1 \section{\module{poplib} ---
2 POP3 protocol client}
4 \declaremodule{standard}{poplib}
5 \modulesynopsis{POP3 protocol client (requires sockets).}
7 %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''.
11 %Revised by ESR, January 2000
13 \indexii{POP3}{protocol}
15 This module defines a class, \class{POP3}, which encapsulates a
16 connection to an POP3 server and implements protocol as defined in
17 \rfc{1725}. The \class{POP3} class supports both the minimal and
18 optional command sets.
20 Note that POP3, though widely supported, is obsolescent. The
21 implementation quality of POP3 servers varies widely, and too many are
22 quite poor. If your mailserver supports IMAP, you would be better off
23 using the \refmodule{IMAP} class, as IMAP servers tend to be better
24 implemented.
26 A single class is provided by the \module{poplib} module:
28 \begin{classdesc}{POP3}{host\optional{, port}}
29 This class implements the actual POP3 protocol. The connection is
30 created when the instance is initialized.
31 If \var{port} is omitted, the standard POP3 port (110) is used.
32 \end{classdesc}
34 One exception is defined as an attribute of the \module{poplib} module:
36 \begin{excdesc}{error_proto}
37 Exception raised on any errors. The reason for the exception is
38 passed to the constructor as a string.
39 \end{excdesc}
42 \subsection{POP3 Objects \label{pop3-objects}}
44 All POP3 commands are represented by methods of the same name,
45 in lower-case; most return the response text sent by the server.
47 An \class{POP3} instance has the following methods:
50 \begin{methoddesc}{getwelcome}{}
51 Returns the greeting string sent by the POP3 server.
52 \end{methoddesc}
55 \begin{methoddesc}{user}{username}
56 Send user command, response should indicate that a password is required.
57 \end{methoddesc}
59 \begin{methoddesc}{pass_}{password}
60 Send password, response includes message count and mailbox size.
61 Note: the mailbox on the server is locked until \method{quit()} is
62 called.
63 \end{methoddesc}
65 \begin{methoddesc}{apop}{user, secret}
66 Use the more secure APOP authentication to log into the POP3 server.
67 \end{methoddesc}
69 \begin{methoddesc}{rpop}{user}
70 Use RPOP authentication (similar to UNIX r-commands) to log into POP3 server.
71 \end{methoddesc}
73 \begin{methoddesc}{stat}{}
74 Get mailbox status. The result is a tuple of 2 integers:
75 \code{(\var{message count}, \var{mailbox size})}.
76 \end{methoddesc}
78 \begin{methoddesc}{list}{\optional{which}}
79 Request message list, result is in the form
80 \code{(\var{response}, ['mesg_num octets', ...])}. If \var{which} is
81 set, it is the message to list.
82 \end{methoddesc}
84 \begin{methoddesc}{retr}{which}
85 Retrieve whole message number \var{which}, and set its seen flag.
86 Result is in form \code{(\var{response}, ['line', ...], \var{octets})}.
87 \end{methoddesc}
89 \begin{methoddesc}{dele}{which}
90 Flag message number \var{which} for deletion. On most servers
91 deletions are not actually performed until QUIT (the major exception is
92 Eudora QPOP, which deliberately violates the RFCs by doing pending
93 deletes on any disconnect).
94 \end{methoddesc}
96 \begin{methoddesc}{rset}{}
97 Remove any deletion marks for the mailbox.
98 \end{methoddesc}
100 \begin{methoddesc}{noop}{}
101 Do nothing. Might be used as a keep-alive.
102 \end{methoddesc}
104 \begin{methoddesc}{quit}{}
105 Signoff: commit changes, unlock mailbox, drop connection.
106 \end{methoddesc}
108 \begin{methoddesc}{top}{which, howmuch}
109 Retrieves the message header plus \var{howmuch} lines of the message
110 after the header of message number \var{which}. Result is in form
111 \code{(\var{response}, ['line', ...], \var{octets})}.
113 The POP3 TOP command this method uses, unlike the RETR command,
114 doesn't set the message's seen flag; unfortunately, TOP is poorly
115 specified in the RFCs and is frequently broken in off-brand servers.
116 Test this method by hand against the POP3 servers you will use before
117 trusting it.
118 \end{methoddesc}
120 \begin{methoddesc}{uidl}{\optional{which}}
121 Return message digest (unique id) list.
122 If \var{which} is specified, result contains the unique id for that
123 message in the form \code{'\var{response}\ \var{mesgnum}\ \var{uid}},
124 otherwise result is list \code{(\var{response}, ['mesgnum uid', ...],
125 \var{octets})}.
126 \end{methoddesc}
128 \begin{seealso}
129 \seemodule{imap}{The standard Python IMAP module.}
130 \seetitle{http://www.tuxedo.org/~esr/fetchail/fetchmail-FAQ.html}{
131 The FAQ for the fetchmail POP/IMAP client collects information
132 on POP3 server variations and RFC noncompliance that may be
133 useful if you need to write an application based on poplib.}
134 \end{seealso}
136 \subsection{POP3 Example \label{pop3-example}}
138 Here is a minimal example (without error checking) that opens a
139 mailbox and retrieves and prints all messages:
141 \begin{verbatim}
142 import getpass, poplib
144 M = poplib.POP3('localhost')
145 M.user(getpass.getuser())
146 M.pass_(getpass.getpass())
147 numMessages = len(M.list()[1])
148 for i in range(numMessages):
149 for j in M.retr(i+1)[1]:
150 print j
151 \end{verbatim}
153 At the end of the module, there is a test section that contains a more
154 extensive example of usage.