prod-wag-backend-automate-s.../BankServices/EmailService/README.md

85 lines
2.6 KiB
Markdown

# Email Service
## Overview
The Email Service is the first component in a Redis pub/sub processing chain for bank-related email automation. It monitors a specified mailbox for emails with attachments, filters them based on IBAN criteria, and publishes the data to a Redis channel for further processing.
## Features
### Email Processing
- Connects to a configured mailbox using IMAP
- Implements smart date-based filtering:
- Checks emails from the past 14 days on the first run of each day
- Checks emails from the past 7 days on subsequent runs within the same day
- Extracts attachments from emails
- Filters attachments based on IBAN criteria
- Uses a context manager to ensure emails are properly handled even during errors
### Redis Integration
- Publishes messages to a Redis pub/sub channel ("CollectedData")
- Each message contains:
- Unique UUID
- Timestamp
- Initial stage marker ("red")
- Attachment payload and metadata
- Connects to an external Redis server
### Error Handling
- Robust error management with context managers
- Automatic marking of emails as unread if processing fails
- Comprehensive logging
## Configuration
### Environment Variables
```
EMAIL_HOST=10.10.2.34
EMAIL_USERNAME=isbank@mehmetkaratay.com.tr
EMAIL_PASSWORD=system
EMAIL_SLEEP=60
AUTHORIZE_IBAN=4245-0093333
REDIS_HOST=10.10.2.15
REDIS_PORT=6379
REDIS_PASSWORD=your_strong_password_here
```
## Deployment
### Docker
The service is containerized using Docker and can be deployed using the provided Dockerfile and docker-compose configuration.
```bash
# Build and start the service
docker compose -f bank-services-docker-compose.yml up -d --build
# View logs
docker compose -f bank-services-docker-compose.yml logs -f email_service
# Stop the service
docker compose -f bank-services-docker-compose.yml down
```
### Service Management
The `check_bank_services.sh` script provides a simple way to restart the service:
```bash
./check_bank_services.sh
```
## Architecture
### Redis Pub/Sub Chain
This service is the first in a multi-stage processing chain:
1. **Email Service** (this service): Reads emails, extracts attachments, publishes to Redis with stage="red"
2. **Processor Service**: Subscribes to stage="red" messages, processes data, republishes with stage="processed"
3. **Writer Service**: Subscribes to stage="processed" messages, writes data to final destination, marks as stage="completed"
## Development
### Dependencies
- Python 3.12
- Redbox (email library)
- Redis
### State Management
The service maintains a state file at `/tmp/email_service_last_run.json` to track when it last ran, enabling the smart date-based filtering feature.