How to remove the largest emails from my gmail using python script?

My gmail is populating ... I need a method to find out the largest email address in my inbox and delete it. However, in the gmail web interface, I can first figure out emails with attachments, and then check the attachment size one at a time.

Efficiency is too low!

I also found a python script that could log in to my gmail account and receive emails through the imap protocol, but I did not find a way to check the attachment size.

Can someone help me? Thanks in advance.

+6
source share
1 answer

Imap library has a 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(' example@gmail.com ','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() 
+4
source

Source: https://habr.com/ru/post/909056/


All Articles