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

1

6

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.

Detailed answered 22/2, 2012 at 8:41 Comment(1)
you can setup your gmail in thunderbird and sort by size thereJorgensen
H
5

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()
Highborn answered 22/2, 2012 at 9:0 Comment(3)
I'm using imap_server.search(None, '(SMALLER %d)(LARGER %d)' % (10*1024*1000, 5*1024*1000)), but error occurs: imaplib.error: SEARCH command error: BAD ['Could not parse command']Detailed
Same result with python 2.7.2. typ, response = imap_server.search(None, '(SMALLER %d)(LARGER %d)' % (10*102 4*1000, 5*1024*1000)) File "c:\python27\lib\imaplib.py", line 627, in search typ, dat = self._simple_command(name, *criteria) File "c:\python27\lib\imaplib.py", line 1070, in _simple_command return self._command_complete(name, self._command(name, *args)) File "c:\python27\lib\imaplib.py", line 905, in _command_complete raise self.error('%s command error: %s %s' % (name, typ, data)) imaplib.error: SEARCH command error: BAD ['Could not parse command']Detailed
Why would I need to multiply it with 1000? I am taking value from the user in KB or MB. So, do I still need to multiply it?Subdelirium

© 2022 - 2024 — McMap. All rights reserved.