The poolib module from Python's standard library defines POP3 and POP3_SSL classes. POP3 class encapsulates a connection to a POP3 server and implements the protocol as defined in RFC 1939. POP3_SSL classsupports POP3 servers that use SSL as an underlying protocol layer.
POP3 protocolis obsolescent as its implementation quality of POP3 servers is quite poor. If your mailserver supports IMAP, it is recommended to use the imaplib.IMAP4 class.
Both classes have following methods defined −
Returns the greeting string sent by the POP3 server.
Send user command, response should indicate that a password is required.
Get mailbox status. The result contains 2 integers: (message count, mailbox size).
Request message list, result is in the form (response, ['mesg_num octets', ...], octets).
Retrieve message of specified index, and set its seen flag.
Flag message number which for deletion.
Retrieves the message header plus number of lines of the message after the header of message
commit changes, unlock mailbox, drop connection.
Following code retrieves all unread messages from gmail’s POP server.
import poplib box = poplib.POP3_SSL('pop.googlemail.com', '995') box.user("YourGmailUserName") box.pass_('YourPassword') N = len(box.list()) for i in range(N): for msg in box.retr(i+1): print (msg) box.quit()
Before running above script ensure that your gmail account is configure to allow less secure apps.