blob: e248d6b834a5f1cfde5511f7ca1242e8fad6b5d0 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`poplib` --- POP3 protocol client
2======================================
3
4.. module:: poplib
5 :synopsis: POP3 protocol client (requires sockets).
Christian Heimes5b5e81c2007-12-31 16:14:33 +00006.. sectionauthor:: Andrew T. Csillag
7.. revised by ESR, January 2000
Georg Brandl116aa622007-08-15 14:28:22 +00008
9.. index:: pair: POP3; protocol
10
Raymond Hettinger469271d2011-01-27 20:38:46 +000011**Source code:** :source:`Lib/poplib.py`
12
13--------------
14
Georg Brandl116aa622007-08-15 14:28:22 +000015This module defines a class, :class:`POP3`, which encapsulates a connection to a
Antoine Pitrou8618d742012-11-23 20:13:48 +010016POP3 server and implements the protocol as defined in :rfc:`1939`. The
17:class:`POP3` class supports both the minimal and optional command sets from
18:rfc:`1939`. The :class:`POP3` class also supports the `STLS` command introduced
19in :rfc:`2595` to enable encrypted communication on an already established connection.
20
Georg Brandl116aa622007-08-15 14:28:22 +000021Additionally, this module provides a class :class:`POP3_SSL`, which provides
22support for connecting to POP3 servers that use SSL as an underlying protocol
23layer.
24
25Note that POP3, though widely supported, is obsolescent. The implementation
26quality of POP3 servers varies widely, and too many are quite poor. If your
27mailserver supports IMAP, you would be better off using the
28:class:`imaplib.IMAP4` class, as IMAP servers tend to be better implemented.
29
Antoine Pitrou3813c9e2012-11-18 18:37:02 +010030The :mod:`poplib` module provides two classes:
Georg Brandl116aa622007-08-15 14:28:22 +000031
32
Georg Brandl18244152009-09-02 20:34:52 +000033.. class:: POP3(host, port=POP3_PORT[, timeout])
Georg Brandl116aa622007-08-15 14:28:22 +000034
35 This class implements the actual POP3 protocol. The connection is created when
36 the instance is initialized. If *port* is omitted, the standard POP3 port (110)
37 is used. The optional *timeout* parameter specifies a timeout in seconds for the
Georg Brandlf78e02b2008-06-10 17:40:04 +000038 connection attempt (if not specified, the global default timeout setting will
39 be used).
Georg Brandl116aa622007-08-15 14:28:22 +000040
Georg Brandl116aa622007-08-15 14:28:22 +000041
Giampaolo RodolĂ 42382fe2010-08-17 16:09:53 +000042.. class:: POP3_SSL(host, port=POP3_SSL_PORT, keyfile=None, certfile=None, timeout=None, context=None)
Georg Brandl116aa622007-08-15 14:28:22 +000043
44 This is a subclass of :class:`POP3` that connects to the server over an SSL
45 encrypted socket. If *port* is not specified, 995, the standard POP3-over-SSL
46 port is used. *keyfile* and *certfile* are also optional - they can contain a
47 PEM formatted private key and certificate chain file for the SSL connection.
Giampaolo RodolĂ 42382fe2010-08-17 16:09:53 +000048 *timeout* works as in the :class:`POP3` constructor. *context* parameter is a
49 :class:`ssl.SSLContext` object which allows bundling SSL configuration
50 options, certificates and private keys into a single (potentially long-lived)
51 structure.
52
53 .. versionchanged:: 3.2
54 *context* parameter added.
Georg Brandl116aa622007-08-15 14:28:22 +000055
Georg Brandl116aa622007-08-15 14:28:22 +000056
57One exception is defined as an attribute of the :mod:`poplib` module:
58
59
60.. exception:: error_proto
61
62 Exception raised on any errors from this module (errors from :mod:`socket`
63 module are not caught). The reason for the exception is passed to the
64 constructor as a string.
65
66
67.. seealso::
68
69 Module :mod:`imaplib`
70 The standard Python IMAP module.
71
72 `Frequently Asked Questions About Fetchmail <http://www.catb.org/~esr/fetchmail/fetchmail-FAQ.html>`_
73 The FAQ for the :program:`fetchmail` POP/IMAP client collects information on
74 POP3 server variations and RFC noncompliance that may be useful if you need to
75 write an application based on the POP protocol.
76
77
78.. _pop3-objects:
79
80POP3 Objects
81------------
82
83All POP3 commands are represented by methods of the same name, in lower-case;
84most return the response text sent by the server.
85
86An :class:`POP3` instance has the following methods:
87
88
89.. method:: POP3.set_debuglevel(level)
90
91 Set the instance's debugging level. This controls the amount of debugging
92 output printed. The default, ``0``, produces no debugging output. A value of
93 ``1`` produces a moderate amount of debugging output, generally a single line
94 per request. A value of ``2`` or higher produces the maximum amount of
95 debugging output, logging each line sent and received on the control connection.
96
97
98.. method:: POP3.getwelcome()
99
100 Returns the greeting string sent by the POP3 server.
101
102
Antoine Pitrou25cee192012-11-23 20:07:39 +0100103.. method:: POP3.capa()
104
105 Query the server's capabilities as specified in :rfc:`2449`.
106 Returns a dictionary in the form ``{'name': ['param'...]}``.
107
108 .. versionadded:: 3.4
109
110
Georg Brandl116aa622007-08-15 14:28:22 +0000111.. method:: POP3.user(username)
112
113 Send user command, response should indicate that a password is required.
114
115
116.. method:: POP3.pass_(password)
117
118 Send password, response includes message count and mailbox size. Note: the
Jesus Ceac73f8632012-12-26 16:47:03 +0100119 mailbox on the server is locked until :meth:`~poplib.quit` is called.
Georg Brandl116aa622007-08-15 14:28:22 +0000120
121
122.. method:: POP3.apop(user, secret)
123
124 Use the more secure APOP authentication to log into the POP3 server.
125
126
127.. method:: POP3.rpop(user)
128
129 Use RPOP authentication (similar to UNIX r-commands) to log into POP3 server.
130
131
132.. method:: POP3.stat()
133
134 Get mailbox status. The result is a tuple of 2 integers: ``(message count,
135 mailbox size)``.
136
137
138.. method:: POP3.list([which])
139
140 Request message list, result is in the form ``(response, ['mesg_num octets',
141 ...], octets)``. If *which* is set, it is the message to list.
142
143
144.. method:: POP3.retr(which)
145
146 Retrieve whole message number *which*, and set its seen flag. Result is in form
147 ``(response, ['line', ...], octets)``.
148
149
150.. method:: POP3.dele(which)
151
152 Flag message number *which* for deletion. On most servers deletions are not
153 actually performed until QUIT (the major exception is Eudora QPOP, which
154 deliberately violates the RFCs by doing pending deletes on any disconnect).
155
156
157.. method:: POP3.rset()
158
159 Remove any deletion marks for the mailbox.
160
161
162.. method:: POP3.noop()
163
164 Do nothing. Might be used as a keep-alive.
165
166
167.. method:: POP3.quit()
168
169 Signoff: commit changes, unlock mailbox, drop connection.
170
171
172.. method:: POP3.top(which, howmuch)
173
174 Retrieves the message header plus *howmuch* lines of the message after the
175 header of message number *which*. Result is in form ``(response, ['line', ...],
176 octets)``.
177
178 The POP3 TOP command this method uses, unlike the RETR command, doesn't set the
179 message's seen flag; unfortunately, TOP is poorly specified in the RFCs and is
180 frequently broken in off-brand servers. Test this method by hand against the
181 POP3 servers you will use before trusting it.
182
183
Georg Brandl18244152009-09-02 20:34:52 +0000184.. method:: POP3.uidl(which=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000185
186 Return message digest (unique id) list. If *which* is specified, result contains
187 the unique id for that message in the form ``'response mesgnum uid``, otherwise
188 result is list ``(response, ['mesgnum uid', ...], octets)``.
189
Antoine Pitrou8618d742012-11-23 20:13:48 +0100190.. method:: POP3.stls(context=None)
191
192 Start a TLS session on the active connection as specified in :rfc:`2595`.
193 This is only allowed before user authentication
194
195 *context* parameter is a :class:`ssl.SSLContext` object which allows
196 bundling SSL configuration options, certificates and private keys into
197 a single (potentially long-lived) structure.
198
199 .. versionadded:: 3.4
200
201
Georg Brandl116aa622007-08-15 14:28:22 +0000202Instances of :class:`POP3_SSL` have no additional methods. The interface of this
203subclass is identical to its parent.
204
205
206.. _pop3-example:
207
208POP3 Example
209------------
210
211Here is a minimal example (without error checking) that opens a mailbox and
212retrieves and prints all messages::
213
214 import getpass, poplib
215
216 M = poplib.POP3('localhost')
217 M.user(getpass.getuser())
218 M.pass_(getpass.getpass())
219 numMessages = len(M.list()[1])
220 for i in range(numMessages):
221 for j in M.retr(i+1)[1]:
Georg Brandl6911e3c2007-09-04 07:15:32 +0000222 print(j)
Georg Brandl116aa622007-08-15 14:28:22 +0000223
224At the end of the module, there is a test section that contains a more extensive
225example of usage.
226