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