blob: 8468f4c49885c0d48616b15a0aeff56271089567 [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
Georg Brandl93a56cd2014-10-30 22:25:41 +010018:rfc:`1939`. The :class:`POP3` class also supports the ``STLS`` command introduced
Antoine Pitrou8618d742012-11-23 20:13:48 +010019in :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
Antoine Pitrouc5e075f2014-03-22 18:19:11 +010046 port is used. *timeout* works as in the :class:`POP3` constructor.
47 *context* is an optional :class:`ssl.SSLContext` object which allows
48 bundling SSL configuration options, certificates and private keys into a
49 single (potentially long-lived) structure. Please read :ref:`ssl-security`
50 for best practices.
51
52 *keyfile* and *certfile* are a legacy alternative to *context* - they can
53 point to PEM-formatted private key and certificate chain files,
54 respectively, for the SSL connection.
Giampaolo RodolĂ 42382fe2010-08-17 16:09:53 +000055
56 .. versionchanged:: 3.2
57 *context* parameter added.
Georg Brandl116aa622007-08-15 14:28:22 +000058
Christian Heimes1bc70682013-12-02 20:10:50 +010059 .. versionchanged:: 3.4
60 The class now supports hostname check with
Antoine Pitrouc5e075f2014-03-22 18:19:11 +010061 :attr:`ssl.SSLContext.check_hostname` and *Server Name Indication* (see
62 :data:`ssl.HAS_SNI`).
Georg Brandl116aa622007-08-15 14:28:22 +000063
64One exception is defined as an attribute of the :mod:`poplib` module:
65
66
67.. exception:: error_proto
68
69 Exception raised on any errors from this module (errors from :mod:`socket`
70 module are not caught). The reason for the exception is passed to the
71 constructor as a string.
72
73
74.. seealso::
75
76 Module :mod:`imaplib`
77 The standard Python IMAP module.
78
79 `Frequently Asked Questions About Fetchmail <http://www.catb.org/~esr/fetchmail/fetchmail-FAQ.html>`_
80 The FAQ for the :program:`fetchmail` POP/IMAP client collects information on
81 POP3 server variations and RFC noncompliance that may be useful if you need to
82 write an application based on the POP protocol.
83
84
85.. _pop3-objects:
86
87POP3 Objects
88------------
89
90All POP3 commands are represented by methods of the same name, in lower-case;
91most return the response text sent by the server.
92
93An :class:`POP3` instance has the following methods:
94
95
96.. method:: POP3.set_debuglevel(level)
97
98 Set the instance's debugging level. This controls the amount of debugging
99 output printed. The default, ``0``, produces no debugging output. A value of
100 ``1`` produces a moderate amount of debugging output, generally a single line
101 per request. A value of ``2`` or higher produces the maximum amount of
102 debugging output, logging each line sent and received on the control connection.
103
104
105.. method:: POP3.getwelcome()
106
107 Returns the greeting string sent by the POP3 server.
108
109
Antoine Pitrou25cee192012-11-23 20:07:39 +0100110.. method:: POP3.capa()
111
112 Query the server's capabilities as specified in :rfc:`2449`.
113 Returns a dictionary in the form ``{'name': ['param'...]}``.
114
115 .. versionadded:: 3.4
116
117
Georg Brandl116aa622007-08-15 14:28:22 +0000118.. method:: POP3.user(username)
119
120 Send user command, response should indicate that a password is required.
121
122
123.. method:: POP3.pass_(password)
124
125 Send password, response includes message count and mailbox size. Note: the
Jesus Ceac73f8632012-12-26 16:47:03 +0100126 mailbox on the server is locked until :meth:`~poplib.quit` is called.
Georg Brandl116aa622007-08-15 14:28:22 +0000127
128
129.. method:: POP3.apop(user, secret)
130
131 Use the more secure APOP authentication to log into the POP3 server.
132
133
134.. method:: POP3.rpop(user)
135
136 Use RPOP authentication (similar to UNIX r-commands) to log into POP3 server.
137
138
139.. method:: POP3.stat()
140
141 Get mailbox status. The result is a tuple of 2 integers: ``(message count,
142 mailbox size)``.
143
144
145.. method:: POP3.list([which])
146
147 Request message list, result is in the form ``(response, ['mesg_num octets',
148 ...], octets)``. If *which* is set, it is the message to list.
149
150
151.. method:: POP3.retr(which)
152
153 Retrieve whole message number *which*, and set its seen flag. Result is in form
154 ``(response, ['line', ...], octets)``.
155
156
157.. method:: POP3.dele(which)
158
159 Flag message number *which* for deletion. On most servers deletions are not
160 actually performed until QUIT (the major exception is Eudora QPOP, which
161 deliberately violates the RFCs by doing pending deletes on any disconnect).
162
163
164.. method:: POP3.rset()
165
166 Remove any deletion marks for the mailbox.
167
168
169.. method:: POP3.noop()
170
171 Do nothing. Might be used as a keep-alive.
172
173
174.. method:: POP3.quit()
175
176 Signoff: commit changes, unlock mailbox, drop connection.
177
178
179.. method:: POP3.top(which, howmuch)
180
181 Retrieves the message header plus *howmuch* lines of the message after the
182 header of message number *which*. Result is in form ``(response, ['line', ...],
183 octets)``.
184
185 The POP3 TOP command this method uses, unlike the RETR command, doesn't set the
186 message's seen flag; unfortunately, TOP is poorly specified in the RFCs and is
187 frequently broken in off-brand servers. Test this method by hand against the
188 POP3 servers you will use before trusting it.
189
190
Georg Brandl18244152009-09-02 20:34:52 +0000191.. method:: POP3.uidl(which=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000192
193 Return message digest (unique id) list. If *which* is specified, result contains
194 the unique id for that message in the form ``'response mesgnum uid``, otherwise
195 result is list ``(response, ['mesgnum uid', ...], octets)``.
196
R David Murrayb8cd3e42015-05-16 15:05:53 -0400197
198.. method:: POP3.utf8()
199
Berker Peksagfee05da2015-05-19 01:38:05 +0300200 Try to switch to UTF-8 mode. Returns the server response if successful,
R David Murrayb8cd3e42015-05-16 15:05:53 -0400201 raises :class:`error_proto` if not. Specified in :RFC:`6856`.
202
203 .. versionadded:: 3.5
204
205
Antoine Pitrou8618d742012-11-23 20:13:48 +0100206.. method:: POP3.stls(context=None)
207
208 Start a TLS session on the active connection as specified in :rfc:`2595`.
209 This is only allowed before user authentication
210
211 *context* parameter is a :class:`ssl.SSLContext` object which allows
212 bundling SSL configuration options, certificates and private keys into
Antoine Pitrouc5e075f2014-03-22 18:19:11 +0100213 a single (potentially long-lived) structure. Please read :ref:`ssl-security`
214 for best practices.
215
216 This method supports hostname checking via
217 :attr:`ssl.SSLContext.check_hostname` and *Server Name Indication* (see
218 :data:`ssl.HAS_SNI`).
Antoine Pitrou8618d742012-11-23 20:13:48 +0100219
220 .. versionadded:: 3.4
221
222
Georg Brandl116aa622007-08-15 14:28:22 +0000223Instances of :class:`POP3_SSL` have no additional methods. The interface of this
224subclass is identical to its parent.
225
226
227.. _pop3-example:
228
229POP3 Example
230------------
231
232Here is a minimal example (without error checking) that opens a mailbox and
233retrieves and prints all messages::
234
235 import getpass, poplib
236
237 M = poplib.POP3('localhost')
238 M.user(getpass.getuser())
239 M.pass_(getpass.getpass())
240 numMessages = len(M.list()[1])
241 for i in range(numMessages):
242 for j in M.retr(i+1)[1]:
Georg Brandl6911e3c2007-09-04 07:15:32 +0000243 print(j)
Georg Brandl116aa622007-08-15 14:28:22 +0000244
245At the end of the module, there is a test section that contains a more extensive
246example of usage.
247