blob: 2f349b35b7e0ff393031de21b9154d7782a9ba6a [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).
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04006
Christian Heimes5b5e81c2007-12-31 16:14:33 +00007.. sectionauthor:: Andrew T. Csillag
8.. revised by ESR, January 2000
Georg Brandl116aa622007-08-15 14:28:22 +00009
Raymond Hettinger469271d2011-01-27 20:38:46 +000010**Source code:** :source:`Lib/poplib.py`
11
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040012.. index:: pair: POP3; protocol
13
Raymond Hettinger469271d2011-01-27 20:38:46 +000014--------------
15
Georg Brandl116aa622007-08-15 14:28:22 +000016This module defines a class, :class:`POP3`, which encapsulates a connection to a
Antoine Pitrou8618d742012-11-23 20:13:48 +010017POP3 server and implements the protocol as defined in :rfc:`1939`. The
18:class:`POP3` class supports both the minimal and optional command sets from
Georg Brandl93a56cd2014-10-30 22:25:41 +010019:rfc:`1939`. The :class:`POP3` class also supports the ``STLS`` command introduced
Antoine Pitrou8618d742012-11-23 20:13:48 +010020in :rfc:`2595` to enable encrypted communication on an already established connection.
21
Georg Brandl116aa622007-08-15 14:28:22 +000022Additionally, this module provides a class :class:`POP3_SSL`, which provides
23support for connecting to POP3 servers that use SSL as an underlying protocol
24layer.
25
26Note that POP3, though widely supported, is obsolescent. The implementation
27quality of POP3 servers varies widely, and too many are quite poor. If your
28mailserver supports IMAP, you would be better off using the
29:class:`imaplib.IMAP4` class, as IMAP servers tend to be better implemented.
30
Antoine Pitrou3813c9e2012-11-18 18:37:02 +010031The :mod:`poplib` module provides two classes:
Georg Brandl116aa622007-08-15 14:28:22 +000032
33
Georg Brandl18244152009-09-02 20:34:52 +000034.. class:: POP3(host, port=POP3_PORT[, timeout])
Georg Brandl116aa622007-08-15 14:28:22 +000035
36 This class implements the actual POP3 protocol. The connection is created when
37 the instance is initialized. If *port* is omitted, the standard POP3 port (110)
38 is used. The optional *timeout* parameter specifies a timeout in seconds for the
Georg Brandlf78e02b2008-06-10 17:40:04 +000039 connection attempt (if not specified, the global default timeout setting will
40 be used).
Georg Brandl116aa622007-08-15 14:28:22 +000041
Steve Dower44f91c32019-06-27 10:47:59 -070042 .. audit-event:: poplib.connect self,host,port poplib.POP3
Steve Dower60419a72019-06-24 08:42:54 -070043
Steve Dower44f91c32019-06-27 10:47:59 -070044 .. audit-event:: poplib.putline self,line poplib.POP3
45
46 All commands will raise an :ref:`auditing event <auditing>`
47 ``poplib.putline`` with arguments ``self`` and ``line``,
48 where ``line`` is the bytes about to be sent to the remote host.
Steve Dower60419a72019-06-24 08:42:54 -070049
Dong-hee Nac39b52f2020-01-10 23:34:05 +090050 .. versionchanged:: 3.9
51 If the *timeout* parameter is set to be zero, it will raise a
52 :class:`ValueError` to prevent the creation of a non-blocking socket.
Georg Brandl116aa622007-08-15 14:28:22 +000053
Giampaolo Rodolà42382fe2010-08-17 16:09:53 +000054.. class:: POP3_SSL(host, port=POP3_SSL_PORT, keyfile=None, certfile=None, timeout=None, context=None)
Georg Brandl116aa622007-08-15 14:28:22 +000055
56 This is a subclass of :class:`POP3` that connects to the server over an SSL
57 encrypted socket. If *port* is not specified, 995, the standard POP3-over-SSL
Antoine Pitrouc5e075f2014-03-22 18:19:11 +010058 port is used. *timeout* works as in the :class:`POP3` constructor.
59 *context* is an optional :class:`ssl.SSLContext` object which allows
60 bundling SSL configuration options, certificates and private keys into a
61 single (potentially long-lived) structure. Please read :ref:`ssl-security`
62 for best practices.
63
64 *keyfile* and *certfile* are a legacy alternative to *context* - they can
65 point to PEM-formatted private key and certificate chain files,
66 respectively, for the SSL connection.
Giampaolo Rodolà42382fe2010-08-17 16:09:53 +000067
Steve Dower44f91c32019-06-27 10:47:59 -070068 .. audit-event:: poplib.connect self,host,port poplib.POP3_SSL
Steve Dower60419a72019-06-24 08:42:54 -070069
Steve Dower44f91c32019-06-27 10:47:59 -070070 .. audit-event:: poplib.putline self,line popplib.POP3_SSL
71
72 All commands will raise an :ref:`auditing event <auditing>`
73 ``poplib.putline`` with arguments ``self`` and ``line``,
74 where ``line`` is the bytes about to be sent to the remote host.
Steve Dower60419a72019-06-24 08:42:54 -070075
Giampaolo Rodolà42382fe2010-08-17 16:09:53 +000076 .. versionchanged:: 3.2
77 *context* parameter added.
Georg Brandl116aa622007-08-15 14:28:22 +000078
Christian Heimes1bc70682013-12-02 20:10:50 +010079 .. versionchanged:: 3.4
80 The class now supports hostname check with
Antoine Pitrouc5e075f2014-03-22 18:19:11 +010081 :attr:`ssl.SSLContext.check_hostname` and *Server Name Indication* (see
82 :data:`ssl.HAS_SNI`).
Georg Brandl116aa622007-08-15 14:28:22 +000083
Christian Heimesd0486372016-09-10 23:23:33 +020084 .. deprecated:: 3.6
85
86 *keyfile* and *certfile* are deprecated in favor of *context*.
87 Please use :meth:`ssl.SSLContext.load_cert_chain` instead, or let
88 :func:`ssl.create_default_context` select the system's trusted CA
89 certificates for you.
90
Dong-hee Nac39b52f2020-01-10 23:34:05 +090091 .. versionchanged:: 3.9
92 If the *timeout* parameter is set to be zero, it will raise a
93 :class:`ValueError` to prevent the creation of a non-blocking socket.
94
Georg Brandl116aa622007-08-15 14:28:22 +000095One exception is defined as an attribute of the :mod:`poplib` module:
96
97
98.. exception:: error_proto
99
100 Exception raised on any errors from this module (errors from :mod:`socket`
101 module are not caught). The reason for the exception is passed to the
102 constructor as a string.
103
104
105.. seealso::
106
107 Module :mod:`imaplib`
108 The standard Python IMAP module.
109
110 `Frequently Asked Questions About Fetchmail <http://www.catb.org/~esr/fetchmail/fetchmail-FAQ.html>`_
111 The FAQ for the :program:`fetchmail` POP/IMAP client collects information on
112 POP3 server variations and RFC noncompliance that may be useful if you need to
113 write an application based on the POP protocol.
114
115
116.. _pop3-objects:
117
118POP3 Objects
119------------
120
121All POP3 commands are represented by methods of the same name, in lower-case;
122most return the response text sent by the server.
123
124An :class:`POP3` instance has the following methods:
125
126
127.. method:: POP3.set_debuglevel(level)
128
129 Set the instance's debugging level. This controls the amount of debugging
130 output printed. The default, ``0``, produces no debugging output. A value of
131 ``1`` produces a moderate amount of debugging output, generally a single line
132 per request. A value of ``2`` or higher produces the maximum amount of
133 debugging output, logging each line sent and received on the control connection.
134
135
136.. method:: POP3.getwelcome()
137
138 Returns the greeting string sent by the POP3 server.
139
140
Antoine Pitrou25cee192012-11-23 20:07:39 +0100141.. method:: POP3.capa()
142
143 Query the server's capabilities as specified in :rfc:`2449`.
144 Returns a dictionary in the form ``{'name': ['param'...]}``.
145
146 .. versionadded:: 3.4
147
148
Georg Brandl116aa622007-08-15 14:28:22 +0000149.. method:: POP3.user(username)
150
151 Send user command, response should indicate that a password is required.
152
153
154.. method:: POP3.pass_(password)
155
156 Send password, response includes message count and mailbox size. Note: the
Jesus Ceac73f8632012-12-26 16:47:03 +0100157 mailbox on the server is locked until :meth:`~poplib.quit` is called.
Georg Brandl116aa622007-08-15 14:28:22 +0000158
159
160.. method:: POP3.apop(user, secret)
161
162 Use the more secure APOP authentication to log into the POP3 server.
163
164
165.. method:: POP3.rpop(user)
166
167 Use RPOP authentication (similar to UNIX r-commands) to log into POP3 server.
168
169
170.. method:: POP3.stat()
171
172 Get mailbox status. The result is a tuple of 2 integers: ``(message count,
173 mailbox size)``.
174
175
176.. method:: POP3.list([which])
177
178 Request message list, result is in the form ``(response, ['mesg_num octets',
179 ...], octets)``. If *which* is set, it is the message to list.
180
181
182.. method:: POP3.retr(which)
183
184 Retrieve whole message number *which*, and set its seen flag. Result is in form
185 ``(response, ['line', ...], octets)``.
186
187
188.. method:: POP3.dele(which)
189
190 Flag message number *which* for deletion. On most servers deletions are not
191 actually performed until QUIT (the major exception is Eudora QPOP, which
192 deliberately violates the RFCs by doing pending deletes on any disconnect).
193
194
195.. method:: POP3.rset()
196
197 Remove any deletion marks for the mailbox.
198
199
200.. method:: POP3.noop()
201
202 Do nothing. Might be used as a keep-alive.
203
204
205.. method:: POP3.quit()
206
207 Signoff: commit changes, unlock mailbox, drop connection.
208
209
210.. method:: POP3.top(which, howmuch)
211
212 Retrieves the message header plus *howmuch* lines of the message after the
213 header of message number *which*. Result is in form ``(response, ['line', ...],
214 octets)``.
215
216 The POP3 TOP command this method uses, unlike the RETR command, doesn't set the
217 message's seen flag; unfortunately, TOP is poorly specified in the RFCs and is
218 frequently broken in off-brand servers. Test this method by hand against the
219 POP3 servers you will use before trusting it.
220
221
Georg Brandl18244152009-09-02 20:34:52 +0000222.. method:: POP3.uidl(which=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000223
224 Return message digest (unique id) list. If *which* is specified, result contains
225 the unique id for that message in the form ``'response mesgnum uid``, otherwise
226 result is list ``(response, ['mesgnum uid', ...], octets)``.
227
R David Murrayb8cd3e42015-05-16 15:05:53 -0400228
229.. method:: POP3.utf8()
230
Berker Peksagfee05da2015-05-19 01:38:05 +0300231 Try to switch to UTF-8 mode. Returns the server response if successful,
R David Murrayb8cd3e42015-05-16 15:05:53 -0400232 raises :class:`error_proto` if not. Specified in :RFC:`6856`.
233
234 .. versionadded:: 3.5
235
236
Antoine Pitrou8618d742012-11-23 20:13:48 +0100237.. method:: POP3.stls(context=None)
238
239 Start a TLS session on the active connection as specified in :rfc:`2595`.
240 This is only allowed before user authentication
241
242 *context* parameter is a :class:`ssl.SSLContext` object which allows
243 bundling SSL configuration options, certificates and private keys into
Antoine Pitrouc5e075f2014-03-22 18:19:11 +0100244 a single (potentially long-lived) structure. Please read :ref:`ssl-security`
245 for best practices.
246
247 This method supports hostname checking via
248 :attr:`ssl.SSLContext.check_hostname` and *Server Name Indication* (see
249 :data:`ssl.HAS_SNI`).
Antoine Pitrou8618d742012-11-23 20:13:48 +0100250
251 .. versionadded:: 3.4
252
253
Georg Brandl116aa622007-08-15 14:28:22 +0000254Instances of :class:`POP3_SSL` have no additional methods. The interface of this
255subclass is identical to its parent.
256
257
258.. _pop3-example:
259
260POP3 Example
261------------
262
263Here is a minimal example (without error checking) that opens a mailbox and
264retrieves and prints all messages::
265
266 import getpass, poplib
267
268 M = poplib.POP3('localhost')
269 M.user(getpass.getuser())
270 M.pass_(getpass.getpass())
271 numMessages = len(M.list()[1])
272 for i in range(numMessages):
273 for j in M.retr(i+1)[1]:
Georg Brandl6911e3c2007-09-04 07:15:32 +0000274 print(j)
Georg Brandl116aa622007-08-15 14:28:22 +0000275
276At the end of the module, there is a test section that contains a more extensive
277example of usage.