IMAPLIB METHODS WITH AN EXAMPLE
The imaplib module consists of 3 classes:
These three classes encapsulate a connection between the user and IMAP4 server and adheres to the protocols already defined in RFC 2060, an Internet Message Access Protocol. The version created is IMAP4rev1 and the base class is IMAP4.
The majorly used commands are as listed:
1) IMAP4.append(mailbox, flags, date_time, message): Appends the message attribute to the named mailbox.
2) IMAP4.authenticate(mechanism, authobject): Command used for authentication, which requires response processing.
3) IMAP4.create(): Lets users create a mailbox of the desired name.
4) IMAP.check(): Checkpoint mailbox on the server.
5) IMAP.fetch(message_set,message_parts): Fetch (parts of) messages. message_parts should be a string of message parts with names enclosed within parentheses. Returned data are tuples of the message part envelope and data.
6) IMAP.list(): List mailbox names in directory matching patterns. directory defaults to the top-level mail folder, and pattern defaults to match anything. Returned data contains a list of list responses.
7) IMAP4.login(): The connection is set up and opened via this command.
8) IMAP4.logout(): Shutdowns the connection server.
9) IMAP.select(): Allows to select the mailbox wanted to open by the users.
10) IMAP.open(host,port): Opens the socket at the host port.
11) IMAP.shutdown(): Command to close the connection established in open. This command could be used to override the logout() method.
12) IMAP.socket(): Returns socket instance to connect to server.
13) IMAP.status(): Request named status conditions for mailbox.
14) IMAP.send(data): Sends data to the remote server. This method could be overridden.
15) IMAP.search(charset, criterion[, ...]): Search mailbox for matching messages. charset may be None, in which case no charset will be specified in the request to the server. The IMAP protocol requires that at least one criterion be specified; an exception will be raised when the server returns an error.
The most commonly used commands as part of a project are as listed above. The entire command list however could be found on the Python documentation.
SIMPLE EXAMPLE TO READ EMAILS FROM INBOX:
a)Import the necessary modules. 'imaplib 'to govern the prortocols to setup the connections and to use the methods of the library to read the emails. 'pprint' is to print the output in a neat manner (could be ignored).
Enter the username and user password on the portal host that is being used. In this example. we are setting up a connection with the host, gmail.com. Hence, imap.gmail.com is given as the portal. The username and password thus have to be related to the Gmail account only.
b)Establish a connection using the IMAP.SSL() command listed above, with the argument passed, is the host on which the email is being accessed.
The SSL encrypts the data being transferred between the connection, so as to ensure a secure mechanism to enhance the confidentiality of the data.
c)Once the connection is established, login using the credentials with the IMAP method, login(). Then, using the IMAP.select() method, open the mailbox from which you want to read the emails (ex: inbox, spam, drafts, etc.). In this case, e-mails are being read from the inbox.
d) Now, the crux of the entire program happens. The search() method is used to search the mailbox according to particular criteria. If no such criteria exist, 'ALL' is used to specify that all the emails are to be retrieved.
The data variable is parsed and the messages are fetched using fetch() method. This method returns a tuple of message and envelope format of the email. The message is then printed.
e)The output is printed in a non-readable manner, The entire message is printed in the form of a html format as sent by the sender. This could be enhanced by adding the GUI features using the imaplib.
f) Close the connection established.
Depending on the mailbox configuration, the mail is displayed. The output is printed in the format Gmail receives it through the connections established. The GUI features could be added so as to display the message the way users see it in the Gmail inbox.