File size: 2,773 Bytes
a6c2ca1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import requests
import time
import random
import string

# --- Configuration ---
# URL for a free temp mail API (using temp-mail.org as an example)
BASE_URL = "https://api.temp-mail.org"
# ---

def get_temp_email():
    """Generates a temp email using the API."""
    # Step 1: Get available domains
    try:
        domain_response = requests.get(f"{BASE_URL}/request/domains/format/json")
        domain_response.raise_for_status()
        domains = domain_response.json()
        if not domains:
            return "Error: No domains available", ""
        domain = domains[0] # Use the first available domain
    except Exception as e:
        return f"Error fetching domains: {e}", ""
    
    # Step 2: Generate a random username
    username = ''.join(random.choices(string.ascii_lowercase, k=10))
    email = f"{username}{domain}"
    
    # Step 3: Request the email be created (some APIs auto-generate on first check)
    return "Email generated successfully!", email

def check_inbox(email):
    """Fetches the inbox for a given temp email."""
    if not email or "@" not in email:
        return "Please generate an email first!"
    
    # Extract username and domain from the full email address
    try:
        username, domain = email.split("@")
        messages_url = f"{BASE_URL}/request/mail/id/{username}/format/json"
        response = requests.get(messages_url)
        response.raise_for_status()
        messages = response.json()
    except Exception as e:
        return f"Error checking inbox: {e}"
    
    if not messages:
        return "No messages yet. Check back in a few seconds."
    
    output = []
    for msg in messages:
        from_addr = msg.get("mail_from", "Unknown Sender")
        subject = msg.get("mail_subject", "No Subject")
        body = msg.get("mail_text_only", "No text content.")
        output.append(f"From: {from_addr}\nSubject: {subject}\nBody:\n{body}\n{'-'*30}")
    
    return "\n\n".join(output)

# --- Gradio UI ---
with gr.Blocks(title="Temp Mail", theme=gr.themes.Soft()) as ui:
    gr.Markdown("# 📧 Temporary Email Service")
    gr.Markdown("Generate a disposable email address and check your inbox!")
    
    with gr.Row():
        status_box = gr.Textbox(label="Status", interactive=False, scale=2)
        email_box = gr.Textbox(label="Your Temporary Email", interactive=False, scale=3)
    
    generate_btn = gr.Button("Generate New Email")
    check_btn = gr.Button("Check Inbox")
    inbox_display = gr.Textbox(label="Inbox Messages", interactive=False, lines=10)
    
    generate_btn.click(get_temp_email, outputs=[status_box, email_box])
    check_btn.click(check_inbox, inputs=email_box, outputs=inbox_display)

app = ui.launch(server_name="0.0.0.0", server_port=7860)