Parsing Gmail with Python and mark older dates as "read",

In short, I created a new gmail account and associated with it several other accounts (each of which contains 1000 messages) that I import. All imported messages arrive as unread, but I need them to appear as read.

I have little experience with python, but I only used the mail and imaplib modules to send mail, not account processing.

Is there a way to massively process all the items in the inbox and just mark messages older than the set date as read?

+4
source share
4 answers
typ, data = M.search(None, '(BEFORE 01-Jan-2009)') for num in data[0].split(): M.store(num, '+FLAGS', '\\Seen') 

This is a small code modification in the imaplib doc page for the repository method. I found search criteria for using RFC 3501 . That should get you started.

+8
source

Based on the answer of Philip T. above and RFC 3501 and RFC 2822 , I built a few lines of code to mark emails older than 10 days as read. For short month names, a static list is used. It's not particularly elegant, but the Python% b format string is language dependent, which can cause unpleasant surprises. All IMAP commands are UID based.

 import imaplib, datetime myAccount = imaplib.IMAP4(<imapserver>) myAccount.login(<imapuser>, <password>) myAccount.select(<mailbox>) monthListRfc2822 = ['0', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] beforeDate = datetime.datetime.today() - datetime.timedelta(days = 10) beforeDateString = ("(BEFORE %s-%s-%s)" % (beforeDate.strftime('%d'), monthListRfc2822[beforeDate.month], beforeDate.strftime('%Y'))) typ, data = myAccount.uid('SEARCH', beforeDateString) for uid in data[0].split(): myAccount.uid('STORE', uid, '+FLAGS', '(\Seen)') 

By the way: I don’t know why I had to use β€œ-” as a date separator in the search bar in my case (dovecot IMAP server). It seems to me that this contradicts RFC 2822. However, dates with simple spaces as a separator only returned IMAP errors.

+2
source

Instead of trying to parse our HTML, why not just use the IMAP interface? Connect it to a standard email client, and then just sort by date and mark the ones you want to read.

+1
source

Just go to the Gmail web interface, do an advanced search by date, then select everything and mark as read.

+1
source

All Articles