Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import time
|
| 4 |
+
import random
|
| 5 |
+
import string
|
| 6 |
+
|
| 7 |
+
# --- Configuration ---
|
| 8 |
+
# URL for a free temp mail API (using temp-mail.org as an example)
|
| 9 |
+
BASE_URL = "https://api.temp-mail.org"
|
| 10 |
+
# ---
|
| 11 |
+
|
| 12 |
+
def get_temp_email():
|
| 13 |
+
"""Generates a temp email using the API."""
|
| 14 |
+
# Step 1: Get available domains
|
| 15 |
+
try:
|
| 16 |
+
domain_response = requests.get(f"{BASE_URL}/request/domains/format/json")
|
| 17 |
+
domain_response.raise_for_status()
|
| 18 |
+
domains = domain_response.json()
|
| 19 |
+
if not domains:
|
| 20 |
+
return "Error: No domains available", ""
|
| 21 |
+
domain = domains[0] # Use the first available domain
|
| 22 |
+
except Exception as e:
|
| 23 |
+
return f"Error fetching domains: {e}", ""
|
| 24 |
+
|
| 25 |
+
# Step 2: Generate a random username
|
| 26 |
+
username = ''.join(random.choices(string.ascii_lowercase, k=10))
|
| 27 |
+
email = f"{username}{domain}"
|
| 28 |
+
|
| 29 |
+
# Step 3: Request the email be created (some APIs auto-generate on first check)
|
| 30 |
+
return "Email generated successfully!", email
|
| 31 |
+
|
| 32 |
+
def check_inbox(email):
|
| 33 |
+
"""Fetches the inbox for a given temp email."""
|
| 34 |
+
if not email or "@" not in email:
|
| 35 |
+
return "Please generate an email first!"
|
| 36 |
+
|
| 37 |
+
# Extract username and domain from the full email address
|
| 38 |
+
try:
|
| 39 |
+
username, domain = email.split("@")
|
| 40 |
+
messages_url = f"{BASE_URL}/request/mail/id/{username}/format/json"
|
| 41 |
+
response = requests.get(messages_url)
|
| 42 |
+
response.raise_for_status()
|
| 43 |
+
messages = response.json()
|
| 44 |
+
except Exception as e:
|
| 45 |
+
return f"Error checking inbox: {e}"
|
| 46 |
+
|
| 47 |
+
if not messages:
|
| 48 |
+
return "No messages yet. Check back in a few seconds."
|
| 49 |
+
|
| 50 |
+
output = []
|
| 51 |
+
for msg in messages:
|
| 52 |
+
from_addr = msg.get("mail_from", "Unknown Sender")
|
| 53 |
+
subject = msg.get("mail_subject", "No Subject")
|
| 54 |
+
body = msg.get("mail_text_only", "No text content.")
|
| 55 |
+
output.append(f"From: {from_addr}\nSubject: {subject}\nBody:\n{body}\n{'-'*30}")
|
| 56 |
+
|
| 57 |
+
return "\n\n".join(output)
|
| 58 |
+
|
| 59 |
+
# --- Gradio UI ---
|
| 60 |
+
with gr.Blocks(title="Temp Mail", theme=gr.themes.Soft()) as ui:
|
| 61 |
+
gr.Markdown("# 📧 Temporary Email Service")
|
| 62 |
+
gr.Markdown("Generate a disposable email address and check your inbox!")
|
| 63 |
+
|
| 64 |
+
with gr.Row():
|
| 65 |
+
status_box = gr.Textbox(label="Status", interactive=False, scale=2)
|
| 66 |
+
email_box = gr.Textbox(label="Your Temporary Email", interactive=False, scale=3)
|
| 67 |
+
|
| 68 |
+
generate_btn = gr.Button("Generate New Email")
|
| 69 |
+
check_btn = gr.Button("Check Inbox")
|
| 70 |
+
inbox_display = gr.Textbox(label="Inbox Messages", interactive=False, lines=10)
|
| 71 |
+
|
| 72 |
+
generate_btn.click(get_temp_email, outputs=[status_box, email_box])
|
| 73 |
+
check_btn.click(check_inbox, inputs=email_box, outputs=inbox_display)
|
| 74 |
+
|
| 75 |
+
app = ui.launch(server_name="0.0.0.0", server_port=7860)
|