Skip to content

ersimransingh/email-python

Repository files navigation

Python Email Service API

A comprehensive RESTful API service for email processing with queue management, built with FastAPI and SQL Server.

πŸš€ Features

  • Authentication: JWT-based secure authentication
  • Database Integration: SQL Server connection with connection pooling
  • Email Processing: Queue-based email processing with PDF attachments
  • Scheduling: Configurable background processing with APScheduler
  • Monitoring: Real-time dashboard and service status
  • Security: Encrypted configuration storage
  • API Documentation: Auto-generated Swagger/OpenAPI documentation

πŸ“‹ Requirements

  • Python 3.11+
  • SQL Server (with ODBC Driver 17)
  • Redis (optional, for advanced features)

πŸ› οΈ Installation & Running

πŸš€ Quick Start (Recommended)

Option 1: Using the run script (Python)

python run.py

Option 2: Using the batch file (Windows)

run.bat

Option 3: Manual installation

# 1. Install core dependencies
pip install fastapi uvicorn pydantic python-jose passlib email-validator sqlalchemy apscheduler aiosmtplib

# 2. Start the server
python -m uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload

🐳 Docker Installation

# Using Docker Compose (Recommended)
docker-compose up -d

# View logs
docker-compose logs -f email-service

# Stop services
docker-compose down

🐳 Docker Deployment

Using Docker Compose

# Build and start services
docker-compose up -d

# View logs
docker-compose logs -f

# Stop services
docker-compose down

Using Docker only

# Build image
docker build -t email-service .

# Run container
docker run -d -p 8000:8000 --name email-service-container email-service

πŸ“š API Documentation

Once the application is running, access the documentation at:

πŸ”§ Configuration

Database Setup

  1. Create the required tables in your SQL Server database:
-- Email Configuration Table
CREATE TABLE tbl_EMailParameters (
    id INT IDENTITY(1,1) PRIMARY KEY,
    SMTPServer NVARCHAR(255),
    SMTPPort INT,
    SMTPAccountName NVARCHAR(255),
    SMTPPassword NVARCHAR(255),
    SMTPMailId NVARCHAR(255),
    ApplicationName NVARCHAR(255),
    SMTPSSLFlag NVARCHAR(1),
    ParamCode NVARCHAR(50),
    IsActive NVARCHAR(1) DEFAULT 'Y'
);

-- Email Queue Table
CREATE TABLE Digital_Emaildetails (
    dd_srno INT IDENTITY(1,1) PRIMARY KEY,
    dd_document VARBINARY(MAX),
    dd_filename NVARCHAR(255),
    dd_toEmailid NVARCHAR(500) NOT NULL,
    dd_ccEmailid NVARCHAR(500),
    dd_subject NVARCHAR(500) NOT NULL,
    dd_bodyText NTEXT NOT NULL,
    dd_SendFlag NVARCHAR(1) DEFAULT 'N',
    dd_EmailParamCode NVARCHAR(50),
    dd_RetryCount INT DEFAULT 0,
    dd_SentDate DATETIME,
    dd_BounceReason NVARCHAR(500),
    dd_LastRetryDate DATETIME,
    created_at DATETIME DEFAULT GETDATE(),
    updated_at DATETIME DEFAULT GETDATE()
);

Initial Configuration

  1. Authenticate: POST /api/authenticate

    {
      "username": "admin",
      "password": "admin"
    }
  2. Configure Database: POST /api/save-config

    {
      "server": "localhost",
      "port": 1433,
      "user": "sa",
      "password": "YourPassword123",
      "database": "EmailDB"
    }
  3. Configure Email Service: POST /api/save-email-config

    {
      "startTime": "09:00",
      "endTime": "18:00",
      "interval": 30,
      "intervalUnit": "minutes",
      "dbRequestTimeout": 30000,
      "dbConnectionTimeout": 30000,
      "username": "emailuser",
      "password": "emailpass"
    }
  4. Start Email Service: POST /api/service-control

    {
      "action": "start",
      "user": "admin"
    }

πŸ§ͺ Testing

Using curl

# Health check
curl http://localhost:8000/health

# Authenticate
curl -X POST "http://localhost:8000/api/authenticate" \
  -H "Content-Type: application/json" \
  -d '{"username":"admin","password":"admin"}'

# Test email
curl -X POST "http://localhost:8000/api/email-test" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"email":"test@example.com"}'

Using Postman

Import the provided Postman collection for comprehensive API testing.

πŸ“ Project Structure

email-service-python/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ main.py                 # FastAPI application
β”‚   β”œβ”€β”€ api/                    # API endpoints
β”‚   β”‚   β”œβ”€β”€ auth.py            # Authentication
β”‚   β”‚   β”œβ”€β”€ database.py        # Database configuration
β”‚   β”‚   β”œβ”€β”€ email.py           # Email processing
β”‚   β”‚   └── service.py         # Service control
β”‚   β”œβ”€β”€ core/                   # Core functionality
β”‚   β”‚   β”œβ”€β”€ config.py          # Configuration models
β”‚   β”‚   └── security.py        # Security & encryption
β”‚   β”œβ”€β”€ models/                 # Data models
β”‚   β”‚   β”œβ”€β”€ auth.py            # Auth models
β”‚   β”‚   β”œβ”€β”€ database.py        # Database models
β”‚   β”‚   └── email.py           # Email models
β”‚   └── services/               # Business logic
β”‚       β”œβ”€β”€ database_manager.py # Database operations
β”‚       β”œβ”€β”€ email_service.py    # Email functionality
β”‚       └── email_worker.py     # Background processing
β”œβ”€β”€ config/                     # Configuration files
β”œβ”€β”€ temp/                       # Temporary files
β”œβ”€β”€ logs/                       # Log files
β”œβ”€β”€ requirements.txt            # Python dependencies
β”œβ”€β”€ Dockerfile                  # Docker configuration
β”œβ”€β”€ docker-compose.yml          # Docker Compose setup
└── README.md                   # This file

πŸ” Security Features

  • JWT-based authentication with configurable expiration
  • Encrypted storage for sensitive configuration data
  • Password hashing using bcrypt
  • SQL injection prevention with parameterized queries
  • CORS configuration for cross-origin requests

πŸ“Š Monitoring & Logging

  • Real-time dashboard with service statistics
  • Health check endpoints
  • Structured logging with configurable levels
  • Email processing statistics and error tracking

🀝 Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ†˜ Support

For support and questions:

  • Create an issue in the GitHub repository
  • Check the API documentation at /docs
  • Review the health check endpoint at /health

🎯 Roadmap

  • WebSocket support for real-time updates
  • Email template management
  • Advanced retry logic with exponential backoff
  • Metrics export (Prometheus format)
  • Multi-tenant support
  • Email delivery tracking

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages