I want to read the unread mails only but what i have done so far it showing already read mails too please help me find out a way to do it
This is what i have done so far
import imaplib
import email
from bs4 import BeautifulSoup
import webbrowser
m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
m.login("mymail@gmail.com","pass")
m.select('"[Gmail]/All Mail"')
result, data = m.uid('search', None, 'HEADER Subject "Website Amazon Adfice"') # search all email and return uids
if result == 'OK':
for num in data[0].split():
result, data = m.uid('fetch', num, '(RFC822)')
if result == 'OK':
email_message = email.message_from_bytes(data[0][1]) # raw email text including headers
print('From:' + email_message['From'])
print('Subject:'+ email_message['Subject'])
#print('Body:'+ email_message['Body'])
if email_message.is_multipart():
for payload in email_message.get_payload():
# if payload.is_multipart(): ...
print (payload.get_payload())
soup = BeautifulSoup(payload.get_payload(), 'html.parser')
link_text = ""
for a in soup.find_all('a', href=True, text=True):
link_text = a['href']
webbrowser.open(link_text)
else:
print (email_message.get_payload())
m.close()
m.logout()
its reading all the mails including already read mails
Any help would be highly appreciated