Note that there are some explanatory texts on larger screens.

plurals
  1. PODelete and append updated message still shows old message
    primarykey
    data
    text
    <p>I have a bunch of messages in a gmail mailbox with slightly mangled headers. I'd like to process and update them automatically using python and gmail's imap interface. I'd like to download the messages, modify the headers locally, then delete it on the server, then add the fixed message back. The problem is that while the message does seem to be properly deleted, after adding it back the old, bad headers are still present. Complicating matters, if I manually delete the message in gmail and then add the message with the same command in python, the new, good headers appear as desired.</p> <pre><code>&gt;&gt;&gt; import imaplib &gt;&gt;&gt; import email &gt;&gt;&gt; mail = imaplib.IMAP4_SSL('imap.gmail.com') &gt;&gt;&gt; mail.login('user@gmail.com', 'password') &gt;&gt;&gt; mail.select('label') ('OK', ['2']) &gt;&gt;&gt; mail.search(None, 'from', 'bad_string') ('OK', ['2']) &gt;&gt;&gt; ret,data = mail.fetch('2', '(RFC822)') &gt;&gt;&gt; msg = email.message_from_string(data1[0][1]) &gt;&gt;&gt; msg['from'] '"Doe, John" &lt;john.doe@bad_string.com&gt;' &gt;&gt;&gt; new = msg['from'].replace('bad_string', 'good_string') &gt;&gt;&gt; msg.replace_header('From', new) &gt;&gt;&gt; msg['from'] '"Doe, John" &lt;john.doe@good_string.com&gt;' &gt;&gt;&gt; mail.store('2', '+FLAGS', '\\Deleted') ('OK', ['2 (FLAGS (\\Seen \\Deleted))']) &gt;&gt;&gt; mail.expunge() ('OK', ['2']) &gt;&gt;&gt; mail.search(None, 'from', 'bad_string') ('OK', ['']) &gt;&gt;&gt; mail.select('label') ('OK', ['1']) </code></pre> <p>At this point, it seems like python sees the email as deleted. Checking in gmail's web interface seems to show it as gone, too. There is only one email in the label instead of two at the beginning and the search returns empty.</p> <pre><code>&gt;&gt;&gt; mail.append('label', None, '"20-Jul-2012 22:30:00 -0400"', str(msg)) ('OK', ['[APPENDUID 24 13] (Success)']) &gt;&gt;&gt; mail.search(None, 'from', 'bad_string') ('OK', ['2']) &gt;&gt;&gt; mail.search(None, 'from', 'good_string') ('OK', ['']) </code></pre> <p>But the message is back with it's original bad string. However, if instead of programmatically marking it as deleted and expunging, I delete and empty the trash in gmail's web interface and then append (still in the same python session as above so running this right after the above output)...</p> <pre><code>&gt;&gt;&gt; mail.append('label', None, '"20-Jul-2012 22:30:00 -0400"', str(msg)) ('OK', ['[APPENDUID 24 14] (Success)']) &gt;&gt;&gt; mail.search(None, 'from', 'bad_string') ('OK', ['']) &gt;&gt;&gt; mail.search(None, 'from', 'good_string') ('OK', ['2']) </code></pre> <p>The IMAP settings in gmail are as follows:</p> <ul> <li>When I mark a message in IMAP as deleted: Auto-Expunge off - Wait for the client to update the server.</li> <li>When a message is marked as deleted and expunged from the last visible IMAP folder: Immediately delete the message forever</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload