blob: 9ca5bbd7f2569454aedc7d71e49ec4c279aa587b [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
Martin v. Löwis48440b72003-10-31 12:52:35 +000016connection to a 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
Martin v. Löwis48440b72003-10-31 12:52:35 +000018optional command sets. Additionally, this module provides a class
19\class{POP3_SSL}, which provides support for connecting to POP3
20servers that use SSL as an underlying protocol layer.
21
Fred Drakea4684041998-04-24 20:49:02 +000022
Eric S. Raymond5ac97952001-01-11 04:19:52 +000023Note that POP3, though widely supported, is obsolescent. The
24implementation quality of POP3 servers varies widely, and too many are
25quite poor. If your mailserver supports IMAP, you would be better off
Georg Brandl4e9165d2007-04-01 21:29:15 +000026using the \class{\refmodule{imaplib}.IMAP4} class, as IMAP
Fred Drakea7c9ac62001-05-09 03:49:48 +000027servers tend to be better implemented.
Eric S. Raymond5ac97952001-01-11 04:19:52 +000028
Fred Drakea4684041998-04-24 20:49:02 +000029A single class is provided by the \module{poplib} module:
30
Facundo Batista1b1c3472007-03-27 18:23:21 +000031\begin{classdesc}{POP3}{host\optional{, port\optional{, timeout}}}
Fred Drakea4684041998-04-24 20:49:02 +000032This class implements the actual POP3 protocol. The connection is
33created when the instance is initialized.
34If \var{port} is omitted, the standard POP3 port (110) is used.
Facundo Batista1b1c3472007-03-27 18:23:21 +000035The optional \var{timeout} parameter specifies a timeout in seconds for the
36connection attempt (if not specified, or passed as None, the global default
37timeout setting will be used).
Facundo Batista3ed365b2007-06-11 16:27:08 +000038
39\versionchanged[\var{timeout} was added]{2.6}
Fred Drakea4684041998-04-24 20:49:02 +000040\end{classdesc}
41
Martin v. Löwis48440b72003-10-31 12:52:35 +000042\begin{classdesc}{POP3_SSL}{host\optional{, port\optional{, keyfile\optional{, certfile}}}}
43This is a subclass of \class{POP3} that connects to the server over an
44SSL encrypted socket. If \var{port} is not specified, 995, the
45standard POP3-over-SSL port is used. \var{keyfile} and \var{certfile}
46are also optional - they can contain a PEM formatted private key and
47certificate chain file for the SSL connection.
48
49\versionadded{2.4}
50\end{classdesc}
51
Fred Drakee2effbd1999-04-22 16:21:09 +000052One exception is defined as an attribute of the \module{poplib} module:
Fred Drakea4684041998-04-24 20:49:02 +000053
54\begin{excdesc}{error_proto}
Facundo Batistac3a35e12007-04-03 14:05:08 +000055Exception raised on any errors from this module (errors from
56\module{socket} module are not caught). The reason for the exception
57is passed to the constructor as a string.
Fred Drakea4684041998-04-24 20:49:02 +000058\end{excdesc}
59
Fred Drakea7c9ac62001-05-09 03:49:48 +000060\begin{seealso}
61 \seemodule{imaplib}{The standard Python IMAP module.}
Andrew M. Kuchling44a98232004-01-11 23:00:16 +000062 \seetitle[http://www.catb.org/\~{}esr/fetchmail/fetchmail-FAQ.html]
Fred Draked8eeeae2002-10-18 16:50:17 +000063 {Frequently Asked Questions About Fetchmail}
64 {The FAQ for the \program{fetchmail} POP/IMAP client collects
65 information on POP3 server variations and RFC noncompliance
66 that may be useful if you need to write an application based
67 on the POP protocol.}
Fred Drakea7c9ac62001-05-09 03:49:48 +000068\end{seealso}
69
Fred Drakea4684041998-04-24 20:49:02 +000070
Fred Drakee2effbd1999-04-22 16:21:09 +000071\subsection{POP3 Objects \label{pop3-objects}}
Fred Drakea4684041998-04-24 20:49:02 +000072
73All POP3 commands are represented by methods of the same name,
Fred Drakee2effbd1999-04-22 16:21:09 +000074in lower-case; most return the response text sent by the server.
Fred Drakea4684041998-04-24 20:49:02 +000075
76An \class{POP3} instance has the following methods:
77
78
Georg Brandlae91afd2007-04-01 22:39:10 +000079\begin{methoddesc}[POP3]{set_debuglevel}{level}
Fred Drakea16433b2001-12-05 22:37:21 +000080Set the instance's debugging level. This controls the amount of
81debugging output printed. The default, \code{0}, produces no
82debugging output. A value of \code{1} produces a moderate amount of
83debugging output, generally a single line per request. A value of
84\code{2} or higher produces the maximum amount of debugging output,
85logging each line sent and received on the control connection.
86\end{methoddesc}
87
Georg Brandlae91afd2007-04-01 22:39:10 +000088\begin{methoddesc}[POP3]{getwelcome}{}
Fred Drakea4684041998-04-24 20:49:02 +000089Returns the greeting string sent by the POP3 server.
90\end{methoddesc}
91
Georg Brandlae91afd2007-04-01 22:39:10 +000092\begin{methoddesc}[POP3]{user}{username}
Thomas Woutersf8316632000-07-16 19:01:10 +000093Send user command, response should indicate that a password is required.
Fred Drakea4684041998-04-24 20:49:02 +000094\end{methoddesc}
95
Georg Brandlae91afd2007-04-01 22:39:10 +000096\begin{methoddesc}[POP3]{pass_}{password}
Fred Drakea4684041998-04-24 20:49:02 +000097Send password, response includes message count and mailbox size.
98Note: the mailbox on the server is locked until \method{quit()} is
99called.
100\end{methoddesc}
101
Georg Brandlae91afd2007-04-01 22:39:10 +0000102\begin{methoddesc}[POP3]{apop}{user, secret}
Fred Drakea4684041998-04-24 20:49:02 +0000103Use the more secure APOP authentication to log into the POP3 server.
104\end{methoddesc}
105
Georg Brandlae91afd2007-04-01 22:39:10 +0000106\begin{methoddesc}[POP3]{rpop}{user}
Fred Drakea4684041998-04-24 20:49:02 +0000107Use RPOP authentication (similar to UNIX r-commands) to log into POP3 server.
108\end{methoddesc}
109
Georg Brandlae91afd2007-04-01 22:39:10 +0000110\begin{methoddesc}[POP3]{stat}{}
Fred Drakea4684041998-04-24 20:49:02 +0000111Get mailbox status. The result is a tuple of 2 integers:
112\code{(\var{message count}, \var{mailbox size})}.
113\end{methoddesc}
114
Georg Brandlae91afd2007-04-01 22:39:10 +0000115\begin{methoddesc}[POP3]{list}{\optional{which}}
Fred Drakea4684041998-04-24 20:49:02 +0000116Request message list, result is in the form
Georg Brandl2772c672005-08-05 21:01:58 +0000117\code{(\var{response}, ['mesg_num octets', ...], \var{octets})}.
118If \var{which} is set, it is the message to list.
Fred Drakea4684041998-04-24 20:49:02 +0000119\end{methoddesc}
120
Georg Brandlae91afd2007-04-01 22:39:10 +0000121\begin{methoddesc}[POP3]{retr}{which}
Eric S. Raymond5ac97952001-01-11 04:19:52 +0000122Retrieve whole message number \var{which}, and set its seen flag.
123Result is in form \code{(\var{response}, ['line', ...], \var{octets})}.
Fred Drakea4684041998-04-24 20:49:02 +0000124\end{methoddesc}
125
Georg Brandlae91afd2007-04-01 22:39:10 +0000126\begin{methoddesc}[POP3]{dele}{which}
Eric S. Raymond5ac97952001-01-11 04:19:52 +0000127Flag message number \var{which} for deletion. On most servers
128deletions are not actually performed until QUIT (the major exception is
Fred Draked8eeeae2002-10-18 16:50:17 +0000129Eudora QPOP, which deliberately violates the RFCs by doing pending
Eric S. Raymond5ac97952001-01-11 04:19:52 +0000130deletes on any disconnect).
Fred Drakea4684041998-04-24 20:49:02 +0000131\end{methoddesc}
132
Georg Brandlae91afd2007-04-01 22:39:10 +0000133\begin{methoddesc}[POP3]{rset}{}
Fred Drakea4684041998-04-24 20:49:02 +0000134Remove any deletion marks for the mailbox.
135\end{methoddesc}
136
Georg Brandlae91afd2007-04-01 22:39:10 +0000137\begin{methoddesc}[POP3]{noop}{}
Fred Drakea4684041998-04-24 20:49:02 +0000138Do nothing. Might be used as a keep-alive.
139\end{methoddesc}
140
Georg Brandlae91afd2007-04-01 22:39:10 +0000141\begin{methoddesc}[POP3]{quit}{}
Fred Drakea4684041998-04-24 20:49:02 +0000142Signoff: commit changes, unlock mailbox, drop connection.
143\end{methoddesc}
144
Georg Brandlae91afd2007-04-01 22:39:10 +0000145\begin{methoddesc}[POP3]{top}{which, howmuch}
Fred Drakea4684041998-04-24 20:49:02 +0000146Retrieves the message header plus \var{howmuch} lines of the message
Fred Draked8eeeae2002-10-18 16:50:17 +0000147after the header of message number \var{which}. Result is in form
Fred Drake3a4ceb71999-07-07 14:04:38 +0000148\code{(\var{response}, ['line', ...], \var{octets})}.
Eric S. Raymond5ac97952001-01-11 04:19:52 +0000149
150The POP3 TOP command this method uses, unlike the RETR command,
151doesn't set the message's seen flag; unfortunately, TOP is poorly
152specified in the RFCs and is frequently broken in off-brand servers.
153Test this method by hand against the POP3 servers you will use before
154trusting it.
Fred Drakea4684041998-04-24 20:49:02 +0000155\end{methoddesc}
156
Georg Brandlae91afd2007-04-01 22:39:10 +0000157\begin{methoddesc}[POP3]{uidl}{\optional{which}}
Fred Drakea4684041998-04-24 20:49:02 +0000158Return message digest (unique id) list.
Fred Drakedbc2d081999-05-13 18:48:14 +0000159If \var{which} is specified, result contains the unique id for that
160message in the form \code{'\var{response}\ \var{mesgnum}\ \var{uid}},
Fred Drake3a4ceb71999-07-07 14:04:38 +0000161otherwise result is list \code{(\var{response}, ['mesgnum uid', ...],
162\var{octets})}.
Fred Drakea4684041998-04-24 20:49:02 +0000163\end{methoddesc}
164
Martin v. Löwis48440b72003-10-31 12:52:35 +0000165Instances of \class{POP3_SSL} have no additional methods. The
166interface of this subclass is identical to its parent.
167
Fred Drakea4684041998-04-24 20:49:02 +0000168
Fred Drakee2effbd1999-04-22 16:21:09 +0000169\subsection{POP3 Example \label{pop3-example}}
Fred Drakea4684041998-04-24 20:49:02 +0000170
171Here is a minimal example (without error checking) that opens a
172mailbox and retrieves and prints all messages:
173
174\begin{verbatim}
Guido van Rossumaac399b1998-12-08 16:30:10 +0000175import getpass, poplib
Fred Drakea4684041998-04-24 20:49:02 +0000176
177M = poplib.POP3('localhost')
178M.user(getpass.getuser())
Guido van Rossumaac399b1998-12-08 16:30:10 +0000179M.pass_(getpass.getpass())
Fred Drakea4684041998-04-24 20:49:02 +0000180numMessages = len(M.list()[1])
181for i in range(numMessages):
182 for j in M.retr(i+1)[1]:
Guido van Rossumaac399b1998-12-08 16:30:10 +0000183 print j
Fred Drakea4684041998-04-24 20:49:02 +0000184\end{verbatim}
185
186At the end of the module, there is a test section that contains a more
187extensive example of usage.