python imaplib - mark email as unread or unseen

In Python, the imaplib module describes STORE as:

(typ, [data]) = <instance>.store(message_set, command, flags)

so, the following line will let you set the message to READ ('+FLAGS') or UNREAD ('-FLAGS') as required.

connection.uid('STORE', MESSAGE_ID, '+FLAGS', '\SEEN')

As you see, the secrets is on the FLAGS command ;)


You can easily clear the \Seen flags with this command:

tag UID STORE -FLAGS (\Seen)

but your software will probably be more robost if you only set the \Seen flag in the first place after you have successfully processed a message. That way, if anything goes wrong while you are processing a message (even if the connection to the IMAP server is broken) the flag remains unset and you can retry that message the next time the script runs. You do this by avoiding the IMAP server's automatic setting of the \Seen flag by using BODY.PEEK instead of BODY.

In Python, I think that STORE command should be issued like this but I haven't tried it.

connection.uid('STORE', '-FLAGS', '(\Seen)')

`imap = imaplib.IMAP4_SSL(server)
 imap.login(username, password)
 imap.select("inbox", readonly=False)`

if readonly="True" you can't change any flags. But,if it is false, you can do as follow,

imap.store(id, '-FLAGS', '\Seen')

THEN EMAIL WILL MARK AS UNREAD

(-) means REMOVE flag and (+) means ADD flag.

ex:you can set imap.store(id, '+FLAGS', '\Deleted') to delete email as well.
Like this you can set,any flag in below

    \Seen       Message has been read

    \Answered   Message has been answered

    \Flagged    Message is "flagged" for urgent/special attention

    \Deleted    Message is "deleted" for removal by later EXPUNGE

    \Draft      Message has not completed composition (marked as a
                draft).

More details :https://www.rfc-editor.org/rfc/rfc2060.html#page-9


You may use imap_tools package: https://pypi.org/project/imap-tools/

from imap_tools import MailBox, MailMessageFlags, A

with MailBox('imap.mail.com').login('[email protected]', 'pwd', 'INBOX') as mailbox:

    # FLAG unseen messages in current folder as Answered and Flagged, *in bulk.
    flags = (MailMessageFlags.ANSWERED, MailMessageFlags.FLAGGED)
    mailbox.flag(mailbox.uids(A(seen=False)), flags, True)

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

I am lib author.

Tags:

Python

Imap