0

I'm trying to mark email as unseen on Gmail server.

I'm using this command:

res, data = mailbox.uid('STORE', uid, '-FLAGS', '(\Seen)')

Everything goes OK but when I check it using web browser it's still marked as seen. When I check flags here's what I got:

 b'46 (FLAGS (-FLAGS \\Seen))'

I've seen multiple questions on this issue but none of the proposed solutions work.

Just to mention that I'm appending this email using:

mailbox.append(db_email.folder, "-FLAGS \Seen", time.mktime(db_email.date.timetuple()), mail.as_bytes())

But the flag parameter -FLAGS \Seen does not have any effect since it's the same when I don't pass flag argument.

Also, I've double-checked uid for given mail folder and it matches to appropriate email.

sstevan
  • 477
  • 2
  • 9
  • 25
  • Shouldn't it be an `Unseen` flag? – Uriel Oct 19 '16 at 13:30
  • @UrielEli I guess there is only Seen flag and you can set +FLAGS or -FLAGS depending on seen/unseen status. – sstevan Oct 19 '16 at 13:38
  • check their docs, dont guess. – Uriel Oct 19 '16 at 13:40
  • The webui can take some time to change/update to changes made on the IMAP connection. Also -FLAGS only has meaning as a STORE command. It does not make sense as an argument to APPEND. – Max Oct 19 '16 at 14:55
  • @Max I'm appending email to the Inbox folder, since it is shown once I refresh webUI it doesn't make sense not to show unread flag as well. There is `flag` parameter [available](https://docs.python.org/2/library/imaplib.html#imaplib.IMAP4.append) in `append()` method. – sstevan Oct 19 '16 at 15:34
  • Yes, the flag parameter takes a `list of flags` not a flag command. You just need to provide an empty list `()` to not get the SEEN flag. `UID STORE x -FLAGS (\Seen)`is a command to remove an existing seen flag;`()` or `(\Seen) `is a flag list appropriate to be provided to APPEND. – Max Oct 19 '16 at 16:26
  • To be more clear, (UID) STORE takes a subcommand: `FLAGS` = set flags to this list. `+FLAGS` = add these flags in this list. `-FLAGS` = remove these flags from this list. They don't mean positive or negative flags, they're just commands to set, add, or remove. – Max Oct 19 '16 at 16:51
  • @Max thanks, I've removed store command and just passed empty flag list. – sstevan Oct 19 '16 at 18:29

1 Answers1

2

It appears you've misunderstood flags on APPEND a bit.

By doing APPEND folder (-FLAGS \Seen) ... you've actually created a message with two flags: The standard \Seen flag, and a nonstandard -FLAGS flag.

To create a message without the \Seen flag, just use () as your flag list for APPEND.

-FLAGS is a subcommand to STORE, saying to remove these flags from the current list. Conversely, +FLAGS is add these flags to the current list. The plain FLAGS overwrites the current list.

Also, if you do remove the \Seen flag over an IMAP connection, it can take sometime to show up in the GMail WebUI. You may need to refresh or switch folders to get the changes to render.

NB: You are not protecting your backslashes. \S is not a legal escape sequence, so will be passed through, but you should either use a double backslash ('\\Seen') or a raw string (r'\Seen')

Max
  • 10,701
  • 2
  • 24
  • 48