Email Protocols

Most email today is sent via servers that implement the Simple Mail Transport Protocol (SMTP) and received via servers that implement the Post Office Protocol version 3 (POP3). These protocols are supported by the Python standard library modules smtplib and poplib. Some servers, instead of or in addition to POP3, implement the richer and more advanced Internet Message Access Protocol version 4 (IMAP4), supported by the Python standard library module imaplib, which I do not cover in this book.

The poplib Module

The poplib module supplies a class POP3 to access a POP mailbox. The specifications of the POP protocol are at http://www.ietf.org/rfc/rfc1939.txt.

POP3

class POP3(host,port=110)

Returns an instance p of class POP3 connected to the given host and port.

Instance p supplies many methods, of which the most frequently used are the following.

dele

p.dele(msgnum)

Marks message msgnum for deletion. The server will perform deletions when this connection terminates by calling p.quit. dele returns the server response string.

list

p.list(msgnum=None)

Returns a pair (response,messages), where response is the server response string and messages is a list of strings, each of two words 'msgnum bytes', giving the message number and the length in bytes of each message in the mailbox. When msgnum is not None, messages has only one item: a 'msgnum bytes' for the given msgnum.

pass_

p.pass_(password)

Sends the password. Must be called after p.user. The trailing underscore in the name is ...

Get Python in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.