0

I'm trying to write a script which moves a particular email to Archive after getting its text, however I've looked all over the place and tried plenty of solutions. My understanding is that when a message is archived, all of its labels are removed and it only appears in the All Mail folder.

This is my current code to try to remove any labels associated with the message, I believe that this should archive the message, however it seems to do nothing. There are no errors when running this code.

one, two = self.connection.store(msg_id, '-X-GM-LABELS', "\Inbox \Important \Sent \Seen")

Thanks!

Jerp Dertin
  • 71
  • 1
  • 9
  • Try parenthesizing the flags parameter, and making it a raw string so the backslashes are kept: r"(\Inbox \Important \Sent \Seen)" – Max Jul 06 '16 at 19:41
  • This gives the same output unfortunately! – Jerp Dertin Jul 07 '16 at 08:10
  • 1
    Issuing a UID MOVE command to the "[Gmail]/All Mail" mailbox does the trick. You have to find that mailbox' name using LIST first, unless you're doing this for just one locale. I have no idea how to convince the python imaplib to issue UID MOVE. – arnt Jul 09 '16 at 10:23

1 Answers1

3

The Delete flag is not the same as the Trash flag, so if you set Delete, it will move it to the archive by removing any excess labels

self.connection.store(msg_id, '+FLAGS', '\\Deleted') 
Jerp Dertin
  • 71
  • 1
  • 9
  • It doesn't move it to "archive", but deletes permanently. See https://www.thepythoncode.com/article/deleting-emails-in-python and plenty of other examples with this command (I use it myself, and it really deletes messages). When you move a message to Trash, you can restore that manually. – Yaroslav Nikitenko Mar 10 '22 at 10:35
  • UPD: it looks like sometimes you are right! This depends on your settings in advanced IMAP controls (http://googlesystem.blogspot.com/2008/10/advanced-imap-settings-for-gmail.html): you can set it just to archive mails, or to delete them permanently. In fact I was mistaken, and now will have to find emails again and delete them permanently (they were removed just from my labeled folder). – Yaroslav Nikitenko Mar 10 '22 at 11:56