Reading email

EZGmail has GmailThread object to represent conversation threads and GmailMessage object for individual emails. The messages attribute of GmailThread object contains a list of GmailMessage objects for each message in the conversation thread.

  • unread() : method returns a list of GmailThread objects for unread threads.
  • summary() : when called on the list of thread objects, returns a summary of the messages in the thread.
  • messages : attribute of GmailThread object which returns a list of GmailMessage objects. Each of these have attributes that describe the email like subject , body , timestamp , sender , and recipient
  • recent() : function that returns a list of GmailThread objects for recent threads. The maxResults parameter is used to set the number of recent mails to display. The default value is 25.

Example:

Python3




import ezgmail
  
# unread emails
unread = ezgmail.unread()
  
print("Summary: "+str(ezgmail.summary(unread)))
print("The first thread in the list: " + str(unread[0]))
print("The first message in the first thread: " + str(unread[0].messages[0]))
  
# attributes of the first message
message = unread[0].messages[0]
  
# subject
print("subject: "+str(message.subject))
  
# body
print("body: "+str(message.body))
  
# sender
print("sender: "+str(message.sender))
  
# timestamp
print("timestamp: "+str(message.timestamp))
  
# recent emails
recent = ezgmail.recent(maxResults=10)
print("List of recent threads: " + str(recent))


Handling mails with EZGmail module in Python

EZGmail is a Python module that can be used to send and receive emails through Gmail. It works on top of the official Gmail API. Although EZGmail does not cover everything that can be done with the Gmail API, it makes the common tasks very simple which are a lot more complicated using Google’s own API. In this article, we will take a look at how to use this module to send, search, read emails and download attachments.

Installation

This module does not comes pre-installed with Python. To install it type the below command in the terminal.

pip install EZGmail

Enabling the Gmail API

First, we need to enable the Gmail API from the official website. Go to the official google developer website  https://developers.google.com/gmail/api/quickstart/python and follow the below steps.

  • Click on Enable the Gmail API button.

  • Select Desktop app as OAuth client.

  • Next, click on Download Client Configuration button.

  • A json file named credentials.json will be downloaded. Copy this file to the working directory of Python.

Authenticating the Application

Next we need to authorise our program to use our Google account. This authorisation process needs to be performed only once.

  • Run ezgmail.init() from Python shell. 
  • This opens up a browser window and takes us to the authentication page. We then need to select the Google account and authorise the application to use our account.

The setup is complete. Now, we shall see how to use this module.

Similar Reads

Sending Email

The send() function is used for sending emails....

Reading email

...

Searching emails

EZGmail has GmailThread object to represent conversation threads and GmailMessage object for individual emails. The messages attribute of GmailThread object contains a list of GmailMessage objects for each message in the conversation thread....

Downloading Attachments

...