This project builds an email classification system that uses a machine learning model to detect and filter rejection emails from Gmail or Outlook. Upon detecting a rejection email, the system can automatically delete the email using IMAP or the Gmail API. The project is split into three main components: Model Training, Email Classification, and Automatic Deletion.
- Project Overview
- Features
- Requirements
- Installation Instructions
- Training the Model
- Email Prediction
- Auto-Delete Email Feature
- File Structure
- Contributing
- License
The Rejection Filter system is designed to classify incoming emails as either "Rejection Emails" or "Normal Emails". It uses Natural Language Processing (NLP) techniques to clean, process, and analyze the email content, using a pre-trained machine learning model to make predictions. When the model detects a rejection email, it can trigger the automatic deletion of the email from Gmail or Outlook accounts.
- Email Classification: Classifies emails as either Rejection Email or Normal Email using a machine learning model.
- Automatic Deletion: Automatically deletes rejection emails from Gmail or Outlook inboxes via IMAP or Gmail API.
- Customizable: The system can be extended to detect different types of emails based on specific keywords or phrases.
- Email Data Preprocessing: Text data is cleaned, and rejection-specific phrases are used to enhance model accuracy.
- Saved Model & Label Encoder: Saves the trained model and label encoder for easy reuse.
To run this project, you will need:
- Python 3.7+
- Libraries:
pandas,sklearn,joblib,numpy,imaplib,gmail-api,email,re,tensorflow(optional depending on models used). - A Gmail/Outlook account (with IMAP enabled).
- 2-Step Verification enabled for Gmail (required to generate App Passwords for Gmail).
git clone https://github.com/yourusername/rejection-filter.git
cd rejection-filterpython3 -m venv venv
source venv/bin/activate # On Windows, use 'venv\Scripts\activate'pip install -r requirements.txt- For Gmail: Enable Gmail API and create a
credentials.jsonfile. Follow the instructions to set up OAuth and obtain your credentials. - For IMAP Access: Enable IMAP in your Gmail settings and ensure you have an App Password if 2FA is enabled.
- Preprocess Email Data: The script
train_model.pycleans and preprocesses the email data by removing unnecessary characters, normalizing the text, and adding rejection-related features. - Train the Model:
- The model is trained using the Naive Bayes classifier to predict rejection emails.
- It uses TF-IDF Vectorizer to convert email text into numerical features.
- Model Saving: The trained model and label encoder are saved using joblib in files
email_classifier_pipeline.pklandlabel_encoder.pklrespectively.
python train_model.pyThis will:
- Preprocess the email data.
- Train the model using the provided dataset (
emails.csv). - Save the trained model and encoder for later use.
Once the model is trained, you can use the trained model to classify incoming emails. The predict.py script loads the model and label encoder, and provides an interface to predict whether an email is a rejection or normal.
python predict.pyYou will need to provide the email content, and the script will output:
- "Rejection Email" or "Normal Email" based on the prediction.
The imap_delete.py script connects to your Gmail or Outlook account via IMAP, reads incoming emails, classifies them, and deletes the rejection emails.
- IMAP: This approach connects to your email account via IMAP, fetches the emails, classifies them using the model, and deletes rejection emails automatically.
- Authentication: You will need to generate an App Password (for Gmail) or provide your login credentials (for IMAP).
python imap_delete.pyThis will:
- Connect to your email account.
- Fetch and classify the latest emails.
- Delete the rejection emails based on the model's prediction.
rejection-filter/
├──.gitignore
├── imap_delete.py # Script for deleting rejection emails from Gmail/Outlook
├── train_model.py # Script for training the classification model
├── predict.py # Script for predicting whether an email is rejection or normal
├── preprocess.py # Data preprocessing and feature extraction (text cleaning, etc.)
├── emails.csv # Dataset of emails for training
├── requirements.txt # Python dependencies
├── label_encoder.pkl # Saved label encoder
└── README.md # This file
If you would like to contribute to this project:
- Fork the repository.
- Clone your forked repository locally.
- Create a new branch for your feature or bug fix.
- Write tests to verify your changes.
- Submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for more details.
- sklearn for the machine learning tools.
- Google API for enabling Gmail interaction.
- IMAP protocol for seamless email processing.
If you encounter any issues, feel free to open an issue in the repository, and I’ll get back to you ASAP!