Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python imaplib - mark email as unread or unseen

Tags:

python

imap

Searching here and on the internet, there are a lot of examples to how to mark a message as SEEN, even though this is automatic with imap.

But how can I mark an email as UNSEEN or UNREAD.

I have a script in python which receives UNSEEN messages, and it works great. But after reading them, imap automatically marks them as SEEN which works fine but only if the script has no errors, because if it raises an exception, I want the email to be marked again as UNSEEN, so next time the script will read that message again.

How can I achieved this?

I have also used mail.select(mail_label,readonly=True), but it doesn't help because with that I cannot mark a message as SEEN which I also need. I also want this to work with Gmail.

like image 487
eLRuLL Avatar asked Jun 28 '13 14:06

eLRuLL


4 Answers

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)')
like image 92
Celada Avatar answered Sep 23 '22 03:09

Celada


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 ;)

like image 45
achalar Avatar answered Sep 24 '22 03:09

achalar


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.

like image 42
Vladimir Avatar answered Sep 24 '22 03:09

Vladimir


`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

like image 28
Sachintha Dhananjana Avatar answered Sep 21 '22 03:09

Sachintha Dhananjana