| 
									
										
										
										
											1998-08-10 19:42:37 +00:00
										 |  |  | \section{\module{poplib} --- | 
					
						
							| 
									
										
										
										
											1999-04-22 16:21:09 +00:00
										 |  |  |          POP3 protocol client} | 
					
						
							| 
									
										
										
										
											1998-07-23 17:59:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-04-22 16:21:09 +00:00
										 |  |  | \declaremodule{standard}{poplib} | 
					
						
							| 
									
										
										
										
											1998-07-23 17:59:49 +00:00
										 |  |  | \modulesynopsis{POP3 protocol client (requires sockets).} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-04-03 20:13:55 +00:00
										 |  |  | %By Andrew T. Csillag
 | 
					
						
							|  |  |  | %Even though I put it into LaTeX, I cannot really claim that I wrote
 | 
					
						
							|  |  |  | %it since I just stole most of it from the poplib.py source code and
 | 
					
						
							|  |  |  | %the imaplib ``chapter''.
 | 
					
						
							| 
									
										
										
										
											2001-01-11 04:19:52 +00:00
										 |  |  | %Revised by ESR, January 2000
 | 
					
						
							| 
									
										
										
										
											2000-04-03 20:13:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-24 20:49:02 +00:00
										 |  |  | \indexii{POP3}{protocol} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This module defines a class, \class{POP3}, which encapsulates a | 
					
						
							| 
									
										
										
										
											2001-08-14 11:42:13 +00:00
										 |  |  | connection to an POP3 server and implements the protocol as defined in | 
					
						
							| 
									
										
										
										
											2000-07-16 19:01:10 +00:00
										 |  |  | \rfc{1725}.  The \class{POP3} class supports both the minimal and | 
					
						
							| 
									
										
										
										
											1998-04-24 20:49:02 +00:00
										 |  |  | optional command sets. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-11 04:19:52 +00:00
										 |  |  | Note that POP3, though widely supported, is obsolescent.  The | 
					
						
							|  |  |  | implementation quality of POP3 servers varies widely, and too many are | 
					
						
							|  |  |  | quite poor. If your mailserver supports IMAP, you would be better off | 
					
						
							| 
									
										
										
										
											2001-05-09 03:49:48 +00:00
										 |  |  | using the \code{\refmodule{imaplib}.\class{IMAP4}} class, as IMAP | 
					
						
							|  |  |  | servers tend to be better implemented. | 
					
						
							| 
									
										
										
										
											2001-01-11 04:19:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-24 20:49:02 +00:00
										 |  |  | A single class is provided by the \module{poplib} module: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{classdesc}{POP3}{host\optional{, port}} | 
					
						
							|  |  |  | This class implements the actual POP3 protocol.  The connection is | 
					
						
							|  |  |  | created when the instance is initialized. | 
					
						
							|  |  |  | If \var{port} is omitted, the standard POP3 port (110) is used. | 
					
						
							|  |  |  | \end{classdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-04-22 16:21:09 +00:00
										 |  |  | One exception is defined as an attribute of the \module{poplib} module: | 
					
						
							| 
									
										
										
										
											1998-04-24 20:49:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | \begin{excdesc}{error_proto} | 
					
						
							|  |  |  | Exception raised on any errors.  The reason for the exception is | 
					
						
							|  |  |  | passed to the constructor as a string. | 
					
						
							|  |  |  | \end{excdesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-05-09 03:49:48 +00:00
										 |  |  | \begin{seealso} | 
					
						
							|  |  |  |   \seemodule{imaplib}{The standard Python IMAP module.} | 
					
						
							| 
									
										
										
										
											2002-10-18 16:50:17 +00:00
										 |  |  |   \seetitle[http://www.tuxedo.org/\~{}esr/fetchmail/fetchmail-FAQ.html] | 
					
						
							|  |  |  |         {Frequently Asked Questions About Fetchmail} | 
					
						
							|  |  |  |         {The FAQ for the \program{fetchmail} POP/IMAP client collects | 
					
						
							|  |  |  |          information on POP3 server variations and RFC noncompliance | 
					
						
							|  |  |  |          that may be useful if you need to write an application based | 
					
						
							|  |  |  |          on the POP protocol.} | 
					
						
							| 
									
										
										
										
											2001-05-09 03:49:48 +00:00
										 |  |  | \end{seealso} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-24 20:49:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-04-22 16:21:09 +00:00
										 |  |  | \subsection{POP3 Objects \label{pop3-objects}} | 
					
						
							| 
									
										
										
										
											1998-04-24 20:49:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | All POP3 commands are represented by methods of the same name, | 
					
						
							| 
									
										
										
										
											1999-04-22 16:21:09 +00:00
										 |  |  | in lower-case; most return the response text sent by the server. | 
					
						
							| 
									
										
										
										
											1998-04-24 20:49:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | An \class{POP3} instance has the following methods: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-12-05 22:37:21 +00:00
										 |  |  | \begin{methoddesc}{set_debuglevel}{level} | 
					
						
							|  |  |  | Set the instance's debugging level.  This controls the amount of | 
					
						
							|  |  |  | debugging output printed.  The default, \code{0}, produces no | 
					
						
							|  |  |  | debugging output.  A value of \code{1} produces a moderate amount of | 
					
						
							|  |  |  | debugging output, generally a single line per request.  A value of | 
					
						
							|  |  |  | \code{2} or higher produces the maximum amount of debugging output, | 
					
						
							|  |  |  | logging each line sent and received on the control connection. | 
					
						
							|  |  |  | \end{methoddesc} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-24 20:49:02 +00:00
										 |  |  | \begin{methoddesc}{getwelcome}{} | 
					
						
							|  |  |  | Returns the greeting string sent by the POP3 server. | 
					
						
							|  |  |  | \end{methoddesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{methoddesc}{user}{username} | 
					
						
							| 
									
										
										
										
											2000-07-16 19:01:10 +00:00
										 |  |  | Send user command, response should indicate that a password is required. | 
					
						
							| 
									
										
										
										
											1998-04-24 20:49:02 +00:00
										 |  |  | \end{methoddesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{methoddesc}{pass_}{password} | 
					
						
							|  |  |  | Send password, response includes message count and mailbox size. | 
					
						
							|  |  |  | Note: the mailbox on the server is locked until \method{quit()} is | 
					
						
							|  |  |  | called. | 
					
						
							|  |  |  | \end{methoddesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{methoddesc}{apop}{user, secret} | 
					
						
							|  |  |  | Use the more secure APOP authentication to log into the POP3 server. | 
					
						
							|  |  |  | \end{methoddesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{methoddesc}{rpop}{user} | 
					
						
							|  |  |  | Use RPOP authentication (similar to UNIX r-commands) to log into POP3 server. | 
					
						
							|  |  |  | \end{methoddesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{methoddesc}{stat}{} | 
					
						
							|  |  |  | Get mailbox status.  The result is a tuple of 2 integers: | 
					
						
							|  |  |  | \code{(\var{message count}, \var{mailbox size})}. | 
					
						
							|  |  |  | \end{methoddesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{methoddesc}{list}{\optional{which}} | 
					
						
							|  |  |  | Request message list, result is in the form | 
					
						
							| 
									
										
										
										
											1999-07-07 14:04:38 +00:00
										 |  |  | \code{(\var{response}, ['mesg_num octets', ...])}.  If \var{which} is | 
					
						
							| 
									
										
										
										
											1998-04-24 20:49:02 +00:00
										 |  |  | set, it is the message to list. | 
					
						
							|  |  |  | \end{methoddesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{methoddesc}{retr}{which} | 
					
						
							| 
									
										
										
										
											2001-01-11 04:19:52 +00:00
										 |  |  | Retrieve whole message number \var{which}, and set its seen flag. | 
					
						
							|  |  |  | Result is in form  \code{(\var{response}, ['line', ...], \var{octets})}. | 
					
						
							| 
									
										
										
										
											1998-04-24 20:49:02 +00:00
										 |  |  | \end{methoddesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{methoddesc}{dele}{which} | 
					
						
							| 
									
										
										
										
											2001-01-11 04:19:52 +00:00
										 |  |  | Flag message number \var{which} for deletion.  On most servers | 
					
						
							|  |  |  | deletions are not actually performed until QUIT (the major exception is | 
					
						
							| 
									
										
										
										
											2002-10-18 16:50:17 +00:00
										 |  |  | Eudora QPOP, which deliberately violates the RFCs by doing pending | 
					
						
							| 
									
										
										
										
											2001-01-11 04:19:52 +00:00
										 |  |  | deletes on any disconnect). | 
					
						
							| 
									
										
										
										
											1998-04-24 20:49:02 +00:00
										 |  |  | \end{methoddesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{methoddesc}{rset}{} | 
					
						
							|  |  |  | Remove any deletion marks for the mailbox. | 
					
						
							|  |  |  | \end{methoddesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{methoddesc}{noop}{} | 
					
						
							|  |  |  | Do nothing.  Might be used as a keep-alive. | 
					
						
							|  |  |  | \end{methoddesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{methoddesc}{quit}{} | 
					
						
							|  |  |  | Signoff:  commit changes, unlock mailbox, drop connection. | 
					
						
							|  |  |  | \end{methoddesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{methoddesc}{top}{which, howmuch} | 
					
						
							|  |  |  | Retrieves the message header plus \var{howmuch} lines of the message | 
					
						
							| 
									
										
										
										
											2002-10-18 16:50:17 +00:00
										 |  |  | after the header of message number \var{which}. Result is in form | 
					
						
							| 
									
										
										
										
											1999-07-07 14:04:38 +00:00
										 |  |  | \code{(\var{response}, ['line', ...], \var{octets})}. | 
					
						
							| 
									
										
										
										
											2001-01-11 04:19:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | The POP3 TOP command this method uses, unlike the RETR command, | 
					
						
							|  |  |  | doesn't set the message's seen flag; unfortunately, TOP is poorly | 
					
						
							|  |  |  | specified in the RFCs and is frequently broken in off-brand servers. | 
					
						
							|  |  |  | Test this method by hand against the POP3 servers you will use before | 
					
						
							|  |  |  | trusting it. | 
					
						
							| 
									
										
										
										
											1998-04-24 20:49:02 +00:00
										 |  |  | \end{methoddesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{methoddesc}{uidl}{\optional{which}} | 
					
						
							|  |  |  | Return message digest (unique id) list. | 
					
						
							| 
									
										
										
										
											1999-05-13 18:48:14 +00:00
										 |  |  | If \var{which} is specified, result contains the unique id for that | 
					
						
							|  |  |  | message in the form \code{'\var{response}\ \var{mesgnum}\ \var{uid}}, | 
					
						
							| 
									
										
										
										
											1999-07-07 14:04:38 +00:00
										 |  |  | otherwise result is list \code{(\var{response}, ['mesgnum uid', ...], | 
					
						
							|  |  |  | \var{octets})}. | 
					
						
							| 
									
										
										
										
											1998-04-24 20:49:02 +00:00
										 |  |  | \end{methoddesc} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-04-22 16:21:09 +00:00
										 |  |  | \subsection{POP3 Example \label{pop3-example}} | 
					
						
							| 
									
										
										
										
											1998-04-24 20:49:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | Here is a minimal example (without error checking) that opens a | 
					
						
							|  |  |  | mailbox and retrieves and prints all messages: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \begin{verbatim} | 
					
						
							| 
									
										
										
										
											1998-12-08 16:30:10 +00:00
										 |  |  | import getpass, poplib | 
					
						
							| 
									
										
										
										
											1998-04-24 20:49:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | M = poplib.POP3('localhost') | 
					
						
							|  |  |  | M.user(getpass.getuser()) | 
					
						
							| 
									
										
										
										
											1998-12-08 16:30:10 +00:00
										 |  |  | M.pass_(getpass.getpass()) | 
					
						
							| 
									
										
										
										
											1998-04-24 20:49:02 +00:00
										 |  |  | numMessages = len(M.list()[1]) | 
					
						
							|  |  |  | for i in range(numMessages): | 
					
						
							|  |  |  |     for j in M.retr(i+1)[1]: | 
					
						
							| 
									
										
										
										
											1998-12-08 16:30:10 +00:00
										 |  |  |         print j | 
					
						
							| 
									
										
										
										
											1998-04-24 20:49:02 +00:00
										 |  |  | \end{verbatim} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | At the end of the module, there is a test section that contains a more | 
					
						
							|  |  |  | extensive example of usage. |