Skip to content

Instantly share code, notes, and snippets.

@Ashutoshkumargautam
Created December 2, 2020 12:34
Show Gist options
  • Save Ashutoshkumargautam/dbfa48125cf0237661520781b931ca57 to your computer and use it in GitHub Desktop.
Save Ashutoshkumargautam/dbfa48125cf0237661520781b931ca57 to your computer and use it in GitHub Desktop.
import imaplib, email
import time
import sys
#=========================================
user = '[email protected]'
password = 'xxx@123456'
imap_url = 'imap.gmail.com'
#==========================================
#=============[Reading email here..]===============================>>
mail = imaplib.IMAP4_SSL(imap_url, 993)
mail.login(user, password)
num_of_mail = 0
#=======================================================================================================>
while True:
#=======[Loader start here]=========>>
#=======================================================================================>>>
#if any email is not comming in bot email box then wait infinite time....
print("Waitting for website response...")
#------------------------------------------------->
# animation = ["10%", "20%", "30%", "40%", "50%", "60%", "70%", "80%", "90%", "100%"]
animation = ["[■□□□□□□□□□]", "[■■□□□□□□□□]", "[■■■□□□□□□□]", "[■■■■□□□□□□]", "[■■■■■□□□□□]", "[■■■■■■□□□□]", "[■■■■■■■□□□]", "[■■■■■■■■□□]", "[■■■■■■■■■□]", "[■■■■■■■■■■]"]
for i in range(len(animation)):
time.sleep(0.2)
sys.stdout.write("\r" + animation[i % len(animation)])
sys.stdout.flush()
print("\n")
#======[loader end]=================================================>
mail.select('INBOX')
typ, data = mail.search(None, "UNSEEN")
mail_ids = data[0]
id_list = mail_ids.split()
if len(id_list) > num_of_mail:
# ================================================================================>>
# 1st -> if any new comes than read that email store
f = open("test_1.txt", "w")
f.write("true")
f.close()
f = open("test_1.txt", "r")
website_response = f.read()
for i in reversed(id_list):
type, data = mail.fetch(i, '(RFC822)')
for response_part in data:
if isinstance(response_part, tuple):
msg = email.message_from_string(response_part[1].decode('utf-8', errors='ignore'))
num_of_mail = len(id_list)
# ========[condition here]==========================================================================================>>
if website_response == 'true':
#=====[Email reading here....]===========>>
# 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
#===========================================================
# this is done to make SSL connnection 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*****[email protected]"
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>")
#===========================================================
# printtng the required content which we need
# to extract from our email i.e our body
print(data2[0: indexend])
#===========================================================
except UnicodeEncodeError as e:
pass
#============================================================
f = open('mail_data_file.txt', 'w')
f.write(str(x))
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment