Build A Newsletter Unsubscribe Tool

With just a few lines of Python.

Vlad Gheorghe
4 min readFeb 10, 2020
Photo by Christa Dodoo on Unsplash

The New Year is a time for cleaning up, and the email inbox should be no exception.

In this spirit, I’ve been looking for a tool that would help me unsubscribe in bulk from undesired newsletters. Inevitably, I found my way to Unroll.me. However, the service is not available in Europe — which is a good thing, if it’s true that it was selling user information to Uber and other companies.

Ironically, the services who should protect us from unwanted intrusion can be more dangerous than the problems they claim to solve.

Well, one way you can make sure a service is completely safe is to build it yourself.

Of course, it’s also a great learning experience. Recently, I’ve become a fan of Joel Grus. Although I can’t match the elegance of his code, I share his philosophy: if you really want to understand how something works, you should build it from scratch.

Accordingly, for this task I’ve used no more than a couple regular expressions and a single Pandas import (which is not essential at all). Overall, the script is about 30 lines long.

The project was done on the Gmail MBOX export, but it could be easily extended to other systems.

Requirements

For this project, you simply need Python 3. I’ve also used Pandas to generate the CSV file. You can easily install it from the command line:

pip install pandas. 

Downloading your emails

The first thing is to download a copy of your emails. Go on Google Takeoutand scroll down until you find Gmail and select it for the export. You get to choose what kind of emails to include in the download. I went ahead and downloaded all of them.

When Google sends you the data (it should take no more than a few hours) open the archive and download the MBOX file in your working folder. For simplicity, I renamed it ‘mail.mbox’.

Extracting the emails

The MBOX is basically a text file. We use Python’s native functions to extract a list of all our mails:

We parse the file line by line, and add every line to the mail string. Whenever a line starts with ‘From’, it marks the beginning of a new mail. So we take all the lines we assembled before that and save them as a single message. Then we start again with the ‘From’ line we just found. The first object of our list will be redundant, because there is no text to assemble before that. So we discard it.

Extracting the unsubscribe links

We’re going to use the ‘List-Unsubscribe’ field in our mails. Business senders are strongly encouraged to use this field. When available, it includes an URL, a MAILTO address or both, which allow users to unsubscribe with one click.

The next function reads every email, extracts the name of the sender, checks if the ‘List-Unsubscribe’ field is available, and then saves the unsubscribe links. If that particular sender has already been recorded, each additional mail adds to its count — so we can see who is sending the most emails.

Saving our results

Time to apply our functions. We build a dictionary of unsubscribe links, turn it into a dataframe and export it to CSV.

That’s it! We created a fast and simple e-mail service unsubscribe system. And like I promised, we used no more than 30 lines of code.

Results

Open the CSV with your favorite software. Now you can easily see who is sending you the most emails, and if you think you had enough, you have a handy unsubscribe link at hand. The names will be a bit messy, but it should be possible to infer the identity of the service and decide whether you want to you keep your subscription or not.

It shouldn’t take long to do this. I noticed that a few services were responsible for most of my subscription-based emails. In fact, although I found 1859 different services, 25 of them were responsible for more than 50% of the emails.

The script will not be able to catch services who didn’t fill out the ‘List-Unsubscribe’ field. However, like I said, I was still able to find several hundreds services.

That’s it! It’s far from perfect, I know. But it’s free, it’s secure, and it’s completely yours.

Thank you for reading!

You can find the script at my GitHub.

Next Steps

The method we have used to extract emails is limited. Check out the mailbox library for a more sophisticated approach.

Moreover, you could use Beautiful Soup to find unsubscribe links that are in the body of the mail rather than in the Unsubscribe field.

Disclaimer

Some of the links you will gather with this script might be unsafe. Proceed with judgment and caution! Only follow links to services which you recognize. I am not responsible for any unintended consequences.

Configuring the MAILTO handle

The MAILTO address will only work if it has a handle with your email. To configure this in Google Chrome, go to Settings. In the Advanced Settings section, select Site Settings. Then go to Handlers and make sure that the option to allow sites to become handlers is enabled. If you change this, you will need to restart your browser.

Now open your Gmail page. On the right of the URL bar, you should see an icon that looks like two joined rhombuses. Click there and allow Google to open email links. Now your MAILTO addresses should work!

--

--

Vlad Gheorghe

I like to learn difficult things and explain them simply.