Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`poplib` --- POP3 protocol client |
| 2 | ====================================== |
| 3 | |
| 4 | .. module:: poplib |
| 5 | :synopsis: POP3 protocol client (requires sockets). |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 6 | .. sectionauthor:: Andrew T. Csillag |
| 7 | .. revised by ESR, January 2000 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 8 | |
| 9 | .. index:: pair: POP3; protocol |
| 10 | |
Raymond Hettinger | 469271d | 2011-01-27 20:38:46 +0000 | [diff] [blame] | 11 | **Source code:** :source:`Lib/poplib.py` |
| 12 | |
| 13 | -------------- |
| 14 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 15 | This module defines a class, :class:`POP3`, which encapsulates a connection to a |
Antoine Pitrou | 8618d74 | 2012-11-23 20:13:48 +0100 | [diff] [blame] | 16 | POP3 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 Brandl | 93a56cd | 2014-10-30 22:25:41 +0100 | [diff] [blame^] | 18 | :rfc:`1939`. The :class:`POP3` class also supports the ``STLS`` command introduced |
Antoine Pitrou | 8618d74 | 2012-11-23 20:13:48 +0100 | [diff] [blame] | 19 | in :rfc:`2595` to enable encrypted communication on an already established connection. |
| 20 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 21 | Additionally, this module provides a class :class:`POP3_SSL`, which provides |
| 22 | support for connecting to POP3 servers that use SSL as an underlying protocol |
| 23 | layer. |
| 24 | |
| 25 | Note that POP3, though widely supported, is obsolescent. The implementation |
| 26 | quality of POP3 servers varies widely, and too many are quite poor. If your |
| 27 | mailserver supports IMAP, you would be better off using the |
| 28 | :class:`imaplib.IMAP4` class, as IMAP servers tend to be better implemented. |
| 29 | |
Antoine Pitrou | 3813c9e | 2012-11-18 18:37:02 +0100 | [diff] [blame] | 30 | The :mod:`poplib` module provides two classes: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 31 | |
| 32 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 33 | .. class:: POP3(host, port=POP3_PORT[, timeout]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 34 | |
| 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 Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 38 | connection attempt (if not specified, the global default timeout setting will |
| 39 | be used). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 40 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 41 | |
Giampaolo RodolĂ | 42382fe | 2010-08-17 16:09:53 +0000 | [diff] [blame] | 42 | .. class:: POP3_SSL(host, port=POP3_SSL_PORT, keyfile=None, certfile=None, timeout=None, context=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 43 | |
| 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 Pitrou | c5e075f | 2014-03-22 18:19:11 +0100 | [diff] [blame] | 46 | 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Ă | 42382fe | 2010-08-17 16:09:53 +0000 | [diff] [blame] | 55 | |
| 56 | .. versionchanged:: 3.2 |
| 57 | *context* parameter added. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 58 | |
Christian Heimes | 1bc7068 | 2013-12-02 20:10:50 +0100 | [diff] [blame] | 59 | .. versionchanged:: 3.4 |
| 60 | The class now supports hostname check with |
Antoine Pitrou | c5e075f | 2014-03-22 18:19:11 +0100 | [diff] [blame] | 61 | :attr:`ssl.SSLContext.check_hostname` and *Server Name Indication* (see |
| 62 | :data:`ssl.HAS_SNI`). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 63 | |
| 64 | One 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 | |
| 87 | POP3 Objects |
| 88 | ------------ |
| 89 | |
| 90 | All POP3 commands are represented by methods of the same name, in lower-case; |
| 91 | most return the response text sent by the server. |
| 92 | |
| 93 | An :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 Pitrou | 25cee19 | 2012-11-23 20:07:39 +0100 | [diff] [blame] | 110 | .. 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 118 | .. 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 Cea | c73f863 | 2012-12-26 16:47:03 +0100 | [diff] [blame] | 126 | mailbox on the server is locked until :meth:`~poplib.quit` is called. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 127 | |
| 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 Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 191 | .. method:: POP3.uidl(which=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 192 | |
| 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 | |
Antoine Pitrou | 8618d74 | 2012-11-23 20:13:48 +0100 | [diff] [blame] | 197 | .. method:: POP3.stls(context=None) |
| 198 | |
| 199 | Start a TLS session on the active connection as specified in :rfc:`2595`. |
| 200 | This is only allowed before user authentication |
| 201 | |
| 202 | *context* parameter is a :class:`ssl.SSLContext` object which allows |
| 203 | bundling SSL configuration options, certificates and private keys into |
Antoine Pitrou | c5e075f | 2014-03-22 18:19:11 +0100 | [diff] [blame] | 204 | a single (potentially long-lived) structure. Please read :ref:`ssl-security` |
| 205 | for best practices. |
| 206 | |
| 207 | This method supports hostname checking via |
| 208 | :attr:`ssl.SSLContext.check_hostname` and *Server Name Indication* (see |
| 209 | :data:`ssl.HAS_SNI`). |
Antoine Pitrou | 8618d74 | 2012-11-23 20:13:48 +0100 | [diff] [blame] | 210 | |
| 211 | .. versionadded:: 3.4 |
| 212 | |
| 213 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 214 | Instances of :class:`POP3_SSL` have no additional methods. The interface of this |
| 215 | subclass is identical to its parent. |
| 216 | |
| 217 | |
| 218 | .. _pop3-example: |
| 219 | |
| 220 | POP3 Example |
| 221 | ------------ |
| 222 | |
| 223 | Here is a minimal example (without error checking) that opens a mailbox and |
| 224 | retrieves and prints all messages:: |
| 225 | |
| 226 | import getpass, poplib |
| 227 | |
| 228 | M = poplib.POP3('localhost') |
| 229 | M.user(getpass.getuser()) |
| 230 | M.pass_(getpass.getpass()) |
| 231 | numMessages = len(M.list()[1]) |
| 232 | for i in range(numMessages): |
| 233 | for j in M.retr(i+1)[1]: |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 234 | print(j) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 235 | |
| 236 | At the end of the module, there is a test section that contains a more extensive |
| 237 | example of usage. |
| 238 | |