AI-Powered Phishing Detection & Email Security Platform
Mailguard is a full-stack application that automatically detects phishing emails using machine learning, integrates with Gmail for real-time scanning, and provides detailed analytics through an intuitive dashboard.
π Authentication: Mailguard uses Clerk for modern, secure authentication with multi-factor authentication and social login support.
- π€ AI-Powered Detection: Random Forest ML model with TF-IDF vectorization for accurate phishing detection
- π§ Gmail Integration: OAuth2 integration for secure email fetching and scanning
- π― Real-time Classification: Instant phishing detection with confidence scores
- π Prediction Explainability: Top token-level risk signals for each classification
- π Analytics Dashboard: Comprehensive stats, charts, and email management interface
- π Auto-Retraining: Feedback loop with automatic model retraining capability
- π Secure Authentication: Clerk-based auth with MFA and social login support
- π³ Docker Ready: Complete containerization for easy deployment
- π± Responsive UI: Modern, mobile-friendly interface built with React and TailwindCSS
Frontend
- React 18 + Vite
- TailwindCSS + shadcn/ui
- Clerk React SDK
- Recharts for visualizations
- Nginx for production serving
Backend
- Node.js + Express
- MongoDB for data persistence
- Clerk SDK for authentication
- Gmail OAuth2 integration
- Redis-based caching
ML Service
- Python 3.11 + FastAPI
- scikit-learn (Random Forest)
- TF-IDF vectorization
- Automatic model retraining
- Prediction caching
Mailguard/
βββ docker-compose.yml # Full stack orchestration
βββ .env.docker.example # Environment variables template
βββ backend/ # Node.js API server
β βββ server.js
β βββ controllers/
β βββ models/
β βββ routes/
β βββ Dockerfile
βββ frontend/ # React application
β βββ src/
β βββ nginx.conf
β βββ Dockerfile
βββ ml-service/ # Python ML service
βββ app.py
βββ predictor.py
βββ retrain.py
βββ Dockerfile
- Docker & Docker Compose
- Clerk account (sign up free)
- Google Cloud project with Gmail API enabled (setup guide)
git clone https://github.com/source-rashi/Mailguard.git
cd Mailguard# Copy environment template
cp .env.docker.example .env
# Generate encryption key
node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"Update the following required values:
# Clerk (from https://dashboard.clerk.com)
CLERK_SECRET_KEY=sk_test_your_secret_key
VITE_CLERK_PUBLISHABLE_KEY=pk_test_your_publishable_key
# Google OAuth (from https://console.cloud.google.com)
GOOGLE_CLIENT_ID=your_google_client_id.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=your_google_client_secret
GOOGLE_REDIRECT_URI=http://localhost:5000/api/gmail/callback
# Security (generated above)
ENCRYPTION_KEY=your_64_character_hex_key
# Database (change in production!)
MONGO_ROOT_PASSWORD=your_secure_password# Build and start all services
docker compose up --build
# Or run in detached mode
docker compose up --build -d- Frontend: http://localhost:3000
- Backend API: http://localhost:5000
- ML Service: http://localhost:8000
- MongoDB: mongodb://localhost:27017
- Node.js 18+
- Python 3.11+
- MongoDB running locally
cd backend
npm install
# Create .env file from example
cp .env.example .env
# Edit .env with your credentials
# Start backend server
npm start
# Runs on http://localhost:5000cd frontend
npm install
# Create .env file
cat > .env << EOF
VITE_CLERK_PUBLISHABLE_KEY=your_clerk_key
VITE_API_BASE_URL=http://localhost:5000/api
EOF
# Start dev server
npm run dev
# Runs on http://localhost:5173cd ml-service
pip install -r requirements.txt
# Start ML service
python app.py
# Runs on http://localhost:8000- Create account at Clerk Dashboard
- Create new application
- Copy Publishable Key and Secret Key
- Add to
.env:CLERK_SECRET_KEY(backend)VITE_CLERK_PUBLISHABLE_KEY(frontend)
- Go to Google Cloud Console
- Create new project or select existing
- Enable Gmail API
- Create OAuth 2.0 Client ID credentials
- Add authorized redirect URI:
http://localhost:5000/api/gmail/callback - Copy Client ID and Client Secret to
.env
Required for encrypting Gmail access tokens:
node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"- Create account using email or social login (Google, GitHub, etc.)
- Clerk handles authentication securely
- Click "Connect Gmail" button
- Authorize Mailguard to access your Gmail (read-only)
- Tokens are encrypted and stored securely
- Select time range (last 7 days default)
- Click "Fetch Emails" to scan your inbox
- AI automatically classifies emails as safe or phishing
- View classification results in dashboard
- See confidence scores for each prediction
- Review top model signals that influenced each prediction
- Filter by classification type (All/Safe/Phishing)
- Click thumbs up/down to correct predictions
- Feedback trains the model to improve accuracy
- Collect feedback from users
- Click "Retrain Model" to improve detection
- Model automatically updates and reclassifies emails
# Start all services
docker compose up -d
# View logs
docker compose logs -f
docker compose logs backend # Specific service
# Stop all services
docker compose down
# Rebuild after code changes
docker compose up --build
# Remove volumes (fresh start)
docker compose down -v
# Check service health
docker compose psGET /healthPOST /api/gmail/auth/initiate # Start OAuth flow
GET /api/gmail/callback # OAuth callback
POST /api/gmail/fetch # Fetch emailsGET /api/emails # List emails
POST /api/emails/classify # Classify emails
DELETE /api/emails/:id # Delete email
POST /api/emails/bulk-delete # Delete multiplePOST /predict # Classify email
POST /retrain # Retrain model
GET /health # Service health# Reset everything
docker compose down -v
docker compose up --build
# Check logs
docker compose logs backend
docker compose logs ml-service- Verify redirect URI matches exactly in Google Console
- Check CLIENT_ID and CLIENT_SECRET in
.env - Ensure Gmail API is enabled in Google Cloud project
- Check if
ml-service/phishing_model.pklexists - Verify volume mount in
docker-compose.yml - Rebuild ML service:
docker compose up --build ml-service
If ports are already in use, modify in docker-compose.yml:
ports:
- "3001:80" # Frontend (default 3000:80)
- "5001:5000" # Backend (default 5000:5000)
- "8001:8000" # ML Service (default 8000:8000)- Caching: Redis-based caching for predictions and stats
- Rate Limiting: Development (1000 req/15min), Production (100 req/15min)
- Resource Limits: Configured in docker-compose.yml
- Health Checks: All services have automatic health monitoring
- Never commit
.envfiles - already in.gitignore - Use strong passwords for MongoDB in production
- Enable HTTPS when deploying to production
- Rotate encryption keys periodically
- Review Clerk security settings in dashboard
- Limit Gmail OAuth scopes to read-only access
Contributions welcome! Please:
- Fork the repository
- Create feature branch (
git checkout -b feature/AmazingFeature) - Commit changes (
git commit -m 'Add AmazingFeature') - Push to branch (
git push origin feature/AmazingFeature) - Open Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Clerk for authentication
- scikit-learn for ML capabilities
- FastAPI for ML service framework
- shadcn/ui for UI components
Built with β€οΈ by the Mailguard Team