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)