blob: b462ec5e4538baf7f560a9c5280a873e1bf23575 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`poplib` --- POP3 protocol client
3======================================
4
5.. module:: poplib
6 :synopsis: POP3 protocol client (requires sockets).
7
8
9.. index:: pair: POP3; protocol
10
11.. % By Andrew T. Csillag
12.. % Even though I put it into LaTeX, I cannot really claim that I wrote
13.. % it since I just stole most of it from the poplib.py source code and
14.. % the imaplib ``chapter''.
15.. % Revised by ESR, January 2000
16
17This module defines a class, :class:`POP3`, which encapsulates a connection to a
18POP3 server and implements the protocol as defined in :rfc:`1725`. The
19:class:`POP3` class supports both the minimal and optional command sets.
20Additionally, this module provides a class :class:`POP3_SSL`, which provides
21support for connecting to POP3 servers that use SSL as an underlying protocol
22layer.
23
24Note that POP3, though widely supported, is obsolescent. The implementation
25quality of POP3 servers varies widely, and too many are quite poor. If your
26mailserver supports IMAP, you would be better off using the
27:class:`imaplib.IMAP4` class, as IMAP servers tend to be better implemented.
28
29A single class is provided by the :mod:`poplib` module:
30
31
32.. class:: POP3(host[, port[, timeout]])
33
34 This class implements the actual POP3 protocol. The connection is created when
35 the instance is initialized. If *port* is omitted, the standard POP3 port (110)
36 is used. The optional *timeout* parameter specifies a timeout in seconds for the
37 connection attempt (if not specified, or passed as None, the global default
38 timeout setting will be used).
39
Georg Brandl116aa622007-08-15 14:28:22 +000040
41.. class:: POP3_SSL(host[, port[, keyfile[, certfile]]])
42
43 This is a subclass of :class:`POP3` that connects to the server over an SSL
44 encrypted socket. If *port* is not specified, 995, the standard POP3-over-SSL
45 port is used. *keyfile* and *certfile* are also optional - they can contain a
46 PEM formatted private key and certificate chain file for the SSL connection.
47
Georg Brandl116aa622007-08-15 14:28:22 +000048
49One exception is defined as an attribute of the :mod:`poplib` module:
50
51
52.. exception:: error_proto
53
54 Exception raised on any errors from this module (errors from :mod:`socket`
55 module are not caught). The reason for the exception is passed to the
56 constructor as a string.
57
58
59.. seealso::
60
61 Module :mod:`imaplib`
62 The standard Python IMAP module.
63
64 `Frequently Asked Questions About Fetchmail <http://www.catb.org/~esr/fetchmail/fetchmail-FAQ.html>`_
65 The FAQ for the :program:`fetchmail` POP/IMAP client collects information on
66 POP3 server variations and RFC noncompliance that may be useful if you need to
67 write an application based on the POP protocol.
68
69
70.. _pop3-objects:
71
72POP3 Objects
73------------
74
75All POP3 commands are represented by methods of the same name, in lower-case;
76most return the response text sent by the server.
77
78An :class:`POP3` instance has the following methods:
79
80
81.. method:: POP3.set_debuglevel(level)
82
83 Set the instance's debugging level. This controls the amount of debugging
84 output printed. The default, ``0``, produces no debugging output. A value of
85 ``1`` produces a moderate amount of debugging output, generally a single line
86 per request. A value of ``2`` or higher produces the maximum amount of
87 debugging output, logging each line sent and received on the control connection.
88
89
90.. method:: POP3.getwelcome()
91
92 Returns the greeting string sent by the POP3 server.
93
94
95.. method:: POP3.user(username)
96
97 Send user command, response should indicate that a password is required.
98
99
100.. method:: POP3.pass_(password)
101
102 Send password, response includes message count and mailbox size. Note: the
103 mailbox on the server is locked until :meth:`quit` is called.
104
105
106.. method:: POP3.apop(user, secret)
107
108 Use the more secure APOP authentication to log into the POP3 server.
109
110
111.. method:: POP3.rpop(user)
112
113 Use RPOP authentication (similar to UNIX r-commands) to log into POP3 server.
114
115
116.. method:: POP3.stat()
117
118 Get mailbox status. The result is a tuple of 2 integers: ``(message count,
119 mailbox size)``.
120
121
122.. method:: POP3.list([which])
123
124 Request message list, result is in the form ``(response, ['mesg_num octets',
125 ...], octets)``. If *which* is set, it is the message to list.
126
127
128.. method:: POP3.retr(which)
129
130 Retrieve whole message number *which*, and set its seen flag. Result is in form
131 ``(response, ['line', ...], octets)``.
132
133
134.. method:: POP3.dele(which)
135
136 Flag message number *which* for deletion. On most servers deletions are not
137 actually performed until QUIT (the major exception is Eudora QPOP, which
138 deliberately violates the RFCs by doing pending deletes on any disconnect).
139
140
141.. method:: POP3.rset()
142
143 Remove any deletion marks for the mailbox.
144
145
146.. method:: POP3.noop()
147
148 Do nothing. Might be used as a keep-alive.
149
150
151.. method:: POP3.quit()
152
153 Signoff: commit changes, unlock mailbox, drop connection.
154
155
156.. method:: POP3.top(which, howmuch)
157
158 Retrieves the message header plus *howmuch* lines of the message after the
159 header of message number *which*. Result is in form ``(response, ['line', ...],
160 octets)``.
161
162 The POP3 TOP command this method uses, unlike the RETR command, doesn't set the
163 message's seen flag; unfortunately, TOP is poorly specified in the RFCs and is
164 frequently broken in off-brand servers. Test this method by hand against the
165 POP3 servers you will use before trusting it.
166
167
168.. method:: POP3.uidl([which])
169
170 Return message digest (unique id) list. If *which* is specified, result contains
171 the unique id for that message in the form ``'response mesgnum uid``, otherwise
172 result is list ``(response, ['mesgnum uid', ...], octets)``.
173
174Instances of :class:`POP3_SSL` have no additional methods. The interface of this
175subclass is identical to its parent.
176
177
178.. _pop3-example:
179
180POP3 Example
181------------
182
183Here is a minimal example (without error checking) that opens a mailbox and
184retrieves and prints all messages::
185
186 import getpass, poplib
187
188 M = poplib.POP3('localhost')
189 M.user(getpass.getuser())
190 M.pass_(getpass.getpass())
191 numMessages = len(M.list()[1])
192 for i in range(numMessages):
193 for j in M.retr(i+1)[1]:
Georg Brandl6911e3c2007-09-04 07:15:32 +0000194 print(j)
Georg Brandl116aa622007-08-15 14:28:22 +0000195
196At the end of the module, there is a test section that contains a more extensive
197example of usage.
198