简体   繁体   中英

imap_tools Not Fetching New Emails

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")

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')

I faced the same problem and you just need to refresh the inbox before fetching function:

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

for _ in range(50):
    try:
        mailbox.folder.set('INBOX')
        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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM