This project implements an email service using Go. The service supports asynchronous email sending with attachments, retries for failed deliveries, and integration with RabbitMQ for task queuing.
- Asynchronous email processing: Ensures non-blocking API responses by processing emails in the background.
- Attachment support: Allows sending emails with file attachments encoded in base64 format.
- Retry mechanism: Automatically retries email delivery on failure, up to a configurable limit.
- RabbitMQ integration: Uses RabbitMQ for reliable task queuing and delivery.
Before running the project, ensure you have the following installed:
-
Clone the repository:
git clone https://github.com/your-username/email-service.git cd email-service
-
Create an
.env
file in the root directory with the following content:SMTP_HOST=smtp.example.com SMTP_PORT=587 SMTP_USER=[email protected] SMTP_PASSWORD=your-password RABBITMQ_URL=amqp://guest:guest@localhost:5672/ DATABASE_URL=your-database-connection-string DB_HOST=your-database-host # eg: db or localhost DB_PORT=your-database-port # eg: 5432 DB_USER=your-database-username # eg: postgres DB_NAME=your-database-name # eg: postgres DB_PASSWORD=your-database-password SECRET_KEY=your-secret-key
-
Build and start the services using Docker Compose:
docker-compose up --build
The following API endpoint is available:
Endpoint: POST /send-email
Request Body:
{
"from": "[email protected]",
"to": "[email protected]",
"subject": "Test Email",
"body": "This is a test email.",
"attachments": [
{
"filename": "test.txt",
"content_type": "text/plain",
"data": "base64-encoded-content"
}
],
"cc": ["[email protected]"],
"bcc": ["[email protected]"],
"reply_to": "[email protected]"
}
Response:
- Success: HTTP 202 Accepted
{ "message": "Email task successfully enqueued", "data": null, "status_code":202 }
- Failure: HTTP 400/500 with an error message
email-service/
├── cmd/ # App Entry Point
│ ├── api/ # API
│ ├── woker/ # Woker logic
├── internal/
│ ├── http/ # HTTP Handlers
│ ├── dto/ # Data Transfer Objects
│ ├── models/ # Database models
│ ├── services/ # Business logic
├── migrate/ # Migration function
├── utils/ # Utility functions
├── Dockerfile # Docker build configuration
├── docker-compose.yml # Docker Compose configuration
└── README.md # Project documentation
The worker process listens for tasks from RabbitMQ and processes email sending tasks. The worker is automatically started when you run the docker-compose up
command.
The project uses Docker for containerization. The docker-compose.yml
file defines two services:
api
: The main API serviceworker
: The background worker for processing email tasks
To start the application:
docker-compose up --build
To stop the application:
docker-compose down
Variable | Description |
---|---|
SMTP_HOST | SMTP server host |
SMTP_PORT | SMTP server port |
SMTP_USER | SMTP username |
SMTP_PASSWORD | SMTP password |
RABBITMQ_URL | RabbitMQ connection URL |
DATABASE_URL | Database connection string |
DB_NAME | Database Name |
DB_HOST | Database Host |
DB_USER | Database User |
DB_PORT | Database Port |
DB_PASSWORD | Database password |