Correos funcional correctamente
This commit is contained in:
parent
bf73b52d12
commit
c80721e33e
21
app/main.py
21
app/main.py
|
@ -18,19 +18,16 @@ from solapas.SQLQueryExecutor import SQLQueryExecutor
|
||||||
from solapas.TicTacToe import TicTacToe
|
from solapas.TicTacToe import TicTacToe
|
||||||
from solapas.WebScraperToDB import WebScraperToDB
|
from solapas.WebScraperToDB import WebScraperToDB
|
||||||
|
|
||||||
|
|
||||||
# Configuración del servidor de correos
|
# Configuración del servidor de correos
|
||||||
smtp_server = "192.168.120.103"
|
SMTP_SERVER = "192.168.120.103"
|
||||||
smtp_port = 465
|
SMTP_PORT = 587 # Usar SSL para mayor seguridad
|
||||||
imap_server = "192.168.120.103"
|
|
||||||
imap_port = 993
|
|
||||||
username = "tu_usuario@psp.ieslamar.org"
|
|
||||||
password = "tu_contraseña"
|
|
||||||
|
|
||||||
# Clave de API de OpenWeatherMap
|
# Clave de API de OpenWeatherMap
|
||||||
API_KEY = "1fa8fd05b650773bbc3f2130657e808a"
|
API_KEY = "1fa8fd05b650773bbc3f2130657e808a"
|
||||||
|
|
||||||
# Crear cliente de correo
|
# Crear cliente de correo
|
||||||
mail_client = MailClient(smtp_server, smtp_port, imap_server, imap_port, username, password)
|
mail_client = MailClient(SMTP_SERVER, SMTP_PORT)
|
||||||
|
|
||||||
def update_time(status_bar):
|
def update_time(status_bar):
|
||||||
"""Función que actualiza la hora y el día de la semana en un label"""
|
"""Función que actualiza la hora y el día de la semana en un label"""
|
||||||
|
@ -154,35 +151,35 @@ notebook.pack(fill="both", expand=True)
|
||||||
|
|
||||||
# Crear la Solapa 1 y añadir el downloader
|
# Crear la Solapa 1 y añadir el downloader
|
||||||
tab1 = ttk.Frame(notebook)
|
tab1 = ttk.Frame(notebook)
|
||||||
notebook.add(tab1, text="Solapa 1", padding=4)
|
notebook.add(tab1, text="Downloader", padding=4)
|
||||||
|
|
||||||
# Añadir el downloader a la Solapa 1
|
# Añadir el downloader a la Solapa 1
|
||||||
music_downloader = MusicDownloader(tab1)
|
music_downloader = MusicDownloader(tab1)
|
||||||
|
|
||||||
# Crear la Solapa 2 y añadir los gráficos
|
# Crear la Solapa 2 y añadir los gráficos
|
||||||
tab2 = ttk.Frame(notebook)
|
tab2 = ttk.Frame(notebook)
|
||||||
notebook.add(tab2, text="Solapa 2", padding=4)
|
notebook.add(tab2, text="Graphics", padding=4)
|
||||||
|
|
||||||
# Añadir los gráficos de economía mundial y Bitcoin a la Solapa 2
|
# Añadir los gráficos de economía mundial y Bitcoin a la Solapa 2
|
||||||
economy_bitcoin_chart = EconomyBitcoinChart(tab2)
|
economy_bitcoin_chart = EconomyBitcoinChart(tab2)
|
||||||
|
|
||||||
# Crear la Solapa 3 y añadir el Tic Tac Toe
|
# Crear la Solapa 3 y añadir el Tic Tac Toe
|
||||||
tab3 = ttk.Frame(notebook)
|
tab3 = ttk.Frame(notebook)
|
||||||
notebook.add(tab3, text="Solapa 3", padding=4)
|
notebook.add(tab3, text="Tic Tac Toe", padding=4)
|
||||||
|
|
||||||
# Añadir el juego de Tic Tac Toe a la Solapa 3
|
# Añadir el juego de Tic Tac Toe a la Solapa 3
|
||||||
tic_tac_toe = TicTacToe(tab3)
|
tic_tac_toe = TicTacToe(tab3)
|
||||||
|
|
||||||
# Crear la Solapa 4 y añadir el SQL Query Executor
|
# Crear la Solapa 4 y añadir el SQL Query Executor
|
||||||
tab4 = ttk.Frame(notebook)
|
tab4 = ttk.Frame(notebook)
|
||||||
notebook.add(tab4, text="Solapa 4", padding=4)
|
notebook.add(tab4, text="SQL Querys", padding=4)
|
||||||
|
|
||||||
# Añadir el ejecutor de consultas SQL a la Solapa 4
|
# Añadir el ejecutor de consultas SQL a la Solapa 4
|
||||||
sql_query_executor = SQLQueryExecutor(tab4)
|
sql_query_executor = SQLQueryExecutor(tab4)
|
||||||
|
|
||||||
# Crear la Solapa 5 y añadir el Web Scraper
|
# Crear la Solapa 5 y añadir el Web Scraper
|
||||||
tab5 = ttk.Frame(notebook)
|
tab5 = ttk.Frame(notebook)
|
||||||
notebook.add(tab5, text="Solapa 5", padding=4)
|
notebook.add(tab5, text="Web Scraper", padding=4)
|
||||||
|
|
||||||
# Añadir el widget de Web Scraper a la Solapa 5
|
# Añadir el widget de Web Scraper a la Solapa 5
|
||||||
web_scraper = WebScraperToDB(tab5)
|
web_scraper = WebScraperToDB(tab5)
|
||||||
|
|
|
@ -3,14 +3,14 @@ from tkinter import ttk, messagebox
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
class MailTab:
|
class MailTab:
|
||||||
def __init__(self, notebook, mail_client):
|
def __init__(self, parent, mail_client):
|
||||||
self.mail_client = mail_client
|
self.mail_client = mail_client
|
||||||
|
|
||||||
# Crear el frame de la pestaña dentro del Notebook
|
# Crear el frame de la pestaña
|
||||||
self.frame = ttk.Frame(notebook)
|
self.frame = ttk.Frame(parent)
|
||||||
notebook.add(self.frame, text="Correo") # ← Ahora esto es correcto
|
parent.add(self.frame, text="Correo")
|
||||||
|
|
||||||
# Campos de entrada para credenciales
|
# Campos de entrada para enviar correo
|
||||||
ttk.Label(self.frame, text="Correo electrónico:").grid(row=0, column=0, sticky="e", padx=5, pady=5)
|
ttk.Label(self.frame, text="Correo electrónico:").grid(row=0, column=0, sticky="e", padx=5, pady=5)
|
||||||
self.entry_email = ttk.Entry(self.frame, width=40)
|
self.entry_email = ttk.Entry(self.frame, width=40)
|
||||||
self.entry_email.grid(row=0, column=1, padx=5, pady=5)
|
self.entry_email.grid(row=0, column=1, padx=5, pady=5)
|
||||||
|
@ -19,62 +19,30 @@ class MailTab:
|
||||||
self.entry_password = ttk.Entry(self.frame, show="*", width=40)
|
self.entry_password = ttk.Entry(self.frame, show="*", width=40)
|
||||||
self.entry_password.grid(row=1, column=1, padx=5, pady=5)
|
self.entry_password.grid(row=1, column=1, padx=5, pady=5)
|
||||||
|
|
||||||
self.login_button = ttk.Button(self.frame, text="Iniciar Sesión", command=self.login)
|
ttk.Label(self.frame, text="Destinatario:").grid(row=2, column=0, sticky="e", padx=5, pady=5)
|
||||||
self.login_button.grid(row=2, column=1, padx=5, pady=5, sticky="e")
|
|
||||||
|
|
||||||
# Campos para enviar correo
|
|
||||||
ttk.Label(self.frame, text="Destinatario:").grid(row=3, column=0, padx=5, pady=5, sticky="e")
|
|
||||||
self.recipient_entry = ttk.Entry(self.frame, width=40)
|
self.recipient_entry = ttk.Entry(self.frame, width=40)
|
||||||
self.recipient_entry.grid(row=3, column=1, padx=5, pady=5)
|
self.recipient_entry.grid(row=2, column=1, padx=5, pady=5)
|
||||||
|
|
||||||
ttk.Label(self.frame, text="Asunto:").grid(row=4, column=0, padx=5, pady=5, sticky="e")
|
ttk.Label(self.frame, text="Asunto:").grid(row=3, column=0, sticky="e", padx=5, pady=5)
|
||||||
self.subject_entry = ttk.Entry(self.frame, width=40)
|
self.subject_entry = ttk.Entry(self.frame, width=40)
|
||||||
self.subject_entry.grid(row=4, column=1, padx=5, pady=5)
|
self.subject_entry.grid(row=3, column=1, padx=5, pady=5)
|
||||||
|
|
||||||
ttk.Label(self.frame, text="Mensaje:").grid(row=5, column=0, padx=5, pady=5, sticky="ne")
|
ttk.Label(self.frame, text="Mensaje:").grid(row=4, column=0, sticky="ne", padx=5, pady=5)
|
||||||
self.body_text = tk.Text(self.frame, width=50, height=10)
|
self.body_text = tk.Text(self.frame, width=50, height=10)
|
||||||
self.body_text.grid(row=5, column=1, padx=5, pady=5)
|
self.body_text.grid(row=4, column=1, padx=5, pady=5)
|
||||||
|
|
||||||
self.send_button = ttk.Button(self.frame, text="Enviar", command=self.send_email_thread)
|
self.send_button = ttk.Button(self.frame, text="Enviar", command=self.send_email_thread)
|
||||||
self.send_button.grid(row=6, column=1, padx=5, pady=5, sticky="e")
|
self.send_button.grid(row=5, column=1, padx=5, pady=5, sticky="e")
|
||||||
|
|
||||||
# Widgets para recibir correos
|
|
||||||
self.receive_button = ttk.Button(self.frame, text="Recibir Correos", command=self.fetch_emails_thread)
|
|
||||||
self.receive_button.grid(row=7, column=1, padx=5, pady=5, sticky="e")
|
|
||||||
|
|
||||||
self.emails_listbox = tk.Listbox(self.frame, height=10, width=80)
|
|
||||||
self.emails_listbox.grid(row=8, column=0, columnspan=2, padx=5, pady=5)
|
|
||||||
|
|
||||||
def login(self):
|
|
||||||
email = self.entry_email.get()
|
|
||||||
password = self.entry_password.get()
|
|
||||||
result = self.mail_client.login(email, password)
|
|
||||||
if result:
|
|
||||||
messagebox.showinfo("Inicio de Sesión", "Inicio de sesión exitoso")
|
|
||||||
else:
|
|
||||||
messagebox.showerror("Error", "Credenciales incorrectas")
|
|
||||||
|
|
||||||
def send_email_thread(self):
|
def send_email_thread(self):
|
||||||
threading.Thread(target=self.send_email).start()
|
threading.Thread(target=self.send_email).start()
|
||||||
|
|
||||||
def send_email(self):
|
def send_email(self):
|
||||||
|
sender_email = self.entry_email.get()
|
||||||
|
sender_password = self.entry_password.get()
|
||||||
recipient = self.recipient_entry.get()
|
recipient = self.recipient_entry.get()
|
||||||
subject = self.subject_entry.get()
|
subject = self.subject_entry.get()
|
||||||
body = self.body_text.get("1.0", tk.END).strip()
|
body = self.body_text.get("1.0", tk.END).strip()
|
||||||
result = self.mail_client.send_email(recipient, subject, body)
|
|
||||||
|
result = self.mail_client.send_email(sender_email, sender_password, recipient, subject, body)
|
||||||
messagebox.showinfo("Resultado", result)
|
messagebox.showinfo("Resultado", result)
|
||||||
|
|
||||||
def fetch_emails_thread(self):
|
|
||||||
threading.Thread(target=self.fetch_emails).start()
|
|
||||||
|
|
||||||
def fetch_emails(self):
|
|
||||||
try:
|
|
||||||
result = self.mail_client.fetch_emails()
|
|
||||||
if isinstance(result, list):
|
|
||||||
self.emails_listbox.delete(0, tk.END)
|
|
||||||
for email in result:
|
|
||||||
self.emails_listbox.insert(tk.END, email[:100]) # Mostrar los primeros 100 caracteres
|
|
||||||
else:
|
|
||||||
messagebox.showerror("Error", result)
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Error al recibir correos: {e}")
|
|
||||||
|
|
|
@ -1,41 +1,17 @@
|
||||||
import smtplib
|
import smtplib
|
||||||
import imaplib
|
|
||||||
|
|
||||||
class MailClient:
|
class MailClient:
|
||||||
def __init__(self, smtp_server, smtp_port, imap_server, imap_port, username, password):
|
def __init__(self, smtp_server, smtp_port):
|
||||||
self.smtp_server = smtp_server
|
self.smtp_server = smtp_server
|
||||||
self.smtp_port = smtp_port
|
self.smtp_port = smtp_port
|
||||||
self.imap_server = imap_server
|
|
||||||
self.imap_port = imap_port
|
|
||||||
self.username = username
|
|
||||||
self.password = password
|
|
||||||
|
|
||||||
def send_email(self, recipient, subject, body):
|
def send_email(self, sender_email, sender_password, recipient, subject, body):
|
||||||
"""Envía un correo utilizando el servidor SMTP."""
|
"""Envía un correo utilizando el servidor SMTP con SSL."""
|
||||||
try:
|
try:
|
||||||
with smtplib.SMTP_SSL(self.smtp_server, self.smtp_port) as server:
|
with smtplib.SMTP(self.smtp_server, self.smtp_port) as server:
|
||||||
server.login(self.username, self.password)
|
server.login(sender_email, sender_password)
|
||||||
message = f"Subject: {subject}\\n\\n{body}"
|
message = f"Subject: {subject}\n\n{body}"
|
||||||
server.sendmail(self.username, recipient, message)
|
server.sendmail(sender_email, recipient, message)
|
||||||
return "Correo enviado correctamente"
|
return "Correo enviado correctamente"
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return f"Error al enviar el correo: {str(e)}"
|
return f"Error al enviar el correo: {str(e)}"
|
||||||
|
|
||||||
def fetch_emails(self):
|
|
||||||
"""Obtiene los correos más recientes utilizando el servidor IMAP."""
|
|
||||||
try:
|
|
||||||
with imaplib.IMAP4_SSL(self.imap_server, self.imap_port) as server:
|
|
||||||
server.login(self.username, self.password)
|
|
||||||
server.select("inbox")
|
|
||||||
status, messages = server.search(None, "ALL")
|
|
||||||
email_ids = messages[0].split()
|
|
||||||
emails = []
|
|
||||||
|
|
||||||
for email_id in email_ids[-5:]: # Obtener los últimos 5 correos
|
|
||||||
status, msg_data = server.fetch(email_id, "(RFC822)")
|
|
||||||
for response_part in msg_data:
|
|
||||||
if isinstance(response_part, tuple):
|
|
||||||
emails.append(response_part[1].decode())
|
|
||||||
return emails
|
|
||||||
except Exception as e:
|
|
||||||
return f"Error al recibir correos: {str(e)}"
|
|
||||||
|
|
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue