47 - SMTP Configuration

Overview

OpenAlgo uses SMTP for sending email notifications, password reset links, and alerts. SMTP credentials are stored encrypted in the database for security.

Architecture Diagram

┌──────────────────────────────────────────────────────────────────────────────┐
│                         SMTP Configuration Architecture                       │
└──────────────────────────────────────────────────────────────────────────────┘

┌─────────────────────────────────────────────────────────────────────────────┐
│                           Admin Configuration                                │
│                           /settings/smtp                                     │
│                                                                              │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │  SMTP Settings Form                                                  │   │
│  │                                                                      │   │
│  │  SMTP Server:    [smtp.gmail.com          ]                         │   │
│  │  Port:           [587                      ]                         │   │
│  │  Username:       [[email protected]           ]                         │   │
│  │  Password:       [••••••••••••             ]                         │   │
│  │  From Email:     [[email protected]      ]                         │   │
│  │  Use TLS:        [✓] Enabled                                        │   │
│  │                                                                      │   │
│  │  [Test Connection]  [Save Settings]                                 │   │
│  └─────────────────────────────────────────────────────────────────────┘   │
│                                    │                                         │
│                                    ▼                                         │
└─────────────────────────────────────────────────────────────────────────────┘

                                     │ Save with Encryption


┌─────────────────────────────────────────────────────────────────────────────┐
│                           Database Storage                                   │
│                                                                              │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │  smtp_config table                                                   │   │
│  │                                                                      │   │
│  │  id │ smtp_server │ smtp_port │ username │ password_enc │ ...       │   │
│  │  ─────────────────────────────────────────────────────────────────  │   │
│  │  1  │ smtp.gmail  │ 587       │ user@... │ gAAAAB...    │           │   │
│  │                                           (Fernet encrypted)         │   │
│  └─────────────────────────────────────────────────────────────────────┘   │
└─────────────────────────────────────────────────────────────────────────────┘

                                     │ When Email Needed


┌─────────────────────────────────────────────────────────────────────────────┐
│                         Email Sending Service                                │
│                                                                              │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │  1. Load SMTP config from database                                   │   │
│  │  2. Decrypt password using Fernet                                    │   │
│  │  3. Connect to SMTP server                                           │   │
│  │  4. Send email with TLS                                              │   │
│  │  5. Log result                                                       │   │
│  └─────────────────────────────────────────────────────────────────────┘   │
│                                    │                                         │
│                                    ▼                                         │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │                    SMTP Server                                       │   │
│  │                    (Gmail, SendGrid, etc.)                           │   │
│  └─────────────────────────────────────────────────────────────────────┘   │
│                                    │                                         │
│                                    ▼                                         │
│                           Email Delivered                                    │
│                           to Recipient                                       │
└─────────────────────────────────────────────────────────────────────────────┘

Database Schema

smtp_config Table

Password Encryption

Fernet Encryption

Key Derivation

Email Service

Configuration Loading

Send Email Function

Test Connection

API Endpoints

Save Configuration

Response:

Test Configuration

Response:

Get Configuration (Masked)

Response:

Common SMTP Providers

Gmail

SendGrid

Amazon SES

Email Templates

Password Reset Email

Order Notification Email

Security Considerations

Password Storage

Best Practices

Practice
Implementation

Use App Passwords

Gmail requires app-specific passwords

Enable TLS

Always use STARTTLS on port 587

Rate Limiting

Limit emails per minute

Error Masking

Don't expose SMTP errors to users

Audit Logging

Log all email attempts (without content)

Key Files Reference

File
Purpose

database/smtp_db.py

SMTP configuration model

services/email_service.py

Email sending logic

utils/encryption_utils.py

Fernet encryption helpers

blueprints/settings.py

SMTP configuration routes

frontend/src/pages/SmtpSettings.tsx

Configuration UI

Last updated