# ๐Ÿ—๏ธ Outreach Engine - Technical Architecture ## ๐Ÿ“‹ Project Overview The Outreach Engine is a Node.js-based email automation system designed for professional outreach campaigns to law firms. It features a modular architecture with comprehensive testing, tracking, and safety mechanisms. ## ๐Ÿ›๏ธ System Architecture ### Core Components ``` โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Main App โ”‚ โ”‚ Template โ”‚ โ”‚ Database โ”‚ โ”‚ (index.js) โ”‚โ—„โ”€โ”€โ–บโ”‚ Engine โ”‚โ—„โ”€โ”€โ–บโ”‚ (SQLite) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ”‚ โ–ผ โ–ผ โ–ผ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Rate Limiter โ”‚ โ”‚ Attachment โ”‚ โ”‚ Tracking โ”‚ โ”‚ & Error โ”‚ โ”‚ Handler โ”‚ โ”‚ Server โ”‚ โ”‚ Handler โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ ``` ### Data Flow 1. **Initialization**: Load config โ†’ Initialize DB โ†’ Load firm data 2. **Email Processing**: Template rendering โ†’ Rate limiting โ†’ SMTP sending 3. **Tracking**: Open/click events โ†’ Database logging โ†’ Analytics 4. **Error Handling**: Retry logic โ†’ Fallback mechanisms โ†’ Logging ## ๐Ÿ“ Folder Structure ``` outreach-engine/ โ”œโ”€โ”€ ๐Ÿ“„ index.js # Main application entry point โ”œโ”€โ”€ ๐Ÿ“ config/ โ”‚ โ””โ”€โ”€ ๐Ÿ“„ index.js # Environment configuration management โ”œโ”€โ”€ ๐Ÿ“ lib/ # Core libraries โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ attachmentHandler.js # File attachment processing โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ database.js # SQLite database operations โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ errorHandler.js # Error handling and retry logic โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ logger.js # Winston logging configuration โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ rateLimiter.js # Email rate limiting โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ templateEngine.js # Handlebars template processing โ”‚ โ””โ”€โ”€ ๐Ÿ“„ trackingServer.js # HTTP server for tracking โ”œโ”€โ”€ ๐Ÿ“ templates/ # Email templates (Handlebars) โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ outreach.html # Main outreach template โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ campaign-1-saas.html # SaaS campaign โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ campaign-2-data-service.html โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ campaign-3-license.html โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ campaign-4-gui.html โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ general-intro.html โ”‚ โ””โ”€โ”€ ๐Ÿ“„ employment-layer.html โ”œโ”€โ”€ ๐Ÿ“ tests/ # Test suite โ”‚ โ”œโ”€โ”€ ๐Ÿ“ lib/ โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ errorHandler.test.js โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ rateLimiter.test.js โ”‚ โ”‚ โ””โ”€โ”€ ๐Ÿ“„ templateEngine.test.js โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ setup.js # Test configuration โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ jest.config.js # Jest configuration โ”‚ โ””โ”€โ”€ ๐Ÿ“„ test-campaigns.json # Test campaign data โ”œโ”€โ”€ ๐Ÿ“ scripts/ # Utility scripts โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ migrate-to-database.js # JSON to database migration โ”‚ โ””โ”€โ”€ ๐Ÿ“„ run-campaigns.js # Campaign testing script โ”œโ”€โ”€ ๐Ÿ“ logs/ # Application logs โ”œโ”€โ”€ ๐Ÿ“ data/ # Generated data files โ”œโ”€โ”€ ๐Ÿ“ coverage/ # Test coverage reports โ”œโ”€โ”€ ๐Ÿ“„ firm.json # Law firm contact data โ”œโ”€โ”€ ๐Ÿ“„ tests/test-campaigns.json # Test campaign data โ”œโ”€โ”€ ๐Ÿ“„ package.json # Dependencies and scripts โ””โ”€โ”€ ๐Ÿ“„ tests/jest.config.js # Jest testing configuration ``` ## ๐Ÿ”ง Dependencies ### Production Dependencies | Package | Version | Purpose | | ------------ | ------- | ---------------------------- | | `nodemailer` | ^7.0.5 | Email sending via SMTP | | `handlebars` | ^4.7.8 | Template engine for emails | | `sqlite` | ^5.1.1 | SQLite database driver | | `sqlite3` | ^5.1.7 | SQLite3 bindings | | `winston` | ^3.17.0 | Structured logging | | `dotenv` | ^17.2.0 | Environment variable loading | | `delay` | ^6.0.0 | Promise-based delays | ### Development Dependencies | Package | Version | Purpose | | --------------- | ------- | --------------------- | | `jest` | ^30.0.4 | Testing framework | | `@jest/globals` | ^30.0.4 | Jest global functions | ## ๐Ÿ—„๏ธ Database Schema ### Tables #### `firms` ```sql CREATE TABLE firms ( id INTEGER PRIMARY KEY AUTOINCREMENT, firm_name TEXT NOT NULL, location TEXT, website TEXT, contact_email TEXT UNIQUE, state TEXT, created_at DATETIME DEFAULT CURRENT_TIMESTAMP ); ``` #### `email_sends` ```sql CREATE TABLE email_sends ( id INTEGER PRIMARY KEY AUTOINCREMENT, campaign_id TEXT, firm_id INTEGER, recipient_email TEXT, subject TEXT, status TEXT, tracking_id TEXT, sent_at DATETIME DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (firm_id) REFERENCES firms (id) ); ``` #### `tracking_events` ```sql CREATE TABLE tracking_events ( id INTEGER PRIMARY KEY AUTOINCREMENT, tracking_id TEXT, event_type TEXT, -- 'open' or 'click' ip_address TEXT, user_agent TEXT, url TEXT, created_at DATETIME DEFAULT CURRENT_TIMESTAMP ); ``` ## ๐Ÿ”„ Core Workflows ### 1. Application Startup ```mermaid graph TD A[Start] --> B[Load Environment] B --> C[Initialize Database] C --> D[Load Firm Data] D --> E[Start Tracking Server] E --> F[Begin Email Campaign] ``` ### 2. Email Sending Process ```mermaid graph TD A[Get Next Firm] --> B[Rate Limit Check] B --> C[Render Template] C --> D[Add Tracking] D --> E[Send via SMTP] E --> F[Log Success/Failure] F --> G[Wait Delay] G --> A ``` ### 3. Template Processing ```mermaid graph TD A[Load Template] --> B[Compile Handlebars] B --> C[Inject Firm Data] C --> D[Add GIF if enabled] D --> E[Generate Text Version] E --> F[Add Tracking Pixels] F --> G[Return Email Content] ``` ## ๐Ÿงช Testing Architecture ### Test Structure - **Unit Tests**: `tests/lib/*.test.js` - Test individual modules - **Integration Tests**: Built into main application - **Coverage**: Jest generates coverage reports in `coverage/` ### Test Categories 1. **Error Handler Tests**: Retry logic, error classification 2. **Rate Limiter Tests**: Delay calculations, pause logic 3. **Template Engine Tests**: Template rendering, data injection ### Test Commands ```bash npm test # Run all tests npm run test:watch # Watch mode npm run test:coverage # Generate coverage report ``` ## โš™๏ธ Configuration System ### Environment Loading - **Auto-detection**: Based on `NODE_ENV` - **Files**: `.env.development`, `.env.production` - **Fallback**: Default values for missing variables ### Configuration Categories 1. **Email Settings**: SMTP, credentials, test mode 2. **Rate Limiting**: Delays, limits, pause intervals 3. **Tracking**: Server port, domain, enable/disable 4. **Logging**: Levels, debug mode, file rotation 5. **Templates**: GIF settings, attachment handling ## ๐Ÿ”’ Security & Safety ### Test Mode Protection - **Fail-safe validation**: Stops if test recipient not set - **Double verification**: Checks test mode twice before sending - **Clear logging**: Every email shows test status - **No production data**: Test mode never uses real firm emails ### Rate Limiting - **Configurable delays**: 5+ minutes between emails - **Hourly limits**: 15 emails/hour default - **Automatic pauses**: Every 50 emails, pause 30 minutes - **Randomization**: Adds jitter to prevent detection ## ๐Ÿ“Š Monitoring & Analytics ### Logging System - **Winston**: Structured logging with multiple transports - **File rotation**: Automatic log file management - **Debug mode**: Detailed SMTP and template information - **Error tracking**: Comprehensive error logging ### Tracking Analytics - **Open tracking**: Invisible pixel tracking - **Click tracking**: URL wrapping with redirects - **Database storage**: All events stored in SQLite - **Real-time server**: HTTP server on port 3000 ## ๐Ÿš€ Performance Considerations ### Optimization Features - **Database indexing**: Fast queries on email and tracking - **Template caching**: Handlebars templates compiled once - **Batch processing**: Configurable batch sizes - **Memory management**: Proper cleanup of resources ### Scalability - **Modular design**: Easy to extend with new features - **Database abstraction**: Can switch to other databases - **Template system**: Easy to add new email templates - **Configuration**: Environment-based settings ## ๐Ÿ”ง Development Workflow ### Code Organization - **Separation of concerns**: Each module has single responsibility - **Error boundaries**: Comprehensive error handling - **Async/await**: Modern JavaScript patterns - **ES6 modules**: Clean import/export structure ### Testing Strategy - **Unit tests**: Test individual functions - **Integration tests**: Test module interactions - **End-to-end**: Test complete email sending flow - **Coverage goals**: Maintain high test coverage ## ๐Ÿ“ˆ Future Enhancements ### Planned Features - **A/B testing**: Template variant testing - **Advanced analytics**: Dashboard for campaign metrics - **API endpoints**: REST API for external integration - **Web interface**: Admin dashboard for campaign management ### Technical Debt - **TypeScript migration**: Add type safety - **Dependency updates**: Keep packages current - **Performance optimization**: Database query optimization - **Security hardening**: Additional security measures ## ๐Ÿ› Debugging Guide ### Common Issues 1. **SMTP errors**: Check credentials and 2FA settings 2. **Template errors**: Verify Handlebars syntax 3. **Database errors**: Check SQLite file permissions 4. **Rate limiting**: Adjust delay settings ### Debug Tools - **Debug mode**: `DEBUG=true npm start` - **Log files**: Check `logs/` directory - **Database inspection**: Use SQLite browser - **Network monitoring**: Check tracking server logs ## ๐Ÿ“š API Reference ### Main Functions - `sendEmails()`: Main email sending loop - `sendSingleEmail()`: Individual email sending - `initializeData()`: Database and data setup - `processFirm()`: Single firm processing ### Library Functions - `templateEngine.render()`: Template rendering - `rateLimiter.check()`: Rate limit validation - `errorHandler.handle()`: Error processing - `database.logEmailSend()`: Email logging --- This document provides comprehensive technical information for development, debugging, and AI assistance. For user-focused information, see `README.md`.