certainly, and thanks for responding…
import tkinter as tk
from tkinter import filedialog, messagebox
import email
import os
from PIL import Image
from io import BytesIO
import webptools
import csv
import json
import requests
import subprocess
checkbox_vars = # Declare checkbox_vars as a global variable
attachments = # Declare attachments as a global variable
email_subject = “” # Declare email_subject as a global variable
msg = None # Declare msg as a global variable
def process_email(file_path):
global attachments, email_subject, msg
with open(file_path, 'r', encoding='utf-8') as file:
msg = email.message_from_file(file)
email_subject = msg['Subject']
print("Email Subject:", email_subject)
print("Email From:", msg['From'])
print("Email To:", msg['To'])
print("Email Body:")
if msg.is_multipart():
for part in msg.walk():
content_type = part.get_content_type()
disposition = str(part.get('Content-Disposition'))
if content_type == 'text/plain' and 'attachment' not in disposition:
print(part.get_payload(decode=True).decode())
else:
print(msg.get_payload())
attachments = []
for part in msg.walk():
content_type = part.get_content_type()
disposition = str(part.get('Content-Disposition'))
if 'attachment' in disposition:
filename = part.get_filename()
if filename:
attachments.append(filename)
if attachments:
print("Attachments:")
for attachment in attachments:
print(attachment)
else:
print("No attachments found.")
def select_file():
global file_path
file_path = filedialog.askopenfilename(filetypes=[(‘Email Files’, ‘*.eml’)])
if file_path:
process_email(file_path)
update_attachments_report()
def save_attachments():
global msg # Access the global msg variable
selected_attachments = []
for checkbox_var, attachment in zip(checkbox_vars, attachments):
if checkbox_var.get() == 1: # Check if the variable's value is 1 (selected)
selected_attachments.append(attachment)
if selected_attachments:
folder_name = email_subject.replace("/", "-") # Replace invalid characters in subject for folder name
folder_path = os.path.join(os.getcwd(), folder_name)
os.makedirs(folder_path, exist_ok=True)
for attachment in selected_attachments:
attachment_path = os.path.join(folder_path, attachment)
with open(attachment_path, 'wb') as file:
# Retrieve the attachment content from the email message
for part in msg.walk():
content_type = part.get_content_type()
disposition = str(part.get('Content-Disposition'))
if disposition and 'attachment' in disposition and part.get_filename() == attachment:
attachment_content = part.get_payload(decode=True)
if attachment_content:
# Save the attachment as an image with size constraints
image = Image.open(BytesIO(attachment_content))
image.thumbnail((600, 600), Image.LANCZOS)
image.save(file, format='JPEG', quality=90) # Save as JPEG format
# Convert the saved JPEG image to WebP format using Pillow
webp_path = attachment_path.replace(".jpg", ".webp")
image.save(webp_path, format='WebP', quality=90)
print("Attachments saved to:", folder_path)
else:
print("No attachments selected.")
def update_attachments_report():
for widget in attachments_frame.winfo_children():
widget.destroy()
global checkboxes, checkbox_vars
checkboxes = []
checkbox_vars = []
if attachments:
attachments_label = tk.Label(attachments_frame, text="Attachments:")
attachments_label.pack(anchor="w")
for attachment in attachments:
checkbox_var = tk.IntVar()
checkbox = tk.Checkbutton(attachments_frame, text=attachment, variable=checkbox_var)
checkbox.pack(anchor="w")
checkboxes.append(checkbox)
checkbox_vars.append(checkbox_var)
else:
no_attachments_label = tk.Label(attachments_frame, text="No attachments found.")
no_attachments_label.pack(anchor="w")
def create_prompt():
# Check if an email has been processed
if msg is None:
messagebox.showerror(“Error”, “No email file selected.”)
return
prompt_content = "" # Initialize the prompt content
# Read the content of the MasterPrompt file
prompt_folder = "PROMPT"
master_prompt_file = os.path.join(prompt_folder, "MasterPrompt.txt")
with open(master_prompt_file, 'r', encoding='utf-8') as file:
prompt_content += file.read() # Append the MasterPrompt content
email_content = ""
if msg.is_multipart():
for part in msg.walk():
content_type = part.get_content_type()
disposition = str(part.get('Content-Disposition'))
if content_type == 'text/plain' and 'attachment' not in disposition:
email_content += part.get_payload(decode=True).decode()
else:
email_content = msg.get_payload()
# Append the email content to the prompt
prompt_content += "\n" + email_content
# Write the prompt content to the prompt file
to_chat_folder = "TO_CHAT"
os.makedirs(to_chat_folder, exist_ok=True)
prompt_file_path = os.path.join(to_chat_folder, "prompt.txt")
with open(prompt_file_path, 'w', encoding='utf-8') as file:
file.write(prompt_content)
messagebox.showinfo("Success", "Prompt created.")
def send_to_api():
# Check if a prompt file exists
prompt_file_path = os.path.join(“TO_CHAT”, “prompt.txt”)
if not os.path.isfile(prompt_file_path):
messagebox.showerror(“Error”, “Prompt file not found.”)
return
# Read the prompt content from the file
with open(prompt_file_path, 'r', encoding='utf-8') as file:
prompt_content = file.read()
# Call the ChatGPT API to process the prompt
api_key = "0000" # Replace with your actual API key. I have removed mine
api_url = "https://api.openai.com/v1/chat/completions"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
data = {
"prompt": prompt_content,
"max_tokens": 100,
"n": 1
}
try:
response = requests.post(api_url, headers=headers, json=data)
response.raise_for_status()
response_json = response.json()
if "choices" in response_json:
choices = response_json["choices"]
if choices:
completion = choices[0]["text"]
# Handle the completion as per your requirements
messagebox.showinfo("Success", "Prompt processed.")
else:
messagebox.showinfo("Info", "No completion received from the API.")
else:
messagebox.showerror("Error", "Unexpected response format.")
except requests.exceptions.HTTPError as e:
error_message = str(e)
messagebox.showerror("Error", f"API call failed: {error_message}")
log_error(error_message)
except requests.exceptions.RequestException as e:
error_message = str(e)
messagebox.showerror("Error", f"An error occurred: {error_message}")
log_error(error_message)
def log_error(error_message):
log_folder = “LOGS”
os.makedirs(log_folder, exist_ok=True)
log_file = os.path.join(log_folder, “error_log.txt”)
with open(log_file, "a", encoding="utf-8") as file:
file.write(f"Error: {error_message}\n")
Create the GUI window
window = tk.Tk()
window.title(‘Brundlefly’)
window.configure(bg=‘dark grey’)
window.geometry(‘400x250’)
Create the select file button
select_file_button = tk.Button(window, text=‘Select .eml File’, command=select_file, bd=1, relief=‘solid’, fg=‘red’)
select_file_button.pack()
Create the attachments report canvas
attachments_canvas = tk.Canvas(window, bg=‘white’)
attachments_canvas.pack(fill=tk.BOTH, expand=True, side=tk.LEFT)
Create the attachments report frame
attachments_frame = tk.Frame(attachments_canvas, bg=‘white’)
attachments_frame.pack(fill=tk.BOTH, expand=True)
Create the attachments report scrollbar
attachments_scrollbar = tk.Scrollbar(window, command=attachments_canvas.yview)
attachments_scrollbar.pack(fill=tk.Y, side=tk.RIGHT)
attachments_canvas.configure(yscrollcommand=attachments_scrollbar.set)
attachments_canvas.create_window((0, 0), window=attachments_frame, anchor=tk.NW)
Create the Save Attachments button
save_attachments_button = tk.Button(window, text=‘Save Attachments’, command=save_attachments, bd=1, relief=‘solid’, fg=‘red’)
save_attachments_button.pack()
Create the Create Prompt button
create_prompt_button = tk.Button(window, text=‘Create Prompt’, command=create_prompt, bd=1, relief=‘solid’, fg=‘green’)
create_prompt_button.pack()
Create the Teleport button
teleport_button = tk.Button(window, text=‘Teleport’, command=send_to_api, bd=1, relief=‘solid’, fg=‘blue’)
teleport_button.pack()
Start the GUI event loop
window.mainloop()