Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete the biggest emails from my gmail using a python script?

Tags:

python

gmail

imap

My gmail is getting full... I need a method to find out the biggest email from the Inbox and delete it. However, in the web interface of gmail, what I can do is to find out the emails with attachments first, and then check the attachement size one by one.

The efficiency is too low!

I also find a python script which could log in my gmail account and retrieve emails, via imap protocol, but I didn't find a way to check the attachment size.

Could someone help me? Thanks in advance.

like image 744
ciphor Avatar asked Feb 22 '12 08:02

ciphor


1 Answers

Imap library has search method. There is almost ready to use code for you.

#!/usr/bin/env python
import imaplib
from re import findall

MAXSIZE = 1000
MINSIZE = 1

m = imaplib.IMAP4_SSL('imap.gmail.com')
m.login('[email protected]','testPassword')
m.select()
typ, data = m.search(None, 'ALL')
typ, data = m.search(None,'(SMALLER %d) (LARGER %d)' % (MAXSIZE * 1000,MINSIZE * 1000))
for num in data[0].split():
    typ, data = m.fetch(num, '(RFC822)')
    print 'Message %s\n%s\n' % (num, len(data[0][1]))
m.close()
m.logout()
like image 119
baklarz2048 Avatar answered Oct 16 '22 16:10

baklarz2048