This is a scalable email queue implementation in Go, utilizing Redis for queue management and providing a flexible email sending system with retry mechanisms and template support.
- Redis-backed Queue: Leverages Redis for reliable email task queuing
- Retry Mechanism: Automatic retries for failed email sends
- Template Engine: Supports dynamic email templating
- Configurable: Highly configurable through environment variables
- SMTP Email Sending: Supports configurable SMTP email sending
- Bulk Email Sending: Support for sending multiple emails in a single request
- Endpoint:
GET /health - Description: Checks the health of the application
- Response:
{ "status": "ok", "timestamp": { "server": { "time": "2024-03-27T10:15:30Z", "timezone": "UTC" } } }
- Endpoint:
POST /api/send - Description: Enqueue a single email to be sent
- Request Body:
{ "to": "[email protected]", "subject": "Mail regarding license update", "templateName": "license_update", "data": { "username": "License creator" } } - Successful Response:
{ "message": "email was successfully added to the queue", "details": { "recipient": "[email protected]", "subject": "Mail regarding license update" } } - Error Responses:
400 Bad Request: Validation errors500 Internal Server Error: Queueing failure
-
Endpoint:
POST /api/bulk-send -
Description: Enqueue multiple emails in a single request
-
Request Body:
{ "emails": [ { "to": "[email protected]", "subject": "Welcome User 1", "templateName": "license_update", "data": { "username": "License creator" } }, { "to": "[email protected]", "subject": "Welcome User 2", "templateName": "license_update", "data": { "username": "License creater" } } ] } -
Request Validation:
- Minimum 1 email
- Maximum 50 emails per request
-
Successful Response (All emails queued):
{ "message": "all emails successfully queued", "successCount": 2, "successEmails": [ "[email protected]", "[email protected]" ] } -
Partial Success Response:
{ "message": "partial success in queueing emails", "successCount": 1, "failedCount": 1, "successEmails": ["[email protected]"], "failedEmails": ["[email protected]"] }
| Variable | Description | Default |
|---|---|---|
SERVER_PORT |
HTTP server port | 8080 |
CACHE_HOST |
Redis host | localhost |
CACHE_PORT |
Redis port | 6379 |
CACHE_PASSWORD |
Redis password | "" |
CACHE_DB_INDEX |
Redis database index | 0 |
EMAIL_SMTP_SERVER |
SMTP server address | smtp.gmail.com |
EMAIL_SMTP_PORT |
SMTP server port | 587 |
EMAIL_SMTP_USERNAME |
SMTP username | [email protected] |
EMAIL_SMTP_PASSWORD |
SMTP password | - |
EMAIL_SENDER_ADDRESS |
Sender email address | [email protected] |
EMAIL_SENDER_NAME |
Sender display name | Rohan |
- Create an
EmailTaskwith recipient, subject, template, and data - Enqueue the email task to Redis
- Background worker picks up the task
- Attempts to send email with configurable retries
- Logs success or failure
- Maximum retries: 3
- Retry delay: 5 seconds between attempts
- Queue check interval: 1 second
# Clone the repository
git clone https://github.com/rohansen856/redis-go-mailing-bulk.git
# Set required environment variables
# Run the application
go run ./cmd/server/main.go- Go 1.20+
- Redis
- gin-gonic/gin
- go-redis/redis
- html/template standard library
- Uses connection pooling for Redis
- Non-blocking queue processing
- Configurable pool sizes and timeouts
- Structured logging for performance tracking
- URL and HTML escaping in templates
- Configurable SMTP authentication
- Environment-based configuration management
- Input validation for email tasks
Rohan