M MO
M MO

Reputation: 350

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.

Upvotes: 1

Views: 4298

Answers (2)

Lionel Hamayon
Lionel Hamayon

Reputation: 1330

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)

Upvotes: 2

Vladimir
Vladimir

Reputation: 6726

imap_tools BaseMailBox.fetch has mark_seen argument.

It is True by default, so, emails marks as "seen" on fetch by default.

But you can do it manually:

from imap_tools import MailBox, MailMessageFlags
with MailBox('imap.mail.com').login('[email protected]', 'pwd') as mailbox:
    uids = [msg.uid for msg in mailbox.fetch(mark_seen=False)]
    mailbox.flag(uids, MailMessageFlags.SEEN, True)

*Also IMAP has a NEW search criteria

Upvotes: 2

Related Questions