Overview
The email system provides a comprehensive solution for sending templated emails using:- Background Job Queue: Async email processing with automatic retry (Recommended)
- Nodemailer: For SMTP email delivery
- Pug Templates: For beautiful, maintainable email templates
- Global Settings Integration: Automatic SMTP configuration from global settings
- Template Caching: Performance optimization for template rendering
Recommended: Sending Emails via Background Jobs
For all user-facing email operations, use the background job queue instead of direct email sending.
Why Background Jobs?
Performance Benefits:- Instant Response: API endpoints return immediately without waiting for SMTP operations
- Non-Blocking: Email sending doesn’t delay critical user flows like registration or password reset
- Scalability: Can queue hundreds of emails without impacting application performance
- Automatic Retry: Failed emails automatically retry with exponential backoff (3 attempts by default)
- Error Isolation: Email failures don’t crash user-facing operations
- Rate Limiting: Built-in support for respecting SMTP server rate limits
- Job Tracking: All email operations tracked in the job queue database
- Comprehensive Logging: Full logging of email operations, successes, and failures
- Status Monitoring: Query job status and history for debugging
Background Job Usage
Real-World Example: Registration Flow
When to Use Background Jobs
Use background jobs for:- User registration/verification emails
- Password reset emails
- Welcome emails
- Notification emails
- Batch email operations
- Any email that doesn’t require immediate confirmation
Monitoring Background Jobs
Direct Email Sending (Advanced)
When to Use Direct Sending
UseEmailService.sendEmail() directly only for:
- Test Emails: Admin panel SMTP testing requiring immediate success/failure feedback
- Synchronous Validation: Flows where you must confirm email sent before proceeding
- Development/Debugging: Local testing and troubleshooting
Direct Usage Example
Performance Consideration
Direct email sending adds 2-5 seconds of latency to your endpoint. This is acceptable for admin testing but not acceptable for user-facing operations like registration.Architecture
SMTP Configuration
The email system automatically integrates with SMTP global settings. Configure these settings in the admin panel or global settings:Template System
Available Templates
Template Variables
All templates have access to these common variables:currentYear: Current year (automatically injected)appName: Application name (default: ‘DeployStack’)supportEmail: Support email address (if provided)
Custom Templates
To create custom templates:- Add new Pug template in
src/email/templates/ - Add TypeScript types in
src/email/types.ts - Use with background jobs or
EmailService.sendEmail()
Color Guidelines
When adding colors to email templates, follow the official DeployStack color system to maintain brand consistency. See the UI Design System page for color specifications.Advanced Email Options
Both background jobs and direct sending support advanced options:Utility Methods
Test SMTP Connection
Other Utilities
getSmtpStatus()- Check SMTP configuration statusrefreshConfiguration()- Reload SMTP configurationgetAvailableTemplates()- Get list of available templatesvalidateTemplate()- Validate template and variables
Error Handling
Background Job Errors
Background jobs automatically retry on failure:- Attempt 1: Immediate execution
- Attempt 2: After 1 second (if first fails)
- Attempt 3: After 2 seconds (if second fails)
- Final: Job marked as ‘failed’ after 3 attempts
Direct Sending Errors
Direct sending provides immediate error feedback:Security & Performance
Security Features
- Passwords encrypted in global settings
- Secure connections (TLS/SSL) supported
- Template security with escaped variable injection
- No sensitive data in job queue payload
Performance Features
- Template caching after first compilation
- Connection pooling (max 5 concurrent connections)
- Rate limiting (5 emails per 20 seconds)
- Background processing doesn’t block main thread
Migration Guide
Converting Existing Code to Background Jobs
Before (Blocking):Common Usage Patterns
User Registration
Queue verification email as background job immediately after user creation.Password Reset
Queue password reset email with secure token link.System Notifications
Queue notification emails for system events, deployments, or status changes.Batch Operations
Queue multiple emails with rate limiting to avoid SMTP throttling:API Reference
EmailService Methods
Job Queue Service Methods
Related Documentation
- Background Job Queue - Complete job queue system documentation
- Global Settings - SMTP and system configuration
- API Documentation - REST API patterns and standards
Summary
For all user-facing email operations, use background jobs. This provides instant response times, automatic retry, and better reliability. Reserve directEmailService.sendEmail() calls for admin testing and debugging scenarios requiring immediate feedback.
