Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Email (SMTP) Service

Send emails from your handlers using SMTP.

Configuration

Basic SMTP with STARTTLS

{
  "service_type": "email",
  "config": {
    "host": "smtp.example.com",
    "port": 587,
    "username": "sender@example.com",
    "password": "app-password",
    "encryption": "starttls",
    "from_address": "noreply@example.com",
    "from_name": "My App",
    "reply_to": "support@example.com"
  }
}

Gmail SMTP

{
  "service_type": "email",
  "config": {
    "host": "smtp.gmail.com",
    "port": 587,
    "username": "your-email@gmail.com",
    "password": "your-app-password",
    "encryption": "starttls",
    "from_address": "your-email@gmail.com",
    "from_name": "Your Name"
  }
}

Implicit TLS (Port 465)

{
  "service_type": "email",
  "config": {
    "host": "smtp.example.com",
    "port": 465,
    "username": "sender@example.com",
    "password": "secret",
    "encryption": "tls",
    "from_address": "noreply@example.com"
  }
}

Local SMTP (No Auth)

{
  "service_type": "email",
  "config": {
    "host": "localhost",
    "port": 25,
    "encryption": "none",
    "from_address": "app@localhost"
  }
}

Configuration Options

FieldTypeDefaultDescription
hoststringrequiredSMTP server hostname
portu16587SMTP port
usernamestringnullAuth username (usually email address)
passwordstringnullAuth password or app password
encryptionstring"starttls"Encryption: "none", "starttls", or "tls"
from_addressstringrequiredDefault sender email
from_namestringnullDefault sender display name
reply_tostringnullDefault reply-to address
timeout_secondsu3230Connection timeout
max_retriesu323Send retry attempts

Usage

Send a Simple Email

#![allow(unused)]
fn main() {
use rust_edge_gateway_sdk::prelude::*;

fn handle(req: Request) -> Response {
    let email = EmailPool { pool_id: "notifications".to_string() };
    
    let result = email.send(
        "user@example.com",
        "Welcome!",
        "Thanks for signing up.",
    );
    
    match result {
        Ok(()) => Response::ok(json!({"sent": true})),
        Err(e) => Response::internal_error(e.to_string()),
    }
}
}

Send HTML Email

#![allow(unused)]
fn main() {
fn handle(req: Request) -> Response {
    let email = EmailPool { pool_id: "notifications".to_string() };
    
    let html = r#"
        <h1>Welcome!</h1>
        <p>Thanks for joining us.</p>
        <a href="https://example.com/verify">Verify your email</a>
    "#;
    
    let result = email.send_html(
        "user@example.com",
        "Welcome to Our App",
        html,
    );
    
    match result {
        Ok(()) => Response::ok(json!({"sent": true})),
        Err(e) => Response::internal_error(e.to_string()),
    }
}
}

Send with Custom From

#![allow(unused)]
fn main() {
let result = email.send_from(
    "support@example.com",  // From
    "Support Team",         // From name
    "user@example.com",     // To
    "Your Ticket #123",     // Subject
    "We've received your support request...",
);
}

Common Providers

ProviderHostPortEncryption
Gmailsmtp.gmail.com587starttls
Outlooksmtp.office365.com587starttls
SendGridsmtp.sendgrid.net587starttls
Mailgunsmtp.mailgun.org587starttls
Amazon SESemail-smtp.{region}.amazonaws.com587starttls

Best Practices

  1. Use app passwords - Don't use your main account password
  2. Set up SPF/DKIM - Improve deliverability
  3. Handle failures - Emails can fail; log and retry
  4. Rate limit - Don't spam; respect provider limits
  5. Use templates - Consistent formatting
  6. Test with sandbox - Use test mode before production