Email Notification System
Overview
The worker can email users when selected Celery tasks finish. SMTP is pluggable (SendGrid and others). Sending is non-blocking and respects tenant context.
Features
- Optional per-task via
@email_notification(avoids noisy mail when many parallel tasks run) - HTML and plain text bodies with links into the app when results include IDs
- Graceful no-op if SMTP is misconfigured
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
Opt-in: @email_notification
Only tasks decorated with @email_notification send mail (parallel subtasks usually stay silent; use a chord callback for one summary email).
Templates: EmailTemplate.TASK_COMPLETION (generic) or EmailTemplate.TEST_EXECUTION_SUMMARY (runs). Optional subject_template= uses the same context variables.
Without the decorator: no notification. Legacy: EmailEnabledTask still sends basic completion emails.
Async batch and summary email
Parallel test execution uses an async batch inside one Celery task; it does not use Celery chord. When the batch finishes, trigger_results_collection schedules collect_results with the result list so the same summary path (including @email_notification) runs as for sequential runs. See apps/backend/src/rhesis/backend/tasks/execution/shared.py and batch/__init__.py.
Current Task Configuration
collect_results—TEST_EXECUTION_SUMMARYemail_notification_test—TASK_COMPLETION- Per-test execution and utility tasks — no decorator (no mail)
Automatic Integration
On success or final failure, the worker loads the user from task context (skips placeholder addresses), renders the template with task return values and timing, sends via EmailService, and logs errors 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:
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
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).