Note that there are some explanatory texts on larger screens.

plurals
  1. POUndoing "marked as read" status of emails fetched with imaplib
    text
    copied!<p>I wrote a python script to fetch all of my gmail. I have hundreds of thousands of old emails, of which about 10,000 were unread.</p> <p>After successfully fetching all of my email, I find that gmail has marked all the fetched emails as "read". This is disastrous for me since I need to check all unread emails only. </p> <p>How can I recover the information about which emails were unread? I dumped each mail object into files, the core of my code is shown below:</p> <pre><code>m = imaplib.IMAP4_SSL("imap.gmail.com") m.login(user,pwd) m.select("[Gmail]/All Mail") resp, items = m.uid('search', None, 'ALL') uids = items[0].split() for uid in uids: resp, data = m.uid('fetch', uid, "(RFC822)") email_body = data[0][1] mail = email.message_from_string(email_body) dumbobj(uid, mail) </code></pre> <p>I am hoping there is either an option to undo this in gmail, or a member inside the stored mail objects reflecting the seen-state information. </p> <p>For anyone looking to prevent this headache, consider this answer <a href="https://stackoverflow.com/questions/10991533/imaplib-gmail-how-to-download-full-message-all-parts-while-not-marking-read">here</a>. This does not work for me, however, since the damage has already been done.</p> <p>Edit: I have written the following function to recursively "grep" all strings in an object, and applied it to a dumped email object using the following keywords:</p> <pre><code>regex = "(?i)((marked)|(seen)|(unread)|(read)|(flag)|(delivered)|(status)|(sate))" </code></pre> <p>So far, no results (only an unrelated "Delivered-To"). Which other keywords could I try?</p> <pre><code>def grep_object (obj, regex , cycle = set(), matched = set()): import re if id(obj) in cycle: return cycle.update([id(obj)]) if isinstance(obj, basestring): if re.search(regex, obj): matched.update([obj]) def grep_dict (adict ): try: [ [ grep_object(a, regex, cycle, matched ) for a in ab ] for ab in adict.iteritems() ] except:pass grep_dict(obj) try:grep_dict(obj.__dict__) except:pass try: [ grep_object(elm, regex, cycle, matched ) for elm in obj ] except: pass return matched grep_object(mail_object, regex) </code></pre>
 

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