HomePython Page 2 - Python Email Libraries, part 1: POP3
The POP Protocol - Python
Some very useful business software connects with and interacts with email in various ways. If you are building or working with such software, you might want to know how Python accomplishes these tasks. This article series discusses how to use the email libraries built into Python. In this first part, POP3 is covered.
Overall, POP is a relatively simple protocol; for instance, there is no definition for creating multiple message folders as there is in IMAP, as POP simply allows the client to connect and download from a list of messages available on the server. This means that the tasks available while connected to a POP server are relatively few. You can list the messages on the server, download either all or part of a message, and delete messages off of the server when they no longer need to stay there.
Originally, messages were not intended to remain on the server indefinitely, as many people are familiar with in most contemporary email systems. POP clients were designed to connect to the server, list the messages, download those the client wanted to read, and delete the downloaded messages. Nowadays, with most email accounts accessible over the Web as well as with a normal email client, this is not the preferred manner of use. Rather, most clients will download a message and generally leave the message on the server to allow access to the messages from more than a single PC.
Opening a POP Connection
The first step necessary for downloading messages from the server is to open a connection to that server and authenticate the username and password against the server. The objects to connect with a POP server are contained within the poplib library that comes with the standard download of Python. This library contains the POP3 object which actually does the work of connecting to and communicating with the remote server. This task is really pretty simple:
from poplib import * … server = POP3(“18.104.22.168”) print server.getwelcome() print server.user(“user”) print server.pass_(“password”)
The first line in this block imports the necessary objects from the library. Next, the code creates a new POP3 object to connect with the server at 22.214.171.124. As you can tell from the later lines, all this does is open the basic network connection to the server; you must still log in with a username and password.
After creating the server object, we get the welcome string from the server. Some server administrators use this to disseminate information, so it can be a useful thing to know how to get. The last two lines of this code log in with a specific username and password. The second-to-last line sends the username and the last line sends the password.
This example uses the older, relatively insecure POP authentication. Python provides support for two additional authentication schemes. Both APOP and RPOP are implemented in standard Python. When to use each of these authentication schemes depends on the specific server implementation to which the client is connecting.