A comprehensive RESTful API service for email processing with queue management, built with FastAPI and SQL Server.
- 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
- Python 3.11+
- SQL Server (with ODBC Driver 17)
- Redis (optional, for advanced features)
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
# Using Docker Compose (Recommended)
docker-compose up -d
# View logs
docker-compose logs -f email-service
# Stop services
docker-compose down
# Build and start services
docker-compose up -d
# View logs
docker-compose logs -f
# Stop services
docker-compose down
# Build image
docker build -t email-service .
# Run container
docker run -d -p 8000:8000 --name email-service-container email-service
Once the application is running, access the documentation at:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
- OpenAPI JSON: http://localhost:8000/openapi.json
- 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()
);
-
Authenticate: POST
/api/authenticate
{ "username": "admin", "password": "admin" }
-
Configure Database: POST
/api/save-config
{ "server": "localhost", "port": 1433, "user": "sa", "password": "YourPassword123", "database": "EmailDB" }
-
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" }
-
Start Email Service: POST
/api/service-control
{ "action": "start", "user": "admin" }
# 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"}'
Import the provided Postman collection for comprehensive API testing.
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
- 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
- Real-time dashboard with service statistics
- Health check endpoints
- Structured logging with configurable levels
- Email processing statistics and error tracking
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
For support and questions:
- Create an issue in the GitHub repository
- Check the API documentation at
/docs
- Review the health check endpoint at
/health
- 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