I have this bot that when subject is x then do something. And I want to make it run in a forever loop and import the newest mail only, not all emails.
Here is the code:
import imaplib
import email
if __name__ == '__main__':
email_user = "x@gmail.com"
email_pass = "y"
mail = imaplib.IMAP4_SSL("imap.gmail.com", 993)
mail.login(email_user, email_pass)
mail.select()
type, data = mail.search(None, 'ALL')
mail_ids = data[0].decode('utf-8')
id_list = mail_ids.split()
mail.select('INBOX', readonly=True)
for i in id_list:
typ, msg_data = mail.fetch(str(i), '(RFC822)')
for response_part in msg_data:
if isinstance(response_part, tuple):
msg = email.message_from_bytes(response_part[1])
if msg['subject'] == 'x':
print("z")
Thank you:)