Skip to content

Automatically unsubscribe from unwanted emails by visiting unsubscribe links found in your Gmail inbox. This Python script uses IMAP, BeautifulSoup, and Requests to extract and visit links efficiently.

Notifications You must be signed in to change notification settings

ankushhKapoor/Python-Email-Auto-Unsubscriber-

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python-Email-Auto-Unsubscribe

This Python script automatically searches through your Gmail inbox for unsubscribe links, visits each link, and logs the result in a file (links.txt). It uses IMAP to access your inbox, BeautifulSoup to extract the links, and Requests to visit each link.

Note: This is not 100% accurate and will only unsubscribe from emails that provide a direct unsubscribe link. Emails requiring you to fill out a form after clicking the unsubscribe link will not be processed. Additionally, some important emails, like banking or service-related messages, might also contain unsubscribe links, and this code could unsubscribe from them as well. Be sure to check your inbox first and modify the code as necessary to fit your needs.

Read my blog for better understanding of working of the code - My Blog Page

Screenshot

This is an example of links.txt which contains the link visited along with it's status code.

Features

  • Connects to Gmail via IMAP and logs into your account securely.
  • Searches all emails for unsubscribe links.
  • Extracts the unsubscribe links from HTML email bodies.
  • Visits each unsubscribe link and logs whether it was successfully visited or failed.
  • Saves the status and link information into a text file (links.txt).

Requirements

  • Python 3.6+
  • Check more below in 3rd step of Setup.

Setup

  1. Clone the repository:

    git clone https://github.com/Ankush1626/Python-Email-Auto-Unsubscribe
    cd Python-Email-Auto-Unsubscribe
  2. .env file: The repository includes a .env file containing environment variables for your Gmail credentials:

    EMAIL="[email protected]"
    PASSWORD="your-app-password"
    
    • Repace [email protected] with your own gmail and your-app-password with your own app password.

    • For Gmail, you should use an App Password instead of your regular email password. To create an App Password, enable Two-Factor Authentication (2FA) on your Google account and generate an app-specific password.

    • Important: The .env file is included to keep your credentials secure. Do not upload or share this file publicly. Ensure that the credentials are correct before running the script.

    • You can generate an app password here.

  3. Install required modules:

    Install the required dependencies listed in the requirements.txt file:

    pip install -r requirements.txt
  4. Run the script:

    Once you've set up the .env file, you can run the script:

    python main.py
  5. The script will generate a links.txt file, where each line will contain the status and the corresponding link, like so:

    Successfully visited - http://example.com/unsubscribe
    Failed to visit (Error 404) - http://example.com/unsubscribe
    

How it Works

  1. The script connects to your Gmail account using IMAP, authenticates using the credentials in the .env file, and searches the inbox for emails containing the word "unsubscribe".
  2. It then extracts the unsubscribe links from the HTML content of those emails.
  3. Each link is visited using the requests library, and the script logs the result (whether the link was successfully visited or an error occurred).
  4. Finally, it writes all the links and their statuses into links.txt.

Dislaimer

This code was not originally developed by me. I learned it from a tutorial by the channel Tech with Tim on Youtube and hold no copyright over this code or its related files.
PS - I did not just copy and paste the code. Instead, I understood it and then rewrote the entire code from scratch.

About

Automatically unsubscribe from unwanted emails by visiting unsubscribe links found in your Gmail inbox. This Python script uses IMAP, BeautifulSoup, and Requests to extract and visit links efficiently.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages