summaryrefslogtreecommitdiffstats
path: root/Doc/lib/libpoplib.tex
blob: 7f3af4c79c8c398f41ca212ed182c986232ce81e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
\section{\module{poplib} ---
         POP3 protocol client}

\declaremodule{standard}{poplib}
\modulesynopsis{POP3 protocol client (requires sockets).}

%By Andrew T. Csillag
%Even though I put it into LaTeX, I cannot really claim that I wrote
%it since I just stole most of it from the poplib.py source code and
%the imaplib ``chapter''.
%Revised by ESR, January 2000

\indexii{POP3}{protocol}

This module defines a class, \class{POP3}, which encapsulates a
connection to an POP3 server and implements the protocol as defined in
\rfc{1725}.  The \class{POP3} class supports both the minimal and
optional command sets.

Note that POP3, though widely supported, is obsolescent.  The
implementation quality of POP3 servers varies widely, and too many are
quite poor. If your mailserver supports IMAP, you would be better off
using the \code{\refmodule{imaplib}.\class{IMAP4}} class, as IMAP
servers tend to be better implemented.

A single class is provided by the \module{poplib} module:

\begin{classdesc}{POP3}{host\optional{, port}}
This class implements the actual POP3 protocol.  The connection is
created when the instance is initialized.
If \var{port} is omitted, the standard POP3 port (110) is used.
\end{classdesc}

One exception is defined as an attribute of the \module{poplib} module:

\begin{excdesc}{error_proto}
Exception raised on any errors.  The reason for the exception is
passed to the constructor as a string.
\end{excdesc}

\begin{seealso}
  \seemodule{imaplib}{The standard Python IMAP module.}
  \seetitle{http://www.tuxedo.org/~esr/fetchail/fetchmail-FAQ.html}{
	The FAQ for the fetchmail POP/IMAP client collects information
	on POP3 server variations and RFC noncompliance that may be
	useful if you need to write an application based on poplib.} 
\end{seealso}


\subsection{POP3 Objects \label{pop3-objects}}

All POP3 commands are represented by methods of the same name,
in lower-case; most return the response text sent by the server.

An \class{POP3} instance has the following methods:


\begin{methoddesc}{getwelcome}{}
Returns the greeting string sent by the POP3 server.
\end{methoddesc}


\begin{methoddesc}{user}{username}
Send user command, response should indicate that a password is required.
\end{methoddesc}

\begin{methoddesc}{pass_}{password}
Send password, response includes message count and mailbox size.
Note: the mailbox on the server is locked until \method{quit()} is
called.
\end{methoddesc}

\begin{methoddesc}{apop}{user, secret}
Use the more secure APOP authentication to log into the POP3 server.
\end{methoddesc}

\begin{methoddesc}{rpop}{user}
Use RPOP authentication (similar to UNIX r-commands) to log into POP3 server.
\end{methoddesc}

\begin{methoddesc}{stat}{}
Get mailbox status.  The result is a tuple of 2 integers:
\code{(\var{message count}, \var{mailbox size})}.
\end{methoddesc}

\begin{methoddesc}{list}{\optional{which}}
Request message list, result is in the form
\code{(\var{response}, ['mesg_num octets', ...])}.  If \var{which} is
set, it is the message to list.
\end{methoddesc}

\begin{methoddesc}{retr}{which}
Retrieve whole message number \var{which}, and set its seen flag.
Result is in form  \code{(\var{response}, ['line', ...], \var{octets})}.
\end{methoddesc}

\begin{methoddesc}{dele}{which}
Flag message number \var{which} for deletion.  On most servers
deletions are not actually performed until QUIT (the major exception is
Eudora QPOP, which deliberately violates the RFCs by doing pending 
deletes on any disconnect).
\end{methoddesc}

\begin{methoddesc}{rset}{}
Remove any deletion marks for the mailbox.
\end{methoddesc}

\begin{methoddesc}{noop}{}
Do nothing.  Might be used as a keep-alive.
\end{methoddesc}

\begin{methoddesc}{quit}{}
Signoff:  commit changes, unlock mailbox, drop connection.
\end{methoddesc}

\begin{methoddesc}{top}{which, howmuch}
Retrieves the message header plus \var{howmuch} lines of the message
after the header of message number \var{which}. Result is in form 
\code{(\var{response}, ['line', ...], \var{octets})}.

The POP3 TOP command this method uses, unlike the RETR command,
doesn't set the message's seen flag; unfortunately, TOP is poorly
specified in the RFCs and is frequently broken in off-brand servers.
Test this method by hand against the POP3 servers you will use before
trusting it.
\end{methoddesc}

\begin{methoddesc}{uidl}{\optional{which}}
Return message digest (unique id) list.
If \var{which} is specified, result contains the unique id for that
message in the form \code{'\var{response}\ \var{mesgnum}\ \var{uid}},
otherwise result is list \code{(\var{response}, ['mesgnum uid', ...],
\var{octets})}.
\end{methoddesc}


\subsection{POP3 Example \label{pop3-example}}

Here is a minimal example (without error checking) that opens a
mailbox and retrieves and prints all messages:

\begin{verbatim}
import getpass, poplib

M = poplib.POP3('localhost')
M.user(getpass.getuser())
M.pass_(getpass.getpass())
numMessages = len(M.list()[1])
for i in range(numMessages):
    for j in M.retr(i+1)[1]:
        print j
\end{verbatim}

At the end of the module, there is a test section that contains a more
extensive example of usage.