blob: fa1db01fff0c1b30d9c8834ae9a1747ffd705d2a [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
Christian Heimes1bc70682013-12-02 20:10:50 +010056 .. versionchanged:: 3.4
57 The class now supports hostname check with
58 :attr:`SSLContext.check_hostname` and *Server Name Indicator* (see
59 :data:`~ssl.HAS_SNI`).
Georg Brandl116aa622007-08-15 14:28:22 +000060
61One exception is defined as an attribute of the :mod:`poplib` module:
62
63
64.. exception:: error_proto
65
66 Exception raised on any errors from this module (errors from :mod:`socket`
67 module are not caught). The reason for the exception is passed to the
68 constructor as a string.
69
70
71.. seealso::
72
73 Module :mod:`imaplib`
74 The standard Python IMAP module.
75
76 `Frequently Asked Questions About Fetchmail <http://www.catb.org/~esr/fetchmail/fetchmail-FAQ.html>`_
77 The FAQ for the :program:`fetchmail` POP/IMAP client collects information on
78 POP3 server variations and RFC noncompliance that may be useful if you need to
79 write an application based on the POP protocol.
80
81
82.. _pop3-objects:
83
84POP3 Objects
85------------
86
87All POP3 commands are represented by methods of the same name, in lower-case;
88most return the response text sent by the server.
89
90An :class:`POP3` instance has the following methods:
91
92
93.. method:: POP3.set_debuglevel(level)
94
95 Set the instance's debugging level. This controls the amount of debugging
96 output printed. The default, ``0``, produces no debugging output. A value of
97 ``1`` produces a moderate amount of debugging output, generally a single line
98 per request. A value of ``2`` or higher produces the maximum amount of
99 debugging output, logging each line sent and received on the control connection.
100
101
102.. method:: POP3.getwelcome()
103
104 Returns the greeting string sent by the POP3 server.
105
106
Antoine Pitrou25cee192012-11-23 20:07:39 +0100107.. method:: POP3.capa()
108
109 Query the server's capabilities as specified in :rfc:`2449`.
110 Returns a dictionary in the form ``{'name': ['param'...]}``.
111
112 .. versionadded:: 3.4
113
114
Georg Brandl116aa622007-08-15 14:28:22 +0000115.. method:: POP3.user(username)
116
117 Send user command, response should indicate that a password is required.
118
119
120.. method:: POP3.pass_(password)
121
122 Send password, response includes message count and mailbox size. Note: the
Jesus Ceac73f8632012-12-26 16:47:03 +0100123 mailbox on the server is locked until :meth:`~poplib.quit` is called.
Georg Brandl116aa622007-08-15 14:28:22 +0000124
125
126.. method:: POP3.apop(user, secret)
127
128 Use the more secure APOP authentication to log into the POP3 server.
129
130
131.. method:: POP3.rpop(user)
132
133 Use RPOP authentication (similar to UNIX r-commands) to log into POP3 server.
134
135
136.. method:: POP3.stat()
137
138 Get mailbox status. The result is a tuple of 2 integers: ``(message count,
139 mailbox size)``.
140
141
142.. method:: POP3.list([which])
143
144 Request message list, result is in the form ``(response, ['mesg_num octets',
145 ...], octets)``. If *which* is set, it is the message to list.
146
147
148.. method:: POP3.retr(which)
149
150 Retrieve whole message number *which*, and set its seen flag. Result is in form
151 ``(response, ['line', ...], octets)``.
152
153
154.. method:: POP3.dele(which)
155
156 Flag message number *which* for deletion. On most servers deletions are not
157 actually performed until QUIT (the major exception is Eudora QPOP, which
158 deliberately violates the RFCs by doing pending deletes on any disconnect).
159
160
161.. method:: POP3.rset()
162
163 Remove any deletion marks for the mailbox.
164
165
166.. method:: POP3.noop()
167
168 Do nothing. Might be used as a keep-alive.
169
170
171.. method:: POP3.quit()
172
173 Signoff: commit changes, unlock mailbox, drop connection.
174
175
176.. method:: POP3.top(which, howmuch)
177
178 Retrieves the message header plus *howmuch* lines of the message after the
179 header of message number *which*. Result is in form ``(response, ['line', ...],
180 octets)``.
181
182 The POP3 TOP command this method uses, unlike the RETR command, doesn't set the
183 message's seen flag; unfortunately, TOP is poorly specified in the RFCs and is
184 frequently broken in off-brand servers. Test this method by hand against the
185 POP3 servers you will use before trusting it.
186
187
Georg Brandl18244152009-09-02 20:34:52 +0000188.. method:: POP3.uidl(which=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000189
190 Return message digest (unique id) list. If *which* is specified, result contains
191 the unique id for that message in the form ``'response mesgnum uid``, otherwise
192 result is list ``(response, ['mesgnum uid', ...], octets)``.
193
Antoine Pitrou8618d742012-11-23 20:13:48 +0100194.. method:: POP3.stls(context=None)
195
196 Start a TLS session on the active connection as specified in :rfc:`2595`.
197 This is only allowed before user authentication
198
199 *context* parameter is a :class:`ssl.SSLContext` object which allows
200 bundling SSL configuration options, certificates and private keys into
R David Murray473f45b2013-12-27 17:01:16 -0500201 a single (potentially long-lived) structure. This method supports
202 hostname checking via :attr:`SSLContext.check_hostname`
203 :attr:`SSLContext.check_hostname` and *Server Name Indicator* (see
204 :data:`~ssl.HAS_SNI`).
Antoine Pitrou8618d742012-11-23 20:13:48 +0100205
206 .. versionadded:: 3.4
207
208
Georg Brandl116aa622007-08-15 14:28:22 +0000209Instances of :class:`POP3_SSL` have no additional methods. The interface of this
210subclass is identical to its parent.
211
212
213.. _pop3-example:
214
215POP3 Example
216------------
217
218Here is a minimal example (without error checking) that opens a mailbox and
219retrieves and prints all messages::
220
221 import getpass, poplib
222
223 M = poplib.POP3('localhost')
224 M.user(getpass.getuser())
225 M.pass_(getpass.getpass())
226 numMessages = len(M.list()[1])
227 for i in range(numMessages):
228 for j in M.retr(i+1)[1]:
Georg Brandl6911e3c2007-09-04 07:15:32 +0000229 print(j)
Georg Brandl116aa622007-08-15 14:28:22 +0000230
231At the end of the module, there is a test section that contains a more extensive
232example of usage.
233