# -*- coding: utf8 -*- # # Compter les emails BEFORE DATE # from pprint import pprint import datetime import imaplib # connecter au serveur IMAP conn = imaplib.IMAP4_SSL('mail.gandi.net') conn.login('bureau@avnes.org', 'ConCho#56') # lister les dossiers typ, data = conn.list() print('Liste des dossiers :') pprint(data) # delete mails before 14 years before_date = (datetime.date.today() - datetime.timedelta(365.25 * 13)).strftime("%d-%b-%Y") print("Delete emails before " + before_date) # select ALL conn.select('INBOX') rv, data = conn.search(None, '(BEFORE {0})'.format(before_date)) nb_mails = str(len(data[0])) print(nb_mails + " emails founded") resp = input ("Enter 'c' to continue, or 'a' to abort : ") if resp=="c": print("Moving " + nb_mails + " emails to Trash") messages = data[0].split(b' ') for mail in messages: # move to trash conn.store(mail, '+X-GM-LABELS', '\\Trash') #This block empties trash, remove if you want to keep, Gmail auto purges trash after 30 days. print("Emptying Trash & Expunge...") conn.select('[Gmail]/Corbeille') conn.store("1:*", '+FLAGS', '\\Deleted') # delete all the selected messages conn.expunge() print("Script completed") else: print("Script aborted") # deconnexion du serveur conn.close() conn.logout()