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