Python - IMAP


Advertisements

IMAP is an email retrieval protocol which does not download the emails. It just reads them and displays them. This is very useful in low bandwidth condition. Python’s client side library called imaplib is used for accessing emails over imap protocol.

IMAP stands for Internet Mail Access Protocol. It was first proposed in 1986.

Key Points:

  • IMAP allows the client program to manipulate the e-mail message on the server without downloading them on the local computer.

  • The e-mail is hold and maintained by the remote server.

  • It enables us to take any action such as downloading, delete the mail without reading the mail.It enables us to create, manipulate and delete remote message folders called mail boxes.

  • IMAP enables the users to search the e-mails.

  • It allows concurrent access to multiple mailboxes on multiple mail servers.

IMAP Commands

The following table describes some of the IMAP commands:

S.N.Command Description
1IMAP_LOGIN
This command opens the connection.
2CAPABILITY
This command requests for listing the capabilities that the server supports.
3NOOP
This command is used as a periodic poll for new messages or message status updates during a period of inactivity.
4SELECT
This command helps to select a mailbox to access the messages.
5EXAMINE
It is same as SELECT command except no change to the mailbox is permitted.
6CREATE
It is used to create mailbox with a specified name.
7DELETE
It is used to permanently delete a mailbox with a given name.
8RENAME
It is used to change the name of a mailbox.
9LOGOUT
This command informs the server that client is done with the session. The server must send BYE untagged response before the OK response and then close the network connection.

Example

In the below example we login to a gmail server with user credentials. Then we choose to display the messages in the inbox. A for loop is used to display the fetched messages one by one and finally the connection is closed.

import imaplib
import pprint

imap_host = 'imap.gmail.com'
imap_user = 'username@gmail.com'
imap_pass = 'password'

# connect to host using SSL
imap = imaplib.IMAP4_SSL(imap_host)

## login to server
imap.login(imap_user, imap_pass)

imap.select('Inbox')

tmp, data = imap.search(None, 'ALL')
for num in data[0].split():
	tmp, data = imap.fetch(num, '(RFC822)')
	print('Message: {0}\n'.format(num))
	pprint.pprint(data[0][1])
	break
imap.close()

Depending on the mail box configuration, mail is displayed.

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