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.
This module does not comes pre-installed with Python. To install it type the below command in the terminal.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
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.
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
This will send an email to the recipient from our selected Gmail account.
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.
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:
- ‘from:firstname.lastname@example.org’ : fetch emails from a particular sender
- ‘subject:Python’ : emails with a specific subject
- ‘label:UNREAD’ : unread emails
- ‘has:attachment’ : emails which contain an attachment
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.