# Email System Documentation This document provides comprehensive documentation for the Rustelo email system, including setup, configuration, usage, and examples. ## Table of Contents - [Overview](#overview) - [Features](#features) - [Quick Start](#quick-start) - [Configuration](#configuration) - [Email Providers](#email-providers) - [Templates](#templates) - [API Endpoints](#api-endpoints) - [Client Components](#client-components) - [Server Usage](#server-usage) - [Environment Variables](#environment-variables) - [Security Considerations](#security-considerations) - [Troubleshooting](#troubleshooting) - [Examples](#examples) ## Overview The Rustelo email system provides a comprehensive solution for sending emails from your web application. It supports multiple email providers, template-based emails, form submissions, and both server-side and client-side integration. ### Architecture - **Email Service**: Core service that handles email sending - **Providers**: Pluggable email providers (SMTP, SendGrid, Console) - **Templates**: Handlebars-based email templates - **Forms**: Ready-to-use contact and support form components - **API**: REST endpoints for email operations ## Features ✨ **Multiple Providers** - SMTP (Gmail, Outlook, custom servers) - SendGrid API - Console output (development) 📧 **Template System** - Handlebars templates - HTML and text versions - Custom helpers - Variable substitution 🔧 **Form Integration** - Contact forms - Support forms with priorities - Custom form handling 🛡️ **Security** - Input validation - Rate limiting - CSRF protection - Secure configuration 🎨 **Rich Components** - React/Leptos form components - Real-time validation - Error handling - Success feedback ## Quick Start ### 1. Enable Email Feature Make sure the email feature is enabled in your `Cargo.toml`: ```toml [features] default = ["email"] email = ["lettre", "handlebars", "urlencoding"] ``` ### 2. Basic Configuration Add email configuration to your `config.toml`: ```toml [email] enabled = true provider = "console" # Start with console for development from_email = "noreply@yourapp.com" from_name = "Your App" template_dir = "templates/email" ``` ### 3. Create Template Directory ```bash mkdir -p templates/email/html mkdir -p templates/email/text ``` ### 4. Start Using ```rust // Send a simple email let result = email_service.send_simple_email( "user@example.com", "Welcome!", "Thank you for signing up!" ).await?; // Send a contact form let result = email_service.send_contact_form( "John Doe", "john@example.com", "Question about pricing", "I'd like to know more about your pricing plans.", "admin@yourapp.com" ).await?; ``` ## Configuration ### Email Configuration Options ```toml [email] # Basic settings enabled = true provider = "smtp" # "smtp", "sendgrid", "console" from_email = "noreply@yourapp.com" from_name = "Your App Name" template_dir = "templates/email" # SMTP settings (when provider = "smtp") smtp_host = "smtp.gmail.com" smtp_port = 587 smtp_username = "your-email@gmail.com" smtp_password = "your-app-password" smtp_use_tls = false smtp_use_starttls = true # SendGrid settings (when provider = "sendgrid") sendgrid_api_key = "your-sendgrid-api-key" sendgrid_endpoint = "https://api.sendgrid.com/v3/mail/send" ``` ### Environment-Specific Configuration ```toml # Development [environments.development] email.provider = "console" email.enabled = true # Production [environments.production] email.provider = "sendgrid" email.sendgrid_api_key = "${SENDGRID_API_KEY}" email.enabled = true ``` ## Email Providers ### Console Provider Perfect for development and testing. Prints emails to the console. ```toml [email] provider = "console" ``` **Features:** - No external dependencies - Immediate feedback - Safe for development ### SMTP Provider Use any SMTP server including Gmail, Outlook, or custom servers. ```toml [email] provider = "smtp" smtp_host = "smtp.gmail.com" smtp_port = 587 smtp_username = "your-email@gmail.com" smtp_password = "your-app-password" smtp_use_starttls = true ``` **Common SMTP Configurations:** **Gmail:** ```toml smtp_host = "smtp.gmail.com" smtp_port = 587 smtp_use_starttls = true # Requires App Password (not regular password) ``` **Outlook:** ```toml smtp_host = "smtp-mail.outlook.com" smtp_port = 587 smtp_use_starttls = true ``` **Custom Server:** ```toml smtp_host = "mail.yourdomain.com" smtp_port = 587 smtp_use_starttls = true ``` ### SendGrid Provider Professional email service with high deliverability. ```toml [email] provider = "sendgrid" sendgrid_api_key = "your-api-key" ``` **Setup Steps:** 1. Sign up at [SendGrid](https://sendgrid.com) 2. Create an API key 3. Verify your sender identity 4. Add API key to configuration ## Templates ### Template Structure Templates are organized in HTML and text directories: ``` templates/email/ ├── html/ │ ├── contact.hbs │ ├── notification.hbs │ └── welcome.hbs └── text/ ├── contact.hbs ├── notification.hbs └── welcome.hbs ``` ### Template Naming Templates are named using the pattern: `{template_name}_{format}.hbs` - `contact_html` - HTML version of contact template - `contact_text` - Text version of contact template ### Handlebars Helpers The system includes several built-in helpers: **Date Formatting:** ```handlebars {{date_format submitted_at "%B %d, %Y at %I:%M %p UTC"}} ``` **Capitalization:** ```handlebars {{capitalize form_type}} ``` **Truncation:** ```handlebars {{truncate user_agent 100}} ``` **Default Values:** ```handlebars {{default action_text "Take Action"}} ``` **URL Encoding:** ```handlebars {{url_encode email}} ``` ### Example Template **HTML Template (contact.hbs):** ```html
Name: {{name}}
Email: {{email}}
Subject: {{subject}}
{{message}}
Submitted on {{date_format submitted_at}}
``` **Text Template (contact.hbs):** ```text Contact Form Submission Name: {{name}} Email: {{email}} Subject: {{subject}} Message: {{message}} --- Submitted on {{date_format submitted_at}} ``` ## API Endpoints ### GET /api/email/status Get email service status and available templates. **Response:** ```json { "enabled": true, "provider": "smtp", "configured": true, "templates": ["contact_html", "contact_text", "notification_html"] } ``` ### POST /api/email/contact Submit a contact form. **Request:** ```json { "name": "John Doe", "email": "john@example.com", "subject": "Question about pricing", "message": "I'd like to know more about your plans.", "recipient": "admin@yourapp.com" } ``` **Response:** ```json { "message": "Contact form submitted successfully", "message_id": "smtp-1234567890", "status": "sent" } ``` ### POST /api/email/support Submit a support form with priority and category. **Request:** ```json { "name": "Jane Smith", "email": "jane@example.com", "subject": "Login issues", "message": "I can't log into my account.", "priority": "high", "category": "technical", "recipient": "support@yourapp.com" } ``` ### POST /api/email/send Send a custom email (admin only). **Request:** ```json { "to": "user@example.com", "subject": "Welcome to our service", "template": "welcome", "template_data": { "user_name": "John", "activation_link": "https://app.com/activate/123" } } ``` ### POST /api/email/notification Send a notification email. **Request:** ```json { "to": "user@example.com", "title": "Account Verification", "message": "Please verify your email address", "content": "Click the link below to verify...
" } ``` ## Client Components ### ContactForm Component ```jsx import { ContactForm } from './components/forms';"We'd love to hear from you. Send us a message and we'll respond as soon as possible."