Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

Handling mails with EZGmail module in Python

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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.


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 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.

Sending Email

The send() function is used for sending emails.

Syntax: ezgmail.send(email, sub, text)


  • email : email address of the recipient
  • sub :  Subject of the email
  • text : body of the email



import ezgmail
email = ''
subject = 'EZGmail Test'
text = 'This is the body of the mail.'
ezgmail.send(email, subject, text)

This will send an email to the recipient from our selected Gmail account. 

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.



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))

Searching emails

We can also search for specific emails, in the same way we enter queries in the gmail search box. The search() function returns a list of GmailMessage objects that match the search parameter. We can also perform special search operations like:

  • ‘’ : fetch emails from a particular sender
  • ‘subject:Python’ : emails with a specific subject
  • ‘label:UNREAD’ : unread emails
  • ‘has:attachment’ : emails which contain an attachment



import ezgmail
# searching for specific emails
results ='geeksforgeeks')
# special searches
results ='has:attachment')

Downloading Attachments

The files attached to a message can be downloaded by passing the name of each file to the downloadAttachment() function.

  • The attachments attribute of the GmailMessage object returns a list of names of the files attached with the email.
  • The downloadAttachment() method takes as argument any one of these filenames and downloads that file.
  • The downloadAllAttachments() method is used to download all the files at once.



import ezgmail
# searching emails
# with attachments
results ='has:attachment')
# list of attached filenames
files_attached = results[0].messages[0].attachments
# downloading the first file
# in the list
print('Downloading '+files_attached[0])

My Personal Notes arrow_drop_up
Last Updated : 16 Jul, 2020
Like Article
Save Article
Similar Reads
Related Tutorials