-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicCode_WithGUI
85 lines (71 loc) · 2.54 KB
/
BasicCode_WithGUI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import imaplib
import email
from email.header import decode_header
import tkinter as tk
def fetch_emails(username, password):
# Connect to Gmail's IMAP server
imap = imaplib.IMAP4_SSL("imap.gmail.com")
imap.login(username, password)
imap.select("inbox")
# Search for all emails in the inbox
result, data = imap.search(None, "ALL")
email_ids = data[0].split()
# Fetch email bodies for each email
for email_id in email_ids:
result, data = imap.fetch(email_id, "(RFC822)")
raw_email = data[0][1]
msg = email.message_from_bytes(raw_email)
# Get subject
subject_header = msg["Subject"]
if subject_header is not None:
subject = decode_header(subject_header)[0][0]
if subject is None:
subject = "No subject"
else:
subject = subject.decode() if isinstance(subject, bytes) else subject
else:
subject = "No subject"
# Get sender
from_header = msg.get("From")
if from_header is not None:
from_ = decode_header(from_header)[0][0]
if from_ is None:
from_ = "Unknown sender"
else:
from_ = from_.decode() if isinstance(from_, bytes) else from_
else:
from_ = "Unknown sender"
# Get body
payload = msg.get_payload(decode=True)
body = payload.decode() if payload else "No body found"
print("Subject:", subject)
print("From:", from_)
print("Body:", body)
print("=" * 50)
# Close the connection
imap.close()
imap.logout()
def ok_clicked(email_entry, password_entry):
email = email_entry.get()
password = password_entry.get()
window.destroy() # Close the Tkinter window
fetch_emails(email, password)
# Create a Tkinter window
window = tk.Tk()
window.title("Email Login")
# Create labels and entry fields for email and password
email_label = tk.Label(window, text="Email:")
email_label.grid(row=0, column=0, padx=10, pady=5)
email_entry = tk.Entry(window)
email_entry.grid(row=0, column=1, padx=10, pady=5)
password_label = tk.Label(window, text="Password:")
password_label.grid(row=1, column=0, padx=10, pady=5)
password_entry = tk.Entry(window, show="*")
password_entry.grid(row=1, column=1, padx=10, pady=5)
# Create an OK button
ok_button = tk.Button(
window, text="OK", command=lambda: ok_clicked(email_entry, password_entry)
)
ok_button.grid(row=2, columnspan=2, padx=10, pady=10)
# Run the Tkinter event loop
window.mainloop()