Email Notification System
Overview
The Rhesis backend includes an automated email notification system that sends notifications to users when their background tasks complete. This system integrates with the Celery task system and can use any SMTP provider, including SendGrid.
Features
- 📧 Automatic notifications: Users receive emails when their tasks complete successfully or fail
- 🎨 Rich HTML emails: Professional-looking emails with both HTML and plain text versions
- 🔗 Direct links: Clickable links to view results in the frontend application
- 🏢 Multi-tenant aware: Respects organization and user context
- 🛡️ Error handling: Graceful degradation if email service is unavailable
- ⚡ Performance: Email sending doesn’t block task execution
Configuration
Environment Variables
The following environment variables must be configured in the worker deployment:
SendGrid Setup
For SendGrid specifically:
- Create a SendGrid account and verify your sender domain
- Generate an API key with “Mail Send” permissions
- Use these settings:
SMTP_HOST:smtp.sendgrid.netSMTP_PORT:587SMTP_USER:apikeySMTP_PASSWORD: Your SendGrid API key
Other SMTP Providers
The system works with any SMTP provider. Common configurations:
How It Works
Selective Email Notifications
Important: Email notifications are now opt-in using the @email_notification decorator to prevent spam when running parallel tasks. Only tasks that explicitly use the decorator will send email notifications.
Email Notification Decorator
Using the @email_notification Decorator
The new decorator approach provides fine-grained control over email notifications:
Available Email Templates
Custom Subject Templates
You can customize the email subject using template variables:
Task Types
1. Tasks with Email Notifications - Using Decorator
Use the decorator for tasks that users directly submit and want to be notified about:
2. Silent Tasks - No Decorator
Tasks without the decorator will not send email notifications:
3. Legacy EmailEnabledTask - Still Supported
The old EmailEnabledTask is still supported for backward compatibility:
Chord Example
When running tasks in parallel with chords, use the decorator appropriately:
Current Task Configuration
- ✅
collect_results- Uses@email_notification(template=EmailTemplate.TEST_EXECUTION_SUMMARY)(sends detailed test summaries) - ✅
email_notification_test- Uses@email_notification(template=EmailTemplate.TASK_COMPLETION)(sends basic completion emails) - ✅ Individual test execution tasks - No decorator (no emails)
- ✅ Utility tasks - No decorator (no emails)
Automatic Integration
When a task with the @email_notification decorator completes (either successfully or fails permanently), the system:
- Retrieves user information from the task context
- Skips placeholder emails (internal system users)
- Calculates execution time if available
- Uses the specified template to generate email content
- Applies template variables from task results and context
- Sends HTML email using the centralized email service
- Logs the outcome without failing the task
Template Variables
The decorator automatically provides these variables to templates:
recipient_name: User’s display nametask_name: Human-readable task nametask_id: Unique task identifierstatus: Task completion status (‘success’ or ‘failed’)execution_time: Formatted execution durationerror_message: Error details (for failed tasks)frontend_url: Base URL for linkscompleted_at: Completion timestamp
Additional variables can be provided by returning them from the task:
Task Integration
Email Service Architecture
EmailService Class
The core EmailService class in tasks/email_service.py handles:
BaseTask Integration
The BaseTask class includes:
_get_user_info(): Retrieves user email and name from database_send_task_completion_email(): Handles email sending with error handlingon_success(): Sends success notificationson_failure(): Sends failure notifications (only for permanent failures)
User Experience
What Users Receive
When users submit tasks through the API (e.g., test configurations), they automatically receive:
- Immediate API response with task ID
- Email notification when the task completes with:
- Clear status indication
- Task details and timing
- Direct link to results (if applicable)
- Professional branding
Email Examples
Success Email
Testing
Test Endpoint
Use the test endpoint to verify email functionality:
Manual Testing
Troubleshooting
Common Issues
No Emails Being Sent
- Check SMTP configuration:
- Verify environment variables:
- Check user emails:
- Ensure users have valid email addresses
- System skips placeholder emails (
*@placeholder.rhesis.ai)
SMTP Authentication Errors
-
Verify credentials:
- Double-check SMTP username and password
- For SendGrid, ensure you’re using
apikeyas username
-
Check firewall/network:
- Ensure port 587 is accessible from worker pods
- Some networks block SMTP ports
Email Content Issues
-
Missing links:
- Verify
FRONTEND_URLis set correctly - Check that
test_run_idis included in task results
- Verify
-
Formatting problems:
- Check logs for email generation errors
- Verify HTML content in email client
Monitoring
Monitor email notifications through:
- Worker logs:
- Success indicators:
- Error indicators:
Security Considerations
Email Content
- No sensitive data: Task results are not included in emails
- Secure links: Frontend URLs use HTTPS
- User privacy: Only the task owner receives notifications
SMTP Security
- TLS encryption: All SMTP connections use STARTTLS
- Credential protection: SMTP passwords stored as Kubernetes secrets
- Network security: SMTP traffic encrypted in transit
Access Control
- User context: Emails only sent to the user who submitted the task
- Organization isolation: Multi-tenant architecture prevents cross-organization emails
Future Enhancements
Potential improvements to consider:
- Email preferences: Allow users to opt-out of notifications
- Notification types: Different notifications for different task types
- Email templates: Customizable email templates per organization
- Delivery status: Track email delivery and bounce handling
- Digest emails: Summary emails for multiple completed tasks
- Mobile optimization: Enhanced mobile email experience
Deployment
Worker Deployment
Ensure your worker deployment includes the SMTP environment variables:
CI/CD Pipeline
The GitHub Actions workflow automatically includes SMTP secrets:
Remember to set these secrets in your GitHub repository settings for each environment (dev, stg, prd).