Introduction to poplib

Accessing an email address from Python is very simple if you have POP3 enabled. For this task, can use the poplib library. As an example, I will use Gmail. If you want to try this out for yourself, remember to enable POP3 on the Gmail website. To do this, you need to enter the configuration section inside a Gmail account. You can review Gmail account configuration section of this chapter.

This module defines a class called POP3 that encapsulates a connection to a POP3 server. This class also supports encrypted communication with the TLS protocol.

This module provides two high-level classes:

  • POP()
  • POP3_SSL()

Both classes implement the POP3 and POP3S protocols, respectively. The class constructor for each one accepts three arguments: host, port, and timeout. The optional timeout parameter determines the number of seconds of the connection timeout at the server.

Basically, this module will allow us to connect to a POP3 server, and then authenticate and read the emails. In addition, this module provides a POP3_SSL class, which provides support for connecting to POP3 servers that use SSL as the underlying protocol layer.

As we can see in the documentation on poplib (https://docs.python.org/3/library/poplib.html), the poplib module has two classes with the following constructors:

class poplib.POP3(host[, port[, timeout]])
class poplib.POP3_SSL(host[, port[, keyfile[, certfile]]])

These are the more relevant methods:

  • POP3.user(username): This establishes a user in the connection.
  • POP3.pass_(password): This establishes a password in the connection. Note that the mailbox on the server is locked until the quit() method is called.
  • POP3.getwelcome(): This returns the welcome string that's returned by the POP3 server.
  • POP3.stat(): This gets the status of the mailbox. The result is a tuple of two integers (message count and mailbox size).
  • POP3.list([which]): This requests a list of messages. The result is in the form (response, ['mesg_num octets', ...], octets). If it is configured, it is the message to list.
  • POP3.retr(which): This retrieves the complete message number and configures your view banner.
  • POP3.dele(which): This marks the message number that will be deleted. On most servers, deletions are not carried out until the quit() method is called.
  • POP3.quit(): This allows you to confirm the changes, unlock the mailbox, and release the connection.
  • POP3.top (which, number): This retrieves the header of the message, plus the number of lines of the message after the header of the message number.

To summarize, we have the poplib.POP3 and poplib.POP3_SSL classes to connect to the server (we use the second one if the server has SSL implemented) and the user and pass_ methods to authenticate us. Finally, we have the getwelcome method, which captures the welcome message from the server.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset