Python - POP3


Advertisements

The pop3 protocol is an email protocol to download messages from the email-server. These messages can be stored in the local machine.

Key Points

  • POP is an application layer internet standard protocol.

  • Since POP supports offline access to the messages, thus requires less internet usage time.

  • POP does not allow search facility.

  • In order to access the messaged, it is necessary to download them.

  • It allows only one mailbox to be created on server.

  • It is not suitable for accessing non mail data.

  • POP commands are generally abbreviated into codes of three or four letters. Eg. STAT.

POP Commands

The following table describes some of the POP commands:

S.N.Command Description
1LOGIN
This command opens the connection.
2STAT
It is used to display number of messages currently in the mailbox.
3LIST
It is used to get the summary of messages where each message summary is shown.
4RETR
This command helps to select a mailbox to access the messages.
5DELE
It is used to delete a message.
6RSET
It is used to reset the session to its initial state.
7QUIT
It is used to log off the session.

Pyhton’s poplib module provides classes named pop() and pop3_SSL() which are used to achieve this requirement. We supply the hostname and port number as argument. In the below example we connect to a gmail server and retrieve the messages after supplying the login credentials.

import  poplib

user = 'username' 
# Connect to the mail box 
Mailbox = poplib.POP3_SSL('pop.googlemail.com', '995') 
Mailbox.user(user) 
Mailbox.pass_('password') 
NumofMessages = len(Mailbox.list()[1])
for i in range(NumofMessages):
    for msg in Mailbox.retr(i+1)[1]:
        print msg
Mailbox.quit()

The messages are retrieved when the above program is run.

Useful Video Courses


Video

Python Online Training

Most Popular

187 Lectures 17.5 hours

Malhar Lathkar

Video

Python Essentials Online Training

55 Lectures 8 hours

Arnab Chakraborty

Video

Learn Python Programming in 100 Easy Steps

136 Lectures 11 hours

In28Minutes Official

Video

Python with Data Science

75 Lectures 13 hours

Eduonix Learning Solutions

Video

Python 3 from scratch to become a developer in demand

70 Lectures 8.5 hours

Lets Kode It

Video

Python Data Science basics with Numpy, Pandas and Matplotlib

63 Lectures 6 hours

Abhilash Nelson

Advertisements