Emails are the most commonly used communication in the today’s digital era. The emails can also be way of communication with IoT devices. The emails can be used to pass commands to IoT devices. The devices can then be programmed and configured to read emails received and act accordingly. There can be interesting applications built this way. For receiving emails, the IoT devices need to configure as an Email Client. They can retrieve emails only over a standard email protocol. Internet Message Access Protocol (IMAP) is one of the standard email message protocols.
It is standard email messaging protocol used worldwide in 21st century. After the internet era started gaining momentum, most of the emailing and messaging tasks were based on POP (Post Office Protocol). As the need of more robust, flexible and advanced messaging protocol emerges, Mark Crispin invented a remote mailbox protocol in 1986 known as IMAP. It was also known as Interactive mail access protocol and interim mail access protocol. IMAP was designed with the goal of complete management of email box from multiple email clients. Clients generally leaves a message on the server until the user explicitly deletes them.
Advantage of IMAP over previous version POP –
IMAP offers several advantages over previous POP versions like as follow –
1) Fast Response – IMAP version 4 stays connected to the server unlike POP where connection is lost briefly after download of mail is completed. IMAPv4 allows faster and swift response because it stays connected to the server.
2) Multiple clients simultaneously connected to same mailbox – IMAP allows multiple clients to be connected to same mailbox unlike POP where currently connected clients’ need to be the only client connected to mailbox. IMAP allows us to open the Gmail from PC, phone, or tab at the same time.
3) Message state information – There are different flags in IMAP for different state of email data. For example, the status of the email can be checked whether it is received, read or replied. These flags are stored at server end, so opening the mailbox from any device is able to update the flag and status can be seen from any device.
4) Server-side Search – IMAP provides the search at server end, so client do not need to download every data to its device for searching. This helps in decreasing data payload and faster use of email database.
Though, there are many advantages of the IMAP, the disadvantage is that it adds complexity in the implementation relative to POP.
Functioning of IMAP protocol –
An IMAP server usually listen to requests on port number 143 and if SSL (Secure Socket Layer) is present then, port number 993. Transmission and Reception of emails is done using two protocols: SMTP (Simple mail transfer protocol) and IMAP. The emails are received using IMAP protocol in the following steps –
1. Open Connection
3. Select Folder
A basic algorithm of receiving emails on IMAP is summarized in the image below –
Fig.1 : Protocol Email Retrieving Algorithm
1. Open Connection – To initialize the mail retrieving process, there must first open a socket object. A socket is internal end point for sending and receiving data at a single node in computer network. The data can be sent or received using this socket object.
2. Authentication: Once there is an established socket, username and password must be transmitted for the authentication. The server will respond OK for correct credentials and query if credentials are incorrect.
3. Select Folder: Once credentials are authorized, the complete mail box access and folders list can be accessed by sending folder commands. The server will respond with folders list such as “Inbox”, “Sent”, “Trash”, “Drafts” etc. Before receiving any mail data from server, a folder must be selected for which user want to see/manage the data.
4. Fetch: Once the folder is selected, the data can be received using fetch commands. The user will receive the list of mails, unread mails and mail data from the selected folder. He has the ability to edit data, mark them Read/Unread or delete if required.
5. Logout: When the user log out of the server, the server closes the connection, thus socket object is closed. No data/signal can be transmitted or received using IMAP once the connection is closed.
While implementing IMAP protocol, STARTTLS can be used to provide secure communication between various nodes of sender/receiver.
All the emailing servers like Gmail, Yahoo supports IMAP protocols for email management. A user is able to read and manage his emails from multiple devices using IMAP. A real-world example is of wearable devices. The emails are stored at Google, yahoo servers and IMAP help in retrieving and managing the email data on the wearables. The wearables devices have a built-in app which supports IMAP. When connected with internet, app initializes a socket object and asks for the username and password. The credentials are sent to the server and after verification generally the user see the data of Inbox folder. The inbox folder is selected in the app by default, and later the UI of the app allows to see other folders too. It allows to manage the mailbox like the user can delete mails, mark them as read/unread etc.
In the next tutorial, learn to receive commands through Email on a Raspberry Pi based IoT device.
Filed Under: Tutorials