demo/
demo.rs

1//! Comprehensive example showcasing all GuerrillaMail client functionality.
2//!
3//! Features demonstrated:
4//! - Creating a client (with optional proxy support)
5//! - Viewing available email domains
6//! - Creating a temporary email address
7//! - Polling for incoming messages
8//! - Fetching full email content
9//! - Deleting/cleaning up the email address
10
11use guerrillamail_client::Client;
12
13#[tokio::main]
14async fn main() -> Result<(), Box<dyn std::error::Error>> {
15    println!("šŸ“§ GuerrillaMail Rust Client - Full Demo");
16    println!("{}", "=".repeat(50));
17
18    // =========================================
19    // 1. Create client (optionally with proxy)
20    // =========================================
21    println!("\nšŸ”Œ Creating client...");
22
23    // Without proxy:
24    let client = Client::new().await?;
25
26    // With proxy (uncomment to use):
27    // let client = Client::with_proxy(Some("http://127.0.0.1:8080")).await?;
28
29    println!("   āœ… Connected to GuerrillaMail API");
30
31    // =========================================
32    // 2. View available domains
33    // =========================================
34    println!("\n🌐 Available email domains:");
35    for domain in client.domains() {
36        println!("   - {}", domain);
37    }
38
39    // =========================================
40    // 3. Create temporary email address
41    // =========================================
42    println!("\nšŸ“¬ Creating temporary email...");
43    let alias = format!("demo{}", rand::random::<u16>());
44    let email = client.create_email(&alias, None).await?;
45    println!("   āœ… Created: {}", email);
46
47    // =========================================
48    // 4. Poll for messages (get_messages)
49    // =========================================
50    println!("\nā³ Waiting for messages...");
51    println!("   Send an email to: {}", email);
52    println!("   (Polling for up to 2 minutes)");
53
54    let start = std::time::Instant::now();
55    let timeout = std::time::Duration::from_secs(120);
56    let poll_interval = std::time::Duration::from_secs(5);
57
58    loop {
59        // get_messages returns basic info: id, from, subject, excerpt
60        let messages = client.get_messages(&email).await?;
61
62        if !messages.is_empty() {
63            println!("\n\nšŸ“„ Received {} message(s)!", messages.len());
64
65            for msg in &messages {
66                println!("\n{}", "-".repeat(50));
67                println!("Message ID:  {}", msg.mail_id);
68                println!("From:        {}", msg.mail_from);
69                println!("Subject:     {}", msg.mail_subject);
70                println!(
71                    "Excerpt:     {}",
72                    &msg.mail_excerpt[..msg.mail_excerpt.len().min(80)]
73                );
74                println!("Timestamp:   {}", msg.mail_timestamp);
75
76                // =========================================
77                // 5. Fetch full email content (fetch_email)
78                // =========================================
79                println!("\nšŸ“„ Fetching full email body...");
80                match client.fetch_email(&email, &msg.mail_id).await {
81                    Ok(details) => {
82                        println!("   Body length: {} characters", details.mail_body.len());
83                        println!("   Preview (first 500 chars):");
84                        println!("   {}", "-".repeat(40));
85                        let preview: String = details.mail_body.chars().take(500).collect();
86                        for line in preview.lines().take(10) {
87                            println!("   {}", line);
88                        }
89                        if details.mail_body.len() > 500 {
90                            println!("   ... (truncated)");
91                        }
92                    }
93                    Err(e) => {
94                        eprintln!("   āŒ Failed to fetch: {}", e);
95                    }
96                }
97            }
98            break;
99        }
100
101        if start.elapsed() >= timeout {
102            println!("\n\nāš ļø  Timeout: No messages received");
103            break;
104        }
105
106        let remaining = (timeout - start.elapsed()).as_secs();
107        print!("\r   Checking... {} seconds remaining   ", remaining);
108        use std::io::Write;
109        std::io::stdout().flush().ok();
110
111        tokio::time::sleep(poll_interval).await;
112    }
113
114    // =========================================
115    // 6. Delete/forget email address
116    // =========================================
117    println!("\nšŸ—‘ļø  Cleaning up email address...");
118    match client.delete_email(&email).await {
119        Ok(true) => println!("   āœ… Email address deleted"),
120        Ok(false) => println!("   āš ļø  Deletion may have failed"),
121        Err(e) => eprintln!("   āŒ Error: {}", e),
122    }
123
124    println!("\n{}", "=".repeat(50));
125    println!("✨ Demo complete!");
126
127    Ok(())
128}