Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,12 +2,16 @@
|
|
| 2 |
"""
|
| 3 |
Gradio Email Client App - Web interface for email fetching and management
|
| 4 |
Supports IMAP and POP3 protocols with secure authentication
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
"""
|
| 6 |
|
| 7 |
import gradio as gr
|
| 8 |
-
import imaplib
|
| 9 |
-
import poplib
|
| 10 |
-
import email
|
| 11 |
from email.header import decode_header
|
| 12 |
from email.utils import parsedate_to_datetime
|
| 13 |
import json
|
|
@@ -16,6 +20,7 @@ from datetime import datetime
|
|
| 16 |
import pandas as pd
|
| 17 |
from typing import List, Dict, Tuple, Optional
|
| 18 |
import re
|
|
|
|
| 19 |
|
| 20 |
# Configure logging
|
| 21 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -33,8 +38,10 @@ class EmailClient:
|
|
| 33 |
def connect_imap(self, server: str, port: int, email_addr: str, password: str, use_ssl: bool = True):
|
| 34 |
"""Connect to IMAP server"""
|
| 35 |
try:
|
|
|
|
| 36 |
if use_ssl:
|
| 37 |
-
|
|
|
|
| 38 |
else:
|
| 39 |
self.imap_conn = imaplib.IMAP4(server, port)
|
| 40 |
|
|
@@ -43,6 +50,9 @@ class EmailClient:
|
|
| 43 |
self.connected_email = email_addr
|
| 44 |
logger.info(f"Connected to IMAP server: {server}")
|
| 45 |
return True, f"β
Connected to IMAP server: {server}"
|
|
|
|
|
|
|
|
|
|
| 46 |
except Exception as e:
|
| 47 |
logger.error(f"IMAP connection failed: {e}")
|
| 48 |
return False, f"β IMAP connection failed: {str(e)}"
|
|
@@ -51,7 +61,8 @@ class EmailClient:
|
|
| 51 |
"""Connect to POP3 server"""
|
| 52 |
try:
|
| 53 |
if use_ssl:
|
| 54 |
-
|
|
|
|
| 55 |
else:
|
| 56 |
self.pop_conn = poplib.POP3(server, port)
|
| 57 |
|
|
@@ -61,6 +72,9 @@ class EmailClient:
|
|
| 61 |
self.connected_email = email_addr
|
| 62 |
logger.info(f"Connected to POP3 server: {server}")
|
| 63 |
return True, f"β
Connected to POP3 server: {server}"
|
|
|
|
|
|
|
|
|
|
| 64 |
except Exception as e:
|
| 65 |
logger.error(f"POP3 connection failed: {e}")
|
| 66 |
return False, f"β POP3 connection failed: {str(e)}"
|
|
@@ -651,9 +665,15 @@ with gr.Blocks(title="Email Client", theme=gr.themes.Soft()) as app:
|
|
| 651 |
|
| 652 |
# Launch the app
|
| 653 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
| 654 |
app.launch(
|
| 655 |
server_name="0.0.0.0",
|
| 656 |
-
server_port=
|
| 657 |
-
share=
|
| 658 |
-
show_error=True
|
|
|
|
|
|
|
| 659 |
)
|
|
|
|
| 2 |
"""
|
| 3 |
Gradio Email Client App - Web interface for email fetching and management
|
| 4 |
Supports IMAP and POP3 protocols with secure authentication
|
| 5 |
+
|
| 6 |
+
Requirements (requirements.txt):
|
| 7 |
+
gradio
|
| 8 |
+
pandas
|
| 9 |
"""
|
| 10 |
|
| 11 |
import gradio as gr
|
| 12 |
+
import imaplib # Built-in Python module
|
| 13 |
+
import poplib # Built-in Python module
|
| 14 |
+
import email # Built-in Python module
|
| 15 |
from email.header import decode_header
|
| 16 |
from email.utils import parsedate_to_datetime
|
| 17 |
import json
|
|
|
|
| 20 |
import pandas as pd
|
| 21 |
from typing import List, Dict, Tuple, Optional
|
| 22 |
import re
|
| 23 |
+
import ssl
|
| 24 |
|
| 25 |
# Configure logging
|
| 26 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 38 |
def connect_imap(self, server: str, port: int, email_addr: str, password: str, use_ssl: bool = True):
|
| 39 |
"""Connect to IMAP server"""
|
| 40 |
try:
|
| 41 |
+
# Create SSL context for better security
|
| 42 |
if use_ssl:
|
| 43 |
+
context = ssl.create_default_context()
|
| 44 |
+
self.imap_conn = imaplib.IMAP4_SSL(server, port, ssl_context=context)
|
| 45 |
else:
|
| 46 |
self.imap_conn = imaplib.IMAP4(server, port)
|
| 47 |
|
|
|
|
| 50 |
self.connected_email = email_addr
|
| 51 |
logger.info(f"Connected to IMAP server: {server}")
|
| 52 |
return True, f"β
Connected to IMAP server: {server}"
|
| 53 |
+
except imaplib.IMAP4.error as e:
|
| 54 |
+
logger.error(f"IMAP authentication failed: {e}")
|
| 55 |
+
return False, f"β IMAP authentication failed. Check your credentials."
|
| 56 |
except Exception as e:
|
| 57 |
logger.error(f"IMAP connection failed: {e}")
|
| 58 |
return False, f"β IMAP connection failed: {str(e)}"
|
|
|
|
| 61 |
"""Connect to POP3 server"""
|
| 62 |
try:
|
| 63 |
if use_ssl:
|
| 64 |
+
context = ssl.create_default_context()
|
| 65 |
+
self.pop_conn = poplib.POP3_SSL(server, port, context=context)
|
| 66 |
else:
|
| 67 |
self.pop_conn = poplib.POP3(server, port)
|
| 68 |
|
|
|
|
| 72 |
self.connected_email = email_addr
|
| 73 |
logger.info(f"Connected to POP3 server: {server}")
|
| 74 |
return True, f"β
Connected to POP3 server: {server}"
|
| 75 |
+
except poplib.error_proto as e:
|
| 76 |
+
logger.error(f"POP3 authentication failed: {e}")
|
| 77 |
+
return False, f"β POP3 authentication failed. Check your credentials."
|
| 78 |
except Exception as e:
|
| 79 |
logger.error(f"POP3 connection failed: {e}")
|
| 80 |
return False, f"β POP3 connection failed: {str(e)}"
|
|
|
|
| 665 |
|
| 666 |
# Launch the app
|
| 667 |
if __name__ == "__main__":
|
| 668 |
+
# Check if running in a deployment environment
|
| 669 |
+
import os
|
| 670 |
+
port = int(os.environ.get("PORT", 7860))
|
| 671 |
+
|
| 672 |
app.launch(
|
| 673 |
server_name="0.0.0.0",
|
| 674 |
+
server_port=port,
|
| 675 |
+
share=False,
|
| 676 |
+
show_error=True,
|
| 677 |
+
show_tips=False,
|
| 678 |
+
quiet=False
|
| 679 |
)
|