Skip to content

Instantly share code, notes, and snippets.

@damiwack
Last active July 8, 2025 12:54
Show Gist options
  • Select an option

  • Save damiwack/516919a5a0b00931f0ed4046027208ca to your computer and use it in GitHub Desktop.

Select an option

Save damiwack/516919a5a0b00931f0ed4046027208ca to your computer and use it in GitHub Desktop.
Script en phyton para extraer archivos adjuntos de archivos mbox. Mbox attachment extractor.
Idea original
https://gist.github.com/georgy7/3a80bce2cd8bf2f9985c
Descarga/Download Windows 10 . Portable.
https://mega.nz/file/40k0CJqT#xoZsG-D3fm0iDf0ZrsJBSJEGQOaYYkyVk0zRbsxR7sg
Ayuda con Grok.
El script genera una interface para seleccionar el archivo mbox y la ruta destino.
This script extract attachment of mbox archive. Easy graphical interface.
Se puede generar un archivo ejecutable con el siguiente comando.
pyinstaller --onefile --windowed mbox_extractor_gui.py
import mailbox
import email
from email import policy
import os
import tkinter as tk
from tkinter import filedialog, messagebox, ttk
import shutil
class MboxExtractorApp:
def __init__(self, root):
self.root = root
self.root.title("Extract Mbox Attachments")
self.root.geometry("600x400")
# Variables
self.mbox_path = tk.StringVar()
self.output_dir = tk.StringVar()
# Interfaz
tk.Label(root, text="Mbox Attachment Extractor", font=("Arial", 16)).pack(pady=10)
# Selección de archivo mbox
tk.Label(root, text="Mbox File:").pack()
tk.Entry(root, textvariable=self.mbox_path, width=50).pack(pady=5)
tk.Button(root, text="Browse Mbox", command=self.browse_mbox).pack(pady=5)
# Selección de carpeta de salida
tk.Label(root, text="Output Directory:").pack()
tk.Entry(root, textvariable=self.output_dir, width=50).pack(pady=5)
tk.Button(root, text="Browse Output Folder", command=self.browse_output).pack(pady=5)
# Botón de extracción
tk.Button(root, text="Extract Attachments", command=self.extract_attachments).pack(pady=20)
# Barra de progreso
self.progress = ttk.Progressbar(root, length=400, mode='determinate')
self.progress.pack(pady=10)
# Área de texto para resultados
self.result_text = tk.Text(root, height=8, width=60)
self.result_text.pack(pady=10)
def browse_mbox(self):
file_path = filedialog.askopenfilename(filetypes=[("Mbox files", "*.mbox"), ("All files", "*.*")])
if file_path:
self.mbox_path.set(file_path)
def browse_output(self):
folder_path = filedialog.askdirectory()
if folder_path:
self.output_dir.set(folder_path)
def extract_attachments(self):
mbox_file = self.mbox_path.get()
output_dir = self.output_dir.get()
if not mbox_file or not output_dir:
messagebox.showerror("Error", "Please select both an mbox file and an output directory.")
return
if not os.path.exists(mbox_file):
messagebox.showerror("Error", "The selected mbox file does not exist.")
return
try:
os.makedirs(output_dir, exist_ok=True)
mbox = mailbox.mbox(mbox_file)
total_messages = len(mbox)
self.progress['maximum'] = total_messages
file_counter = 1
self.result_text.delete(1.0, tk.END)
self.result_text.insert(tk.END, "Starting extraction...\n")
for i, message in enumerate(mbox):
msg = email.message_from_bytes(message.as_bytes(), policy=policy.default)
for part in msg.walk():
if part.get_content_disposition() == 'attachment':
filename = part.get_filename()
if filename:
base, ext = os.path.splitext(filename)
filepath = os.path.join(output_dir, f"{base}_{file_counter}{ext}")
file_counter += 1
try:
with open(filepath, 'wb') as f:
f.write(part.get_payload(decode=True))
self.result_text.insert(tk.END, f"Saved: {filepath}\n")
except Exception as e:
self.result_text.insert(tk.END, f"Error saving {filename}: {str(e)}\n")
self.progress['value'] = i + 1
self.root.update()
self.result_text.insert(tk.END, "Extraction completed.\n")
messagebox.showinfo("Success", "Attachment extraction completed!")
except Exception as e:
messagebox.showerror("Error", f"An error occurred: {str(e)}")
self.progress['value'] = 0
if __name__ == "__main__":
root = tk.Tk()
app = MboxExtractorApp(root)
root.mainloop()
@damiwack
Copy link
Author

damiwack commented Jul 8, 2025

Idea original
https://gist.github.com/georgy7/3a80bce2cd8bf2f9985c

Descarga Win 10. Portable.
Download

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment