Piero V.

Bulk delete emails with Python

Recently, I had to check a legacy email address we used at home until a few years ago.

We almost stopped using it, but some services are still tied to it. The result is that whenever I need to check it, I find tons of spam, and the web interface is kinda bad, so it takes ages to delete all of it.

Some months ago, I learned to interact with IMAP from Python when I wrote a script to download and back up email accounts.

So, I modified it to output a CSV with all the senders and subjects of the emails in the mailbox. The scripts left a column empty to mark the emails that should be deleted with an x.

Then, I wrote another script to read the modified changes and to move the marked emails to the trash for a final review before emptying it.

Recently, I found myself in the same situation again, but I did not keep these scripts, so I had to write a new one.

It did not take much, but since it might be helpful to someone (at least the future me), I decided to share it here.

It takes the parameters to connect to the IMAP server on the command line. Then, it takes the action (write-csv to create the CSV with the list of emails, or read-csv to read it to send the changes to the server), and finally the name of the CSV file.

The script is released in the public domain, and, as always, it comes without any warranty.

IMAP downloader

Two weeks ago, I needed to mass-download a few IMAP mailboxes before migrating them to another provider.

Gist contains many scripts to do so, and I tried one of them, but it did not work as I wanted. I wanted to download all the IMAP folders, not only Inbox.

Therefore, I wrote my version of that script 😄️.

I used imapclient instead of the built-in imaplib because it is more Pythonic and handles all the tedious conversions between bytes, strings, and other types.

The center of the script is the process function. It connects to an IMAP server, queries the list of folders, and downloads all the messages from each one but trash and spam (but notice that the comparison is case-sensitive!).

client.fetch downloads all the messages you provide to it, which caused an OOM in my case. Therefore, I split the list with the message IDs into chunks. I had to write the batches function because I was running on Python 3.11. From Python 3.12, you can use itertools.batched instead. … [Leggi il resto]

Another rant on emails

A pair of weeks ago, some people asked me for help because their Outlook was not working anymore. It complained that its data file had reached the maximum size.

Being a Linux user, I have never had Outlook on my machines.

I used it only at my previous job, with an exchange server. I must admit it used to work fine. It even has some appreciable features, such as the company directory, shared calendars that work immediately, etc.

So, when people ask me if I can help them with their Outlook problems, I try to answer as a courtesy. Usually, they are easy problems, and I solve them by searching for their error messages in English and translating the solution into Italian.

One of the suggested solutions for the problem was to empty the bin. But it contained almost one million emails 😨️. So, I went to the webmail and found that the webmail also has the same amount of emails to delete.

The web client also says that emails are automatically deleted after 90 days, but it contained emails from more than two years ago.

It also has a button to empty the folder… but I think it communicates to the backend through IMAP itself, and of course, it went on timeout 😄️. … [Leggi il resto]

Qualcosa è andato storto

L’email è una tecnologia che potremmo dire venga dal paleolitico dell’informatica: SMTP è datato 1982, MIME 1996 (RFC 2045), eppure oggi mi sono trovato in difficoltà.

Il mio obiettivo era stampare i biglietti per l’aereo a mia sorella, una cosa che non dovrebbe richiedere 50 minuti.

Ma partiamo dalla radice del problema: Lufthansa ha pensato di inviare il biglietto come mail HTML con le varie immagini di cui è composto come ulteriori allegati. Questa cosa è possibile, ed è regolata dalla RFC 2392 (Content-ID and Message-ID Uniform Resource Locators, datata agosto 1998), tuttavia, in generale, fare una mail in HTML con immagini non è mai una cosa banale, esistono diverse soluzioni, ma nessuna perfetta.

Una soluzione ottima sarebbe stato un bel file PDF, aperto tranquillamente da ogni dispositivo, salvato e gestito come meglio si crede.

Ciò che però ha veramente causato il problema è stato il modo in cui io ho ricevuto l’email in questione: inoltrata. … [Leggi il resto]

Dovremmo fare un guinness alla catena di Sant'Antonio più inviata

Ogni tanto apro l’Hotmail per vedere che spazzatura mi inviano.

Oggi trovo in duplice copia dallo stesso mittente una mail cazzata che dice che la Microsoft pagherà soldi a chi la invia…

Sessè…

Ho contato le firme che aggiunge hotmail da sola e sono 117.

Sono più lunghi gli indirizzi dei destinatari che il contenuto della pagina.

A sto punto mi chiedo: esiste un guinness per la catena più inviata?