Personal tools
You are here: Home Weblog Archive your Gmail inbox from Python

Archive your Gmail inbox from Python

Posted by Sean Fulmer at Mar 28, 2009 07:15 PM |

A quickie Python script to clean out your Gmail inbox... I run this whenever my Gmail notifier has notified me of a bunch of mail that I don't really need to read.

import imaplib

gmail_user = 'your_username'
gmail_pass = 'your_password'

M = imaplib.IMAP4_SSL('imap.gmail.com')

M.login(gmail_user, gmail_pass)

M.select()

typ, data = M.search(None, 'ALL')

for num in data[0].split():
    M.store(num, '+FLAGS', '\\Seen')
    M.store(num, '+FLAGS', '\\Deleted')

M.expunge()

A couple of points:

  • When I mention 'archive' in the title, I mean the Gmail 'Archive' feature - ie, remove the message from the inbox but keep it in 'All Mail'.
  • You need to have IMAP enabled on your Gmail account.
  • Don't let that '\\Deleted' stuff scare you - in Gmail's IMAP implementation, that removes the message from the inbox, but it doesn't actually delete it.

(... he said, ignoring the fact that he hadn't posted anything here in the past seven months.)

Document Actions
About Me
Sean Fulmer is a Plone developer/integrator and tech support geek.
Clicky Web Analytics