Automate Email Unsubscribing with Python Bot

Automate Email Unsubscribing with Python Bot

Table of Contents

  1. Introduction
  2. Install Python Libraries
  3. Setting up Google Account
  4. Writing the Code
  5. Retrieving Emails from Inbox
  6. Extracting Unsubscribe Links
  7. Opening Unsubscribe Links
  8. Testing and Troubleshooting
  9. Conclusion
  10. Resources

Introduction

In this article, we will learn how to Create a Python bot that automates the process of unsubscribing from long subscriptions in your email account. Have you ever found it time-consuming to click on each unsubscribe link individually? This Python bot will make it easier for you by automatically opening the webpages containing the unsubscribe links. So, let's get started!

Install Python Libraries

Before we dive into writing the code, we need to ensure that we have certain libraries installed in Python. These libraries are essential for our bot to function properly. We will primarily be using beautifulsoup4, imapclient, and lxml libraries. If You haven't installed them yet, follow the instructions provided below:

  1. Open the Windows Run dialog by pressing Windows Key + R.
  2. Type "cmd" and hit Enter to open the Command Prompt.
  3. In the Command Prompt, use the following commands to install the required libraries:
    • pip install beautifulsoup4
    • pip install imapclient
    • pip install lxml

Setting up Google Account

To use this bot with a Google account, we need to make a few changes to our account settings. Follow the steps below:

  1. Go to your Google Account Settings.
  2. Click on the "Security" tab.
  3. Turn off 2-step verification temporarily to allow the script to run smoothly.
  4. Scroll down and enable "Less secure app access" to grant access to our program.

Please note that these settings should be adjusted only for the duration of running the script to ensure the security of your account.

Writing the Code

Now, let's start writing the code to create our Python bot. We will need to import the necessary libraries, define server and port settings, and establish a connection to the email account. Additionally, we will retrieve the emails from the inbox, extract the unsubscribe links, and open them in a web browser.

import webbrowser
import imaplib
from bs4 import BeautifulSoup
import email

# Code continues...

Retrieving Emails from Inbox

To begin retrieving the emails from the inbox, we need to establish a connection to the email account, select the inbox, and fetch the necessary data. We will also count the number of messages in the inbox.

# Establishing connection to the email account
client = imaplib.IMAP4_SSL("imap.gmail.com") # Replace with your IMAP server
client.login(email_address, password) # Replace with your email ID and password

# Selecting the inbox and retrieving the number of messages
client.select("inbox", readonly=True)
_, data = client.search(None, "ALL")
number_of_messages = len(data[0].split())

Extracting Unsubscribe Links

Next, we will extract the unsubscribe links from the email messages. We will iterate through each message, parse it using beautifulsoup4, and find the links. Then, we will filter out only the links that are for unsubscribing.

for num in data[0].split():
    _, email_data = client.fetch(num, "(RFC822)")
    _, b = email_data[0]
    email_message = email.message_from_bytes(b)

    for part in email_message.walk():
        if part.get_content_type() == "text/html":
            email_body = part.get_payload(decode=True)
            soup = BeautifulSoup(email_body, "lxml")
            links = soup.find_all("a")

            for link in links:
                if "unsubscribe" in link.text.lower():
                    print(link.get("href"))

Opening Unsubscribe Links

Once we have extracted the unsubscribe links, we can open them in a web browser using the webbrowser module. This will allow us to quickly unsubscribe from the services.

# Opening the unsubscribe links in web browser
for link in links:
    if "unsubscribe" in link.text.lower():
        webbrowser.open(link.get("href"))

Testing and Troubleshooting

After writing the code, it is essential to test and troubleshoot any errors that might occur. Ensure that all the necessary libraries are installed and the email account settings are configured correctly. If you encounter any issues, refer to the provided resources or Seek help in the comments.

Conclusion

In this article, we have learned how to create a Python bot that automates the process of unsubscribing from email subscriptions. By using the beautifulsoup4, imapclient, and lxml libraries, we were able to retrieve the emails from the inbox, extract the unsubscribe links, and open them in a web browser. This bot can save you time and effort by simplifying the process of managing your subscriptions effectively.

Resources

Find AI tools in Toolify

Join TOOLIFY to find the ai tools

Get started

Sign Up
App rating
4.9
AI Tools
20k+
Trusted Users
5000+
No complicated
No difficulty
Free forever
Browse More Content