# Importing librariesimportimaplib,emailuser='USER_EMAIL_ADDRESS'password='USER_PASSWORD'imap_url='imap.gmail.com'# Function to get email content part i.e its body partdefget_body(msg):ifmsg.is_multipart():returnget_body(msg.get_payload(0))else:returnmsg.get_payload(None,True)# Function to search for a key value pairdefsearch(key,value,con):result,data=con.search(None,key,'"{}"'.format(value))returndata# Function to get the list of emails under this labeldefget_emails(result_bytes):msgs=[]# all the email data are pushed inside an arrayfornuminresult_bytes[0].split():typ,data=con.fetch(num,'(RFC822)')msgs.append(data)returnmsgs# this is done to make SSL connection with GMAILcon=imaplib.IMAP4_SSL(imap_url)# logging the user incon.login(user,password)# calling function to check for email under this labelcon.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 gmailformsginmsgs[::-1]:forsentinmsg:iftype(sent)istuple:# encoding set as utf-8content=str(sent[1],'utf-8')data=str(content)# Handling errors related to unicodenecodetry: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 bodyprint(data2[0:indexend])exceptUnicodeEncodeErrorase:pass