Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{poplib} --- |
Fred Drake | e2effbd | 1999-04-22 16:21:09 +0000 | [diff] [blame] | 2 | POP3 protocol client} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | |
Fred Drake | e2effbd | 1999-04-22 16:21:09 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{poplib} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 5 | \modulesynopsis{POP3 protocol client (requires sockets).} |
| 6 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 7 | %By Andrew T. Csillag |
| 8 | %Even though I put it into LaTeX, I cannot really claim that I wrote |
| 9 | %it since I just stole most of it from the poplib.py source code and |
| 10 | %the imaplib ``chapter''. |
Eric S. Raymond | 5ac9795 | 2001-01-11 04:19:52 +0000 | [diff] [blame] | 11 | %Revised by ESR, January 2000 |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 12 | |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 13 | \indexii{POP3}{protocol} |
| 14 | |
| 15 | This module defines a class, \class{POP3}, which encapsulates a |
Fred Drake | 280f725 | 2001-08-14 11:42:13 +0000 | [diff] [blame] | 16 | connection to an POP3 server and implements the protocol as defined in |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 17 | \rfc{1725}. The \class{POP3} class supports both the minimal and |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 18 | optional command sets. |
| 19 | |
Eric S. Raymond | 5ac9795 | 2001-01-11 04:19:52 +0000 | [diff] [blame] | 20 | Note that POP3, though widely supported, is obsolescent. The |
| 21 | implementation quality of POP3 servers varies widely, and too many are |
| 22 | quite poor. If your mailserver supports IMAP, you would be better off |
Fred Drake | a7c9ac6 | 2001-05-09 03:49:48 +0000 | [diff] [blame] | 23 | using the \code{\refmodule{imaplib}.\class{IMAP4}} class, as IMAP |
| 24 | servers tend to be better implemented. |
Eric S. Raymond | 5ac9795 | 2001-01-11 04:19:52 +0000 | [diff] [blame] | 25 | |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 26 | A single class is provided by the \module{poplib} module: |
| 27 | |
| 28 | \begin{classdesc}{POP3}{host\optional{, port}} |
| 29 | This class implements the actual POP3 protocol. The connection is |
| 30 | created when the instance is initialized. |
| 31 | If \var{port} is omitted, the standard POP3 port (110) is used. |
| 32 | \end{classdesc} |
| 33 | |
Fred Drake | e2effbd | 1999-04-22 16:21:09 +0000 | [diff] [blame] | 34 | One exception is defined as an attribute of the \module{poplib} module: |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 35 | |
| 36 | \begin{excdesc}{error_proto} |
| 37 | Exception raised on any errors. The reason for the exception is |
| 38 | passed to the constructor as a string. |
| 39 | \end{excdesc} |
| 40 | |
Fred Drake | a7c9ac6 | 2001-05-09 03:49:48 +0000 | [diff] [blame] | 41 | \begin{seealso} |
| 42 | \seemodule{imaplib}{The standard Python IMAP module.} |
| 43 | \seetitle{http://www.tuxedo.org/~esr/fetchail/fetchmail-FAQ.html}{ |
| 44 | The FAQ for the fetchmail POP/IMAP client collects information |
| 45 | on POP3 server variations and RFC noncompliance that may be |
| 46 | useful if you need to write an application based on poplib.} |
| 47 | \end{seealso} |
| 48 | |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 49 | |
Fred Drake | e2effbd | 1999-04-22 16:21:09 +0000 | [diff] [blame] | 50 | \subsection{POP3 Objects \label{pop3-objects}} |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 51 | |
| 52 | All POP3 commands are represented by methods of the same name, |
Fred Drake | e2effbd | 1999-04-22 16:21:09 +0000 | [diff] [blame] | 53 | in lower-case; most return the response text sent by the server. |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 54 | |
| 55 | An \class{POP3} instance has the following methods: |
| 56 | |
| 57 | |
| 58 | \begin{methoddesc}{getwelcome}{} |
| 59 | Returns the greeting string sent by the POP3 server. |
| 60 | \end{methoddesc} |
| 61 | |
| 62 | |
| 63 | \begin{methoddesc}{user}{username} |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 64 | Send user command, response should indicate that a password is required. |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 65 | \end{methoddesc} |
| 66 | |
| 67 | \begin{methoddesc}{pass_}{password} |
| 68 | Send password, response includes message count and mailbox size. |
| 69 | Note: the mailbox on the server is locked until \method{quit()} is |
| 70 | called. |
| 71 | \end{methoddesc} |
| 72 | |
| 73 | \begin{methoddesc}{apop}{user, secret} |
| 74 | Use the more secure APOP authentication to log into the POP3 server. |
| 75 | \end{methoddesc} |
| 76 | |
| 77 | \begin{methoddesc}{rpop}{user} |
| 78 | Use RPOP authentication (similar to UNIX r-commands) to log into POP3 server. |
| 79 | \end{methoddesc} |
| 80 | |
| 81 | \begin{methoddesc}{stat}{} |
| 82 | Get mailbox status. The result is a tuple of 2 integers: |
| 83 | \code{(\var{message count}, \var{mailbox size})}. |
| 84 | \end{methoddesc} |
| 85 | |
| 86 | \begin{methoddesc}{list}{\optional{which}} |
| 87 | Request message list, result is in the form |
Fred Drake | 3a4ceb7 | 1999-07-07 14:04:38 +0000 | [diff] [blame] | 88 | \code{(\var{response}, ['mesg_num octets', ...])}. If \var{which} is |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 89 | set, it is the message to list. |
| 90 | \end{methoddesc} |
| 91 | |
| 92 | \begin{methoddesc}{retr}{which} |
Eric S. Raymond | 5ac9795 | 2001-01-11 04:19:52 +0000 | [diff] [blame] | 93 | Retrieve whole message number \var{which}, and set its seen flag. |
| 94 | Result is in form \code{(\var{response}, ['line', ...], \var{octets})}. |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 95 | \end{methoddesc} |
| 96 | |
| 97 | \begin{methoddesc}{dele}{which} |
Eric S. Raymond | 5ac9795 | 2001-01-11 04:19:52 +0000 | [diff] [blame] | 98 | Flag message number \var{which} for deletion. On most servers |
| 99 | deletions are not actually performed until QUIT (the major exception is |
| 100 | Eudora QPOP, which deliberately violates the RFCs by doing pending |
| 101 | deletes on any disconnect). |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 102 | \end{methoddesc} |
| 103 | |
| 104 | \begin{methoddesc}{rset}{} |
| 105 | Remove any deletion marks for the mailbox. |
| 106 | \end{methoddesc} |
| 107 | |
| 108 | \begin{methoddesc}{noop}{} |
| 109 | Do nothing. Might be used as a keep-alive. |
| 110 | \end{methoddesc} |
| 111 | |
| 112 | \begin{methoddesc}{quit}{} |
| 113 | Signoff: commit changes, unlock mailbox, drop connection. |
| 114 | \end{methoddesc} |
| 115 | |
| 116 | \begin{methoddesc}{top}{which, howmuch} |
| 117 | Retrieves the message header plus \var{howmuch} lines of the message |
| 118 | after the header of message number \var{which}. Result is in form |
Fred Drake | 3a4ceb7 | 1999-07-07 14:04:38 +0000 | [diff] [blame] | 119 | \code{(\var{response}, ['line', ...], \var{octets})}. |
Eric S. Raymond | 5ac9795 | 2001-01-11 04:19:52 +0000 | [diff] [blame] | 120 | |
| 121 | The POP3 TOP command this method uses, unlike the RETR command, |
| 122 | doesn't set the message's seen flag; unfortunately, TOP is poorly |
| 123 | specified in the RFCs and is frequently broken in off-brand servers. |
| 124 | Test this method by hand against the POP3 servers you will use before |
| 125 | trusting it. |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 126 | \end{methoddesc} |
| 127 | |
| 128 | \begin{methoddesc}{uidl}{\optional{which}} |
| 129 | Return message digest (unique id) list. |
Fred Drake | dbc2d08 | 1999-05-13 18:48:14 +0000 | [diff] [blame] | 130 | If \var{which} is specified, result contains the unique id for that |
| 131 | message in the form \code{'\var{response}\ \var{mesgnum}\ \var{uid}}, |
Fred Drake | 3a4ceb7 | 1999-07-07 14:04:38 +0000 | [diff] [blame] | 132 | otherwise result is list \code{(\var{response}, ['mesgnum uid', ...], |
| 133 | \var{octets})}. |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 134 | \end{methoddesc} |
| 135 | |
| 136 | |
Fred Drake | e2effbd | 1999-04-22 16:21:09 +0000 | [diff] [blame] | 137 | \subsection{POP3 Example \label{pop3-example}} |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 138 | |
| 139 | Here is a minimal example (without error checking) that opens a |
| 140 | mailbox and retrieves and prints all messages: |
| 141 | |
| 142 | \begin{verbatim} |
Guido van Rossum | aac399b | 1998-12-08 16:30:10 +0000 | [diff] [blame] | 143 | import getpass, poplib |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 144 | |
| 145 | M = poplib.POP3('localhost') |
| 146 | M.user(getpass.getuser()) |
Guido van Rossum | aac399b | 1998-12-08 16:30:10 +0000 | [diff] [blame] | 147 | M.pass_(getpass.getpass()) |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 148 | numMessages = len(M.list()[1]) |
| 149 | for i in range(numMessages): |
| 150 | for j in M.retr(i+1)[1]: |
Guido van Rossum | aac399b | 1998-12-08 16:30:10 +0000 | [diff] [blame] | 151 | print j |
Fred Drake | a468404 | 1998-04-24 20:49:02 +0000 | [diff] [blame] | 152 | \end{verbatim} |
| 153 | |
| 154 | At the end of the module, there is a test section that contains a more |
| 155 | extensive example of usage. |