Email Service
The Email Service in Gradiant is designed to handle all transactional email communication with high reliability and security. It uses Postmark as the email delivery provider and implements a robust queuing system with Redis for reliable delivery.Features
- Reliable email delivery with queuing and retries
- Template-based emails
- HIPAA-compliant email handling
- Comprehensive error handling and logging
- Queue monitoring and statistics
- Type-safe API with Zod validation
Usage
Basic Usage
Creating Templates
Monitoring Queue Status
Configuration
The email service requires the following environment variables:Queue System
The email service uses Redis for queue management with two queues:email:queue- Main queue for pending emailsemail:processing- Queue for emails being processed
Retry Strategy
Failed emails are automatically retried with exponential backoff:- 1st retry: 1 minute
- 2nd retry: 5 minutes
- 3rd retry: 15 minutes
Types
EmailData
EmailTemplate
Error Handling
The service implements comprehensive error handling:-
Input Validation
- All inputs are validated using Zod schemas
- Invalid data throws descriptive errors
-
Queue Processing
- Failed sends are automatically retried
- Permanent failures are logged
- Queue processing errors don’t crash the service
-
Monitoring
- Queue statistics are available in real-time
- Failed sends are logged with full context
- Processing status is tracked
Security
The email service is designed with security in mind:-
HIPAA Compliance
- All emails are sent via secure channels
- PII is handled according to regulations
- Audit trails are maintained
-
Data Validation
- All inputs are strictly validated
- Email addresses are verified
- Templates are sanitized
-
Error Protection
- Rate limiting is enforced
- Retry limits prevent abuse
- Error details are sanitized
Best Practices
-
Template Usage
- Use templates for all emails
- Include both HTML and text versions
- Test templates before deployment
-
Queue Management
- Monitor queue length regularly
- Set up alerts for queue buildup
- Regularly check failed emails
-
Error Handling
- Log all failures appropriately
- Monitor retry patterns
- Alert on high failure rates
Testing
The email service includes comprehensive tests:- Email queueing
- Template management
- Queue processing
- Error handling
- Retry logic
Monitoring
Monitor the email service using:-
Queue Statistics
-
Logs
- Info level: Successful operations
- Warn level: Retryable failures
- Error level: Permanent failures
-
Metrics
- Queue length
- Processing time
- Failure rates
- Retry counts
Troubleshooting
Common issues and solutions:-
Emails not sending
- Check queue statistics
- Verify Postmark credentials
- Check for rate limiting
-
High failure rates
- Review error logs
- Check template validity
- Verify email addresses
-
Queue buildup
- Check processing speed
- Verify Redis connection
- Monitor system resources