pythonimap-tools

Assigning flags in imap_tools


I am using imap_tools and I am getting errors saying NameError: name 'imap_tools' is not defined. I thought it was maybe the way I imported it, but I have copied it directly from their repo and it isn't working. Has anyone come across this?

from imap_tools import MailBox, AND
with MailBox('imap.gmail.com').login('foobar@gmail.com', 'spam', initial_folder='INBOX') as mailbox:
  for msg in mailbox.fetch('SINCE 08-May-2022'):
    flags = (imap_tools.MailMessageFlags.SEEN, imap_tools.MailMessageFlags.FLAGGED, 'TAG1')
    Mailbox.flag(mailbox.uids(AND(seen=False)), flags, True)

I'm getting the error on line 3 when assigning values to "flags" (I have managed to print the UIDs, so its not a login problem

I have also tried

flags = MailBox(imap_tools.MailMessageFlags.SEEN, imap_tools.MailMessageFlags.FLAGGED, 'TAG1')

and

flags = AND(imap_tools.MailMessageFlags.SEEN, imap_tools.MailMessageFlags.FLAGGED, 'TAG1')

Thank you


Solution

  • You have imported two parts of imap_tools, specifically MailBox & AND functions, so you can use these in your workspace, however you've not actually imported the imap_tools module itself. If you add import imap_tools at the top of your script you should be able to access the rest of the module.

    Edit: This article should help give you some more context if you are just starting out.