backbone-email 3.1.1

Backbone Framework Email Module - SMTP support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
# Backbone Email

๐Ÿฆด **Production-ready SMTP email service library with template support**

Backbone Email is a focused email service library that provides reliable SMTP email functionality with advanced features like template sending, file attachments, and comprehensive error handling. Designed for production use with robust error handling and tracking capabilities.

## ๐Ÿ“‹ Table of Contents

- [Overview]#overview
- [Features]#features
- [Usage]#usage
- [SMTP Configuration]#smtp-configuration
- [Technical Details]#technical-details
- [Examples]#examples
- [Error Handling]#error-handling
- [Testing]#testing

## ๐ŸŽฏ Overview

Backbone Email provides a clean, efficient SMTP email service that handles all common email needs without the complexity of multiple provider abstractions. It's designed for production use with comprehensive error handling and template support.

### Key Design Principles

- **SMTP Focused**: Optimized for reliable SMTP email delivery
- **Template Ready**: Built-in template rendering with Handlebars
- **Production Ready**: Robust error handling with retry mechanisms
- **Async First**: Full async/await support with tokio
- **File Attachments**: Support for multiple file types and sizes

## ๐Ÿš€ Features

### ๐Ÿ“ง **Core Email Features**
- **SMTP Support**: Traditional email servers with TLS/SSL support
- **Template System**: Built-in Handlebars template rendering
- **File Attachments**: Multiple file attachment support with type detection
- **Rich Content**: HTML, text, and multipart messages
- **Recipient Management**: To, CC, and BCC support

### ๐Ÿ›ก๏ธ **Production-Ready**
- **Error Handling**: Comprehensive error types with context
- **Connection Testing**: Pre-flight SMTP connection validation
- **Retry Logic**: Configurable retry mechanisms for failed deliveries
- **Async Operations**: Non-blocking email sending and receiving

### ๐Ÿ“Š **Message Capabilities**
- **Multiple Recipients**: Send to multiple email addresses
- **Custom Headers**: Add custom SMTP headers
- **Priority Levels**: Set message priority (High, Normal, Low)
- **Reply-To**: Configure reply-to addresses
- **Bulk Sending**: Send multiple emails efficiently

## ๐Ÿ“– Usage

### Quick Start

```toml
[dependencies]
backbone-email = "2.0.0"
tokio = { version = "1.0", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
uuid = { version = "1.0", features = ["v4"] }
chrono = { version = "0.4", features = ["serde"] }
```

### Basic Email Sending

```rust
use backbone_email::{EmailService, SmtpProvider, EmailMessage};

// Create SMTP email service
let email_service = SmtpProvider::new("smtp.gmail.com", 587)
    .with_credentials("user@gmail.com", "password")
    .with_tls(true)
    .build()?;

// Create email message
let message = EmailMessage::builder()
    .from("sender@example.com")
    .to("recipient@example.com")
    .subject("Welcome to Backbone Email!")
    .text("This is a plain text email")
    .html("<h1>Welcome!</h1><p>This is HTML content.</p>")
    .build()?;

// Send email
let result = email_service.send(message).await?;
println!("Email sent with message ID: {}", result.message_id);
```

### Template Sending

```rust
use backbone_email::{EmailService, SmtpProvider};
use std::collections::HashMap;

// Create email service
let email_service = SmtpProvider::new("smtp.example.com", 587)
    .with_credentials("user", "password")
    .build()?;

// Template data
let mut template_data = HashMap::new();
template_data.insert("name".to_string(), "John Doe".into());
template_data.insert("company".to_string(), "ACME Corp".into());

// Send template email
let result = email_service.send_template(
    "welcome-template",
    vec!["user@example.com"],
    template_data
).await?;

println!("Template email sent: {}", result.message_id);
```

### Email with Attachments

```rust
use backbone_email::{EmailService, SmtpProvider, EmailAttachment};

// Create email service
let email_service = SmtpProvider::new("smtp.example.com", 587)
    .with_credentials("user", "password")
    .build()?;

// Create attachment
let attachment = EmailAttachment::builder()
    .filename("document.pdf")
    .content(pdf_bytes)
    .content_type("application/pdf")
    .build()?;

// Send email with attachment
let message = EmailMessage::builder()
    .from("sender@example.com")
    .to("recipient@example.com")
    .subject("Document Attached")
    .text("Please find the attached document")
    .attachment(attachment)
    .build()?;

let result = email_service.send(message).await?;
println!("Email with attachment sent: {}", result.message_id);
```

## ๐Ÿ”ง SMTP Configuration

### Basic Configuration

```rust
use backbone_email::SmtpProvider;

// Simple SMTP configuration
let email_service = SmtpProvider::new("smtp.example.com", 587)
    .with_credentials("username", "password")
    .build()?;
```

### Advanced Configuration

```rust
use backbone_email::SmtpProvider;

// Advanced SMTP configuration with TLS and custom settings
let email_service = SmtpProvider::new("smtp.gmail.com", 587)
    .with_credentials("user@gmail.com", "app_password")
    .with_tls(true)
    .with_timeout(30)
    .with_connection_pool_size(5)
    .with_default_sender("notifications@company.com")
    .build()?;
```

### Common SMTP Providers

#### Gmail/Google Workspace
```rust
let email_service = SmtpProvider::new("smtp.gmail.com", 587)
    .with_credentials("user@company.com", "app_password")
    .with_tls(true)
    .build()?;
```

#### Outlook/Office 365
```rust
let email_service = SmtpProvider::new("smtp.office365.com", 587)
    .with_credentials("user@company.com", "password")
    .with_tls(true)
    .build()?;
```

#### SendGrid
```rust
let email_service = SmtpProvider::new("smtp.sendgrid.net", 587)
    .with_credentials("apikey", "YOUR_SENDGRID_API_KEY")
    .with_tls(true)
    .build()?;
```

## ๐Ÿ”ง Technical Details

### Core Traits

#### EmailService Trait

```rust
#[async_trait]
pub trait EmailService: Send + Sync {
    async fn send(&self, message: EmailMessage) -> EmailResult<EmailDeliveryReport>;
    async fn send_template(
        &self,
        template_name: &str,
        recipients: Vec<String>,
        template_data: HashMap<String, serde_json::Value>,
    ) -> EmailResult<EmailDeliveryReport>;

    async fn test_connection(&self) -> EmailResult<bool>;
    async fn validate_config(&self) -> EmailResult<bool>;
}
```

### Message Types

#### EmailMessage
```rust
pub struct EmailMessage {
    pub from: String,
    pub to: Vec<String>,
    pub cc: Vec<String>,
    pub bcc: Vec<String>,
    pub subject: String,
    pub text_content: Option<String>,
    pub html_content: Option<String>,
    pub attachments: Vec<EmailAttachment>,
    pub headers: HashMap<String, String>,
    pub reply_to: Option<String>,
    pub priority: EmailPriority,
}
```

#### EmailDeliveryReport
```rust
pub struct EmailDeliveryReport {
    pub message_id: String,
    pub status: EmailStatus,
    pub provider_message_id: Option<String>,
    pub delivered_at: Option<DateTime<Utc>>,
    pub error: Option<String>,
    pub bounce_reason: Option<String>,
}
```

### Error Handling

```rust
#[derive(Debug, thiserror::Error)]
pub enum EmailError {
    #[error("SMTP connection error: {0}")]
    SmtpConnection(String),

    #[error("SMTP authentication error: {0}")]
    SmtpAuth(String),

    #[error("SMTP send error: {0}")]
    SmtpSend(String),

    #[error("Template rendering error: {0}")]
    TemplateError(String),

    #[error("Invalid email address: {0}")]
    InvalidEmail(String),

    #[error("Attachment error: {0}")]
    AttachmentError(String),

    #[error("Configuration error: {0}")]
    ConfigError(String),
}
```

## ๐Ÿ“š Examples

### 1. Welcome Email Service

```rust
use backbone_email::{EmailService, SmtpProvider};
use std::collections::HashMap;

async fn send_welcome_email(email_service: &dyn EmailService, user_email: &str, user_name: &str) -> Result<(), Box<dyn std::error::Error>> {
    let mut template_data = HashMap::new();
    template_data.insert("name".to_string(), user_name.into());
    template_data.insert("email".to_string(), user_email.into());

    let report = email_service.send_template(
        "welcome-email",
        vec![user_email],
        template_data
    ).await?;

    println!("Welcome email sent to {}: {}", user_email, report.message_id);
    Ok(())
}
```

### 2. Bulk Email Newsletter

```rust
use backbone_email::{EmailService, SmtpProvider, EmailMessage};

async fn send_newsletter(
    email_service: &dyn EmailService,
    subscribers: Vec<String>,
    subject: &str,
    content: &str
) -> Result<usize, Box<dyn std::error::Error>> {
    let mut sent_count = 0;

    for subscriber in subscribers {
        let message = EmailMessage::builder()
            .from("newsletter@company.com")
            .to(subscriber.clone())
            .subject(subject)
            .html(content)
            .build()?;

        match email_service.send(message).await {
            Ok(_) => {
                sent_count += 1;
                println!("Newsletter sent to {}", subscriber);
            }
            Err(e) => {
                eprintln!("Failed to send to {}: {}", subscriber, e);
            }
        }
    }

    println!("Newsletter sent to {} subscribers", sent_count);
    Ok(sent_count)
}
```

### 3. Email Service Health Check

```rust
use backbone_email::EmailService;

async fn check_email_health(email_service: &dyn EmailService) -> Result<bool, Box<dyn std::error::Error>> {
    // Test configuration
    if !email_service.validate_config().await? {
        println!("โŒ Email service configuration is invalid");
        return Ok(false);
    }

    // Test connection
    if !email_service.test_connection().await? {
        println!("โŒ Email service connection failed");
        return Ok(false);
    }

    println!("โœ… Email service is healthy");
    Ok(true)
}
```

## ๐Ÿ”ง Error Handling

### Error Recovery

```rust
use backbone_email::{EmailService, EmailError};

async fn send_with_retry(
    email_service: &dyn EmailService,
    message: backbone_email::EmailMessage,
    max_retries: u32
) -> Result<backbone_email::EmailDeliveryReport, Box<dyn std::error::Error>> {
    let mut retries = 0;

    loop {
        match email_service.send(message.clone()).await {
            Ok(report) => return Ok(report),
            Err(EmailError::SmtpSend(err)) if retries < max_retries => {
                retries += 1;
                println!("Send failed (attempt {}), retrying: {}", retries, err);
                tokio::time::sleep(tokio::time::Duration::from_secs(2_u64.pow(retries))).await;
            }
            Err(e) => return Err(e.into()),
        }
    }
}
```

## ๐Ÿงช Testing

### Unit Tests

```bash
# Run all backbone-email tests
cargo test --package backbone-email

# Run specific tests
cargo test --package backbone-email -- -- smtp
cargo test --package backbone-email -- -- template
```

### Mock Testing

```rust
use backbone_email::{EmailService, EmailMessage, EmailDeliveryReport};
use std::collections::HashMap;

struct MockEmailService {
    should_fail: bool,
}

#[async_trait]
impl EmailService for MockEmailService {
    async fn send(&self, message: EmailMessage) -> backbone_email::EmailResult<EmailDeliveryReport> {
        if self.should_fail {
            Err(backbone_email::EmailError::SmtpSend("Mock failure".to_string()))
        } else {
            Ok(EmailDeliveryReport {
                message_id: "mock-123".to_string(),
                status: backbone_email::EmailStatus::Sent,
                provider_message_id: Some("mock-id-123".to_string()),
                delivered_at: Some(chrono::Utc::now()),
                error: None,
                bounce_reason: None,
            })
        }
    }

    // Implement other required methods...
}
```

## ๐Ÿ”— Configuration

### Environment Variables

```bash
# SMTP Configuration
SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
SMTP_USERNAME=user@gmail.com
SMTP_PASSWORD=app_password
SMTP_USE_TLS=true
SMTP_DEFAULT_SENDER=notifications@company.com
```

### YAML Configuration

```yaml
# backbone-email configuration
email:
  provider: "smtp"

  smtp:
    host: "${SMTP_HOST}"
    port: ${SMTP_PORT}
    username: "${SMTP_USERNAME}"
    password: "${SMTP_PASSWORD}"
    use_tls: ${SMTP_USE_TLS}
    timeout: 30
    connection_pool_size: 5

  # Template settings
  templates:
    engine: "handlebars"
    directory: "./templates"

  # Default settings
  defaults:
    sender: "${SMTP_DEFAULT_SENDER}"
    priority: "normal"
```

## ๐Ÿ“Š Performance Considerations

### Connection Pooling

```rust
// Reuse the email service instance
let email_service = Arc::new(
    SmtpProvider::new("smtp.gmail.com", 587)
        .with_credentials("user", "password")
        .with_connection_pool_size(10)
        .build()?
);

// Thread-safe usage across multiple tasks
for i in 0..100 {
    let service = email_service.clone();
    tokio::spawn(async move {
        let message = EmailMessage::builder()
            .from("test@example.com")
            .to(format!("user{}@example.com", i))
            .subject(format!("Test Email {}", i))
            .text("This is a test email")
            .build()?;

        let _ = service.send(message).await;
    });
}
```

## ๐Ÿ”„ Version History

### Current Version: 2.0.0

**Features:**
- โœ… SMTP email delivery with TLS support
- โœ… Handlebars template rendering
- โœ… File attachment support
- โœ… Connection testing and validation
- โœ… Comprehensive error handling
- โœ… Async/await support
- โœ… Builder pattern configuration

**Breaking Changes from v1.x:**
- Simplified to SMTP-only implementation
- Updated API to use builder patterns
- Enhanced error handling with context
- Removed multi-provider complexity

## ๐Ÿ“„ License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## ๐Ÿ”— Related Projects

- **[Backbone Core]../backbone-core/** - Generic CRUD foundation
- **[Backbone CLI]../backbone-cli/** - Code generation tools
- **[Backbone Storage]../backbone-storage/** - File storage services
- **[Framework Documentation]../../docs/technical/** - Complete framework guide

---

**๐Ÿฆด Backbone Email - Simple, reliable SMTP email delivery**

*SMTP Focused โ€ข Template Ready โ€ข Production Ready*