Mark as unseen on Gmail (imaplib)
<p>I'm trying to mark email as unseen on Gmail server.</p>

<p>I'm using this command:</p>

<pre><code>res, data = mailbox.uid('STORE', uid, '-FLAGS', '(\Seen)')
</code></pre>

<p>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:</p>

<pre><code> b'46 (FLAGS (-FLAGS \\Seen))'
</code></pre>

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

<p>Just to mention that I'm appending this email using:</p>

<pre><code>mailbox.append(db_email.folder, "-FLAGS \Seen", time.mktime(db_email.date.timetuple()), mail.as_bytes())
</code></pre>

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

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

<p>It appears you've misunderstood flags on APPEND a bit.</p>

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

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

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

<p>Also, if you do remove the <code>\Seen</code> 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.</p>

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

python email gmail imap imaplib