Skip to main content
This document describes the email system integration in DeployStack, including the email service, template system, and recommended usage patterns.

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
For all user-facing email operations, use the background job queue instead of direct email sending.
The background job approach provides significant benefits over 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
Reliability Benefits:
  • 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
Observability Benefits:
  • 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

For complete details on the job queue system, see the Background Job Queue Documentation.

Direct Email Sending (Advanced)

Direct email sending is only recommended for specific use cases requiring immediate feedback.

When to Use Direct Sending

Use EmailService.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:
  1. Add new Pug template in src/email/templates/
  2. Add TypeScript types in src/email/types.ts
  3. 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 status
  • refreshConfiguration() - Reload SMTP configuration
  • getAvailableTemplates() - Get list of available templates
  • validateTemplate() - 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
Check failed jobs in the database:

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):
After (Non-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

Summary

For all user-facing email operations, use background jobs. This provides instant response times, automatic retry, and better reliability. Reserve direct EmailService.sendEmail() calls for admin testing and debugging scenarios requiring immediate feedback.