Created
December 2, 2020 10:20
-
-
Save Ashutoshkumargautam/27d6d08ae38f42cc293e7ab52072139c to your computer and use it in GitHub Desktop.
Reading email in loop with python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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....]===========>> | |
# Connect to inbox | |
imap_server = imaplib.IMAP4_SSL(host='imap.gmail.com') | |
imap_server.login('[email protected]', 'xxx@123456') | |
#============================================================ | |
imap_server.select() # Default is `INBOX` | |
_, message_numbers_raw = imap_server.search(None, 'ALL') | |
for message_number in message_numbers_raw[0].split(): | |
_, msg = imap_server.fetch(message_number, '(RFC822)') | |
x = msg[0][1] | |
#============================================================ | |
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
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)')