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]

Parte VI muletto: i server Postfix e Dovecot

Introduzione

Ultima revisione: Settembre 2010

(Ovviamente) questa guida non è farina del mio sacco ma è l’insieme di più guide.

Una guida completa è Congiuriamo un server di posta completo se Debian etch (postfix) di Il portalinux.

Poi ho consultato anche questa guida su postfix su XS4ALL

Un’altra fonte sono i readme di Postfix Admin.

Per la configurazione di Dovecot ho consulato Johnny Chadda .se, anche se in realtà è solo da decommentare.

Attenzione:Per questo tutorial si userà la shell come root.

Alcune cose le darò per scontato, per esempio la modifica di un file di testo.

Questa, con l’editor nano si fa così: nano nomefile, per salvare un file si usa CTRL+O mentre per uscire dall’editor si fa CTRL+X. … [Leggi il resto]