imap_tools Not Fetching New Emails

Viewed 441

I am trying to use imap_tools to fetch new emails. For some reason it only seems to fetch emails that were already in my inbox when I log in.

Can anyone see what I am doing wrong?

mailbox.login(email, password, initial_folder='INBOX')
print("Logging in")

for _ in range(50):
    try:
        msgs = mailbox.fetch(AND(new=True, subject='Order')) 
        print("Fetching emails")
        for msg in msgs:
            mail = msg.subject
            print(mail)
    except:
        pass

    sleep(1)

mailbox.logout()
print("Logging out")
1 Answers

1st mistake - you are not read "new" description: NEW - have the Recent flag set but not the Seen flag

2nd - instead fetch each second you can use IDLE

from imap_tools import MailBox, A

# waiting for updates 60 sec, print unseen immediately if any update
with MailBox('imap.my.moon').login('acc', 'pwd', 'INBOX') as mailbox:
    responses = mailbox.idle.wait(timeout=60)
    if responses:
        for msg in mailbox.fetch(A(seen=False)):
            print(msg.date, msg.subject)
    else:
        print('no updates in 60 sec')
Related