Skip to main content

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::builder().proxy("http://127.0.0.1:8080").build().await?;
28
29    // Custom configuration example (uncomment to use):
30    // let client = Client::builder()
31    //     .proxy("http://127.0.0.1:8080")
32    //     .danger_accept_invalid_certs(false)
33    //     .user_agent("guerrillamail-demo/1.0")
34    //     .ajax_url("https://www.guerrillamail.com/ajax.php")
35    //     .build()
36    //     .await?;
37
38    println!("   āœ… Connected to GuerrillaMail API");
39
40    // =========================================
41    // 2. Create temporary email address
42    // =========================================
43    println!("\nšŸ“¬ Creating temporary email...");
44    let alias = format!("demo{}", rand::random::<u16>());
45    let email = client.create_email(&alias).await?;
46    println!("   āœ… Created: {}", email);
47
48    // =========================================
49    // 3. Poll for messages (get_messages)
50    // =========================================
51    println!("\nā³ Waiting for messages...");
52    println!("   Send an email to: {}", email);
53    println!("   (Polling for up to 2 minutes)");
54
55    let start = std::time::Instant::now();
56    let timeout = std::time::Duration::from_secs(120);
57    let poll_interval = std::time::Duration::from_secs(5);
58
59    loop {
60        // get_messages returns basic info: id, from, subject, excerpt
61        let messages = client.get_messages(&email).await?;
62
63        if !messages.is_empty() {
64            println!("\n\nšŸ“„ Received {} message(s)!", messages.len());
65
66            for msg in &messages {
67                println!("\n{}", "-".repeat(50));
68                println!("Message ID:  {}", msg.mail_id);
69                println!("From:        {}", msg.mail_from);
70                println!("Subject:     {}", msg.mail_subject);
71                println!(
72                    "Excerpt:     {}",
73                    &msg.mail_excerpt[..msg.mail_excerpt.len().min(80)]
74                );
75                println!("Timestamp:   {}", msg.mail_timestamp);
76
77                // =========================================
78                // 4. Fetch full email content (fetch_email)
79                // =========================================
80                println!("\nšŸ“„ Fetching full email body...");
81                match client.fetch_email(&email, &msg.mail_id).await {
82                    Ok(details) => {
83                        println!("   Body length: {} characters", details.mail_body.len());
84                        println!("   Preview (first 500 chars):");
85                        println!("   {}", "-".repeat(40));
86                        let preview: String = details.mail_body.chars().take(500).collect();
87                        for line in preview.lines().take(10) {
88                            println!("   {}", line);
89                        }
90                        if details.mail_body.len() > 500 {
91                            println!("   ... (truncated)");
92                        }
93                    }
94                    Err(e) => {
95                        eprintln!("   āŒ Failed to fetch: {}", e);
96                    }
97                }
98            }
99            break;
100        }
101
102        if start.elapsed() >= timeout {
103            println!("\n\nāš ļø  Timeout: No messages received");
104            break;
105        }
106
107        let remaining = (timeout - start.elapsed()).as_secs();
108        print!("\r   Checking... {} seconds remaining   ", remaining);
109        use std::io::Write;
110        std::io::stdout().flush().ok();
111
112        tokio::time::sleep(poll_interval).await;
113    }
114
115    // =========================================
116    // 5. Delete/forget email address
117    // =========================================
118    println!("\nšŸ—‘ļø  Cleaning up email address...");
119    match client.delete_email(&email).await {
120        Ok(true) => println!("   āœ… Email address deleted"),
121        Ok(false) => println!("   āš ļø  Deletion may have failed"),
122        Err(e) => eprintln!("   āŒ Error: {}", e),
123    }
124
125    println!("\n{}", "=".repeat(50));
126    println!("✨ Demo complete!");
127
128    Ok(())
129}