-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
264 lines (214 loc) · 10.9 KB
/
app.py
File metadata and controls
264 lines (214 loc) · 10.9 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import webview
import imaplib
import smtplib
import email
from email.message import EmailMessage
from email.header import decode_header
import sys
import os
class AppState:
def __init__(self):
self.email_address = None
self.password = None
self.imap_server = None
state = AppState()
FOLDER_MAPPING = {
'inbox': 'INBOX',
'starred': '"[Gmail]/Starred"',
'sent': '"[Gmail]/Sent Mail"',
'trash': '"[Gmail]/Trash"'
}
class MailApi:
def login(self, email_addr, pwd):
if not email_addr or not pwd:
return {'success': False, 'message': 'Missing credentials'}
try:
imap = imaplib.IMAP4_SSL("imap.gmail.com")
imap.login(email_addr, pwd)
state.email_address = email_addr
state.password = pwd
state.imap_server = imap
return {'success': True}
except imaplib.IMAP4.error:
return {'success': False, 'message': 'Login failed. Check credentials.'}
except Exception as e:
return {'success': False, 'message': str(e)}
def get_emails(self, folder='inbox'):
if not state.imap_server:
return {'success': False, 'message': 'Not logged in'}
try:
try:
state.imap_server.noop()
except:
state.imap_server = imaplib.IMAP4_SSL("imap.gmail.com")
state.imap_server.login(state.email_address, state.password)
imap_folder = FOLDER_MAPPING.get(folder, 'INBOX')
# Select folder
try:
status, select_data = state.imap_server.select(imap_folder)
if status != 'OK':
# Fallback to INBOX if the folder doesn't exist
state.imap_server.select('INBOX')
imap_folder = 'INBOX'
except:
state.imap_server.select('INBOX')
imap_folder = 'INBOX'
# If we selected INBOX but requested 'starred', search only FLAGGED
search_query = "ALL"
if folder == 'starred' and imap_folder == 'INBOX':
search_query = "FLAGGED"
status, messages = state.imap_server.search(None, search_query)
if status == "OK":
mail_ids = messages[0].split()
# Get the latest 20 emails
latest_ids = mail_ids[-20:]
# Fetch flagged and unseen status lists to stamp on each email
status_flagged, flagged_data = state.imap_server.search(None, "FLAGGED")
flagged_ids = flagged_data[0].split() if status_flagged == "OK" else []
status_unseen, unseen_data = state.imap_server.search(None, "UNSEEN")
unseen_ids = unseen_data[0].split() if status_unseen == "OK" else []
parsed_emails = []
for num in reversed(latest_ids):
res, data = state.imap_server.fetch(num, "(RFC822)")
for response_part in data:
if isinstance(response_part, tuple):
msg = email.message_from_bytes(response_part[1])
subject, encoding = decode_header(msg.get("Subject", ""))[0]
if isinstance(subject, bytes):
subject = subject.decode(encoding if encoding else "utf-8", errors="ignore")
from_, encoding = decode_header(msg.get("From", ""))[0]
if isinstance(from_, bytes):
from_ = from_.decode(encoding if encoding else "utf-8", errors="ignore")
date_ = msg.get("Date", "")
html_body = ""
plain_body = ""
if msg.is_multipart():
for part in msg.walk():
content_type = part.get_content_type()
if content_type == "text/html":
payload = part.get_payload(decode=True)
if payload:
html_body += payload.decode("utf-8", errors="ignore")
elif content_type == "text/plain":
payload = part.get_payload(decode=True)
if payload:
plain_body += payload.decode("utf-8", errors="ignore")
else:
payload = msg.get_payload(decode=True)
if payload:
if msg.get_content_type() == "text/html":
html_body = payload.decode("utf-8", errors="ignore")
else:
plain_body = payload.decode("utf-8", errors="ignore")
body = html_body if html_body else f"<pre style='font-family: inherit; white-space: pre-wrap;'>{plain_body}</pre>"
is_starred = (folder == 'starred') or (num in flagged_ids)
is_unread = num in unseen_ids
parsed_emails.append({
"id": num.decode(),
"from": from_,
"subject": subject if subject else "(No Subject)",
"date": date_,
"body": body,
"starred": is_starred,
"unread": is_unread
})
return {'success': True, 'emails': parsed_emails}
return {'success': True, 'emails': []}
except Exception as e:
return {'success': False, 'message': str(e)}
def send_email(self, to_addr, subj, body):
if not state.email_address:
return {'success': False, 'message': 'Not logged in'}
if not to_addr or not body:
return {'success': False, 'message': 'Recipient and Body are required.'}
try:
msg = EmailMessage()
msg.set_content(body)
msg["Subject"] = subj
msg["From"] = state.email_address
msg["To"] = to_addr
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
server.login(state.email_address, state.password)
server.send_message(msg)
return {'success': True}
except Exception as e:
return {'success': False, 'message': str(e)}
def toggle_star_email(self, email_id, folder, is_starred):
if not state.imap_server:
return {'success': False, 'message': 'Not logged in'}
if not email_id:
return {'success': False, 'message': 'Email ID is required.'}
try:
imap_folder = FOLDER_MAPPING.get(folder, 'INBOX')
state.imap_server.select(imap_folder)
flag_action = '+FLAGS' if is_starred else '-FLAGS'
state.imap_server.store(email_id.encode(), flag_action, '\\Flagged')
return {'success': True}
except Exception as e:
return {'success': False, 'message': str(e)}
def mark_as_read(self, email_id, folder, is_read):
if not state.imap_server:
return {'success': False, 'message': 'Not logged in'}
if not email_id:
return {'success': False, 'message': 'Email ID is required.'}
try:
imap_folder = FOLDER_MAPPING.get(folder, 'INBOX')
state.imap_server.select(imap_folder)
flag_action = '+FLAGS' if is_read else '-FLAGS'
state.imap_server.store(email_id.encode(), flag_action, '\\Seen')
return {'success': True}
except Exception as e:
return {'success': False, 'message': str(e)}
def delete_email(self, email_id, folder='inbox'):
if not state.imap_server:
return {'success': False, 'message': 'Not logged in'}
if not email_id:
return {'success': False, 'message': 'Email ID is required.'}
try:
imap_folder = FOLDER_MAPPING.get(folder, 'INBOX')
state.imap_server.select(imap_folder)
if folder == 'trash':
# Permanently delete from trash
state.imap_server.store(email_id.encode(), '+FLAGS', '\\Deleted')
state.imap_server.expunge()
else:
trash_folder = FOLDER_MAPPING.get('trash', '"[Gmail]/Trash"')
try:
# Move to Trash
copy_status, _ = state.imap_server.copy(email_id.encode(), trash_folder)
if copy_status == 'OK':
state.imap_server.store(email_id.encode(), '+FLAGS', '\\Deleted')
state.imap_server.expunge()
else:
# Fallback
state.imap_server.store(email_id.encode(), '+FLAGS', '\\Deleted')
state.imap_server.expunge()
except Exception:
# Fallback
state.imap_server.store(email_id.encode(), '+FLAGS', '\\Deleted')
state.imap_server.expunge()
return {'success': True}
except Exception as e:
return {'success': False, 'message': str(e)}
def logout(self):
try:
if state.imap_server:
state.imap_server.logout()
except:
pass
finally:
state.imap_server = None
state.email_address = None
state.password = None
return {'success': True}
if __name__ == '__main__':
if getattr(sys, 'frozen', False):
base_dir = sys._MEIPASS
else:
base_dir = os.path.dirname(os.path.abspath(__file__))
index_path = os.path.join(base_dir, 'templates', 'index.html')
# We replace backslashes to ensure the file:// URI works smoothly on Windows
index_uri = index_path.replace('\\', '/')
api = MailApi()
webview.create_window('Mail Client', url=f'file:///{index_uri}', js_api=api, width=1100, height=750, min_size=(800, 600))
webview.start()