1
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.

Vladimir
  • 6,162
  • 2
  • 32
  • 36
M MO
  • 323
  • 4
  • 16

2 Answers2

2

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)
Vladimir
  • 6,162
  • 2
  • 32
  • 36
Lionel Hamayon
  • 1,240
  • 15
  • 25
  • 1
    it works but we have to pass msg.uid instead of msg. client.seen(msg.uid, True) now it works. thank you – M MO May 17 '21 at 10:53
2

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('test@mail.com', '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

Vladimir
  • 6,162
  • 2
  • 32
  • 36
  • but for some reason, it did not happen by default... but yeah this method works. thanks. – M MO May 17 '21 at 12:16