Chapter 14 ■ pOp
266
You will note that the listing uses the email module, introduced in Chapter 12, to great advantage, because even
fancy modern MIME e-mails with HTML and images usually have a text/plain section that the email module can
extract on behalf of a simple program like this for printing to the screen.
If you run this program in the book’s network playground (see Chapter 1), you’ll see output similar to the
following:
$ python3 download-and-delete.py mail.example.com brandon
password: abc123
Message 1 (size is 354 bytes):
From: Administrator [email protected]
To: Brandon [email protected]
Subject: Welcome to example.com!
Read this message [ny]? y
We are happy that you have chosen to use example.com's industry-leading
Internet e-mail service and we hope that you experience is a pleasant
one. If you ever need your password reset, simply contact our staff!
example.com
Delete this message [ny]? y
Deleted.
Summary
POP provides a simple way to download e-mail messages stored on a remote server. With Python’s poplib interface,
you can obtain information about the number of messages in a mailbox and the size of each message. You can also
retrieve or delete individual messages by number.
Connecting to a POP server may lock a mailbox. Therefore, it’s important to try to keep POP sessions as brief as
possible and always call quit() when done.
POP should be used with SSL whenever possible to protect your passwords and e-mail messages’ contents.
In the absence of SSL, try to at least use APOP; send your password in the clear only in dire circumstances where you
desperately need to use POP and none of the fancier options work.
Although POP is a simple and widely deployed protocol, it has a number of drawbacks that make it unsuitable
for some applications. For instance, it can access only one folder, and it does not provide persistent tracking of
individual messages.
The next chapter discusses IMAP, a protocol that provides the features of POP with a number of new features as well.