pythonsmtpimapimaplibimap-tools

How to get unseen emails using python imap-tools


from imap_tools import MailBox, AND
import re

yahooSmtpServer = "imap.mail.yahoo.com"

client = MailBox(yahooSmtpServer).login('myEmail', 'myPassword', 'INBOX')
for msg in client.fetch(AND(seen=False)):
    mail = msg.html
    print(mail)
            

I want t get unseen messages in my mail as soon as they'll appear in my inbox. looping through this code I can always check for unseen messages but it's really troublesome and I don't know how to flag a message as read.

so is there any way I can get unseen messages in my yahoo mail inbox using IMAP-tools? if not... can I do it using another library? Thank you.


Solution

  • from imaptools documentation and this example:

    # SEEN: flag as unseen all messages sent at 05.03.2007 in current folder, *in bulk
    mailbox.flag(mailbox.fetch("SENTON 05-Mar-2007"), MailMessageFlags.SEEN, False)
    

    it seems this code should work:

    client = MailBox(yahooSmtpServer).login('myEmail', 'myPassword', 'INBOX')
    for msg in client.fetch(AND(seen=False)):
        mail = msg.html
        print(mail)
    # pass the email uid and bool here
        client.flag(msg.uid, MailMessageFlags.SEEN, True)