Open In App

Python | Fetch your gmail emails from a particular user

Improve
Improve
Like Article
Like
Save
Share
Report

If you are ever curious to know how we can fetch Gmail e-mails using Python then this article is for you.
As we know Python is a multi-utility language which can be used to do a wide range of tasks. Fetching Gmail emails though is a tedious task but with Python, many things can be done if you are well versed with its usage. Gmail provides IMAP access to clients who want to access Gmail without manually logging in the browser.

In setting page, enable this before running script. 

Implementation: 
The libraries used in this implementation includes imaplib, email. You have to manually go and make IMAP access enabled by going into your Gmail account settings. After this only you could access your Gmail account without logging in browser. 

  • Three functions are defined in the implementation which is used to get email body, search for emails from a particular user and get all emails under a label.
  • For showing results I have sent email to my id from my another Gmail account. Now I will be fetching emails from my Gmail account which is received from my another Gmail account.
  • The process begins from making Gmail connection with the help of imaplib library and proving our Gmail login credentials to it.
  • After logging we are selecting emails under the label: Inbox which is a default labeled section for all users. However, you can create your own labels also.
  • Then we are calling get emails function and provide it the parameter from search function result i.e “from user”
  • In get emails function we are putting all emails in an array named “msgs”
  • Now print to see the msgs array
  • Now we can easily iterate over this array. We are iterating it in the order the emails arrived. Then we are searching for the index from where our content begins. This indexing part will be different for different emails/users and the user can manually change the indexes to print only that part which they require.
  • We have our results printed out.

Below is the Python implementation –  

Python3




# Importing libraries
import imaplib, email
 
user = 'USER_EMAIL_ADDRESS'
password = 'USER_PASSWORD'
imap_url = 'imap.gmail.com'
 
# Function to get email content part i.e its body part
def get_body(msg):
    if msg.is_multipart():
        return get_body(msg.get_payload(0))
    else:
        return msg.get_payload(None, True)
 
# Function to search for a key value pair
def search(key, value, con):
    result, data = con.search(None, key, '"{}"'.format(value))
    return data
 
# Function to get the list of emails under this label
def get_emails(result_bytes):
    msgs = [] # all the email data are pushed inside an array
    for num in result_bytes[0].split():
        typ, data = con.fetch(num, '(RFC822)')
        msgs.append(data)
 
    return msgs
 
# this is done to make SSL connection with GMAIL
con = imaplib.IMAP4_SSL(imap_url)
 
# logging the user in
con.login(user, password)
 
# calling function to check for email under this label
con.select('Inbox')
 
 # fetching emails from this user "tu**h*****1@gmail.com"
msgs = get_emails(search('FROM', 'MY_ANOTHER_GMAIL_ADDRESS', con))
 
# Uncomment this to see what actually comes as data
# print(msgs)
 
 
# Finding the required content from our msgs
# User can make custom changes in this part to
# fetch the required content he / she needs
 
# printing them by the order they are displayed in your gmail
for msg in msgs[::-1]:
    for sent in msg:
        if type(sent) is tuple:
 
            # encoding set as utf-8
            content = str(sent[1], 'utf-8')
            data = str(content)
 
            # Handling errors related to unicodenecode
            try:
                indexstart = data.find("ltr")
                data2 = data[indexstart + 5: len(data)]
                indexend = data2.find("</div>")
 
                # printing the required content which we need
                # to extract from our email i.e our body
                print(data2[0: indexend])
 
            except UnicodeEncodeError as e:
                pass


Output: 

Code Explanation: 

  1. The code starts by importing the libraries imaplib and email.
  2. Next, the user’s email address and password are set.
  3. The IMAP URL for Gmail is also imported.
  4. The get_body() function is used to get the body of an email message.
  5. If a message is multipart, then the get_body() function will return the body of the first part of the message.
  6. If a message is not multipart, then get_body() will return the entire message as a string.
  7. The search() function is used to find a key value pair in an email message.
  8. The key can be any string and the value can be anything that can be converted to a Python object (in this case, it’s just a string).
  9. The search() function returns two values: result and data.
  10. result is an instance of SearchResult class, while data is an instance of MIMETextMessage class (which contains all information about an email including its text content).
  11. Finally, get_emails() calls con.fetch().
  12. This method takes three arguments: num (the number representing where in the mailbox we want to start fetching emails), type (the type of mailbox we want to access;
  13. The code imports libraries imaplib and email.
  14. Next, the user is set and password is also set.
  15. The IMAP URL for Gmail is set.
  16. A function to get email content part (body) is defined.
  17. This function takes in a multipart message as input and returns the body part of the message as output.
  18. If there is no multipart message, then it returns the payload of the first message in the list as output.
  19. The function to search for a key value pair is also defined.
  20. This function takes in a key and a value and searches for the key value pair in the given data using the Python regular expression syntax.
  21. If found, it returns an object containing the data associated with the key value pair.

 



Last Updated : 12 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads