Repository navigation
Expand file tree
/
Copy pathmail_client.py
More file actions
379 lines (327 loc) Β· 13.4 KB
/
Copy pathmail_client.py
File metadata and controls
379 lines (327 loc) Β· 13.4 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/env python3
"""
mail_client.py β Hybrid IMAP + AppleScript mail client for macOS Mail
Provides unified interface for fetching, managing, and categorizing emails from multiple accounts
"""
import imaplib
import email
from email.message import Message as EmailMessage
import json
import subprocess
import logging
from typing import List, Dict, Optional, Tuple
from dataclasses import dataclass
from datetime import datetime, timedelta
from pathlib import Path
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
@dataclass
class MailMessage:
"""Unified mail message representation"""
id: str
sender: str
subject: str
body: str
date: datetime
is_read: bool
is_flagged: bool
mailbox: str
message_id: str
account: str
raw_message: Optional[bytes] = None
class IMAPMailClient:
"""IMAP-based mail client (primary method)"""
def __init__(self, imap_host: str, imap_port: int, username: str, password: str, account_name: str = "default"):
self.imap_host = imap_host
self.imap_port = imap_port
self.username = username
self.password = password
self.account_name = account_name
self.imap = None
self.is_connected = False
def connect(self) -> bool:
"""Establish IMAP connection"""
try:
self.imap = imaplib.IMAP4_SSL(self.imap_host, self.imap_port)
self.imap.login(self.username, self.password)
self.is_connected = True
logger.info(f"β
Connected to {self.account_name} via IMAP ({self.imap_host})")
return True
except Exception as e:
logger.error(f"β IMAP connection failed for {self.account_name}: {str(e)}")
self.is_connected = False
return False
def disconnect(self):
"""Close IMAP connection"""
if self.imap:
try:
self.imap.close()
self.imap.logout()
self.is_connected = False
except:
pass
def list_mailboxes(self) -> List[str]:
"""List all available mailboxes"""
if not self.is_connected:
return []
try:
_, mailboxes = self.imap.list()
return [mb.decode().split('"')[-2] for mb in mailboxes]
except Exception as e:
logger.error(f"Failed to list mailboxes: {e}")
return []
def fetch_emails(self, mailbox: str = "INBOX", limit: int = 50, unread_only: bool = False) -> List[MailMessage]:
"""Fetch emails from specified mailbox"""
if not self.is_connected:
return []
messages = []
try:
self.imap.select(mailbox)
# Search for emails
if unread_only:
_, email_ids = self.imap.search(None, "UNSEEN")
else:
_, email_ids = self.imap.search(None, "ALL")
email_list = email_ids[0].split()[-limit:] if email_ids[0] else []
for email_id in email_list:
try:
_, raw_message = self.imap.fetch(email_id, "(RFC822)")
msg = email.message_from_bytes(raw_message[0][1])
# Parse message details
sender = msg.get("From", "Unknown")
subject = msg.get("Subject", "(No Subject)")
date_str = msg.get("Date", "")
message_id = msg.get("Message-ID", str(email_id))
# Decode subject if needed
if isinstance(subject, email.header.Header):
subject = str(subject)
# Extract body
body = self._extract_body(msg)
# Parse date
try:
from email.utils import parsedate_to_datetime
msg_date = parsedate_to_datetime(date_str)
except:
msg_date = datetime.now()
# Check flags
_, flags = self.imap.fetch(email_id, "(FLAGS)")
flag_str = flags[0].decode()
is_read = "\\Seen" in flag_str
is_flagged = "\\Flagged" in flag_str
mail_msg = MailMessage(
id=email_id.decode(),
sender=sender,
subject=subject,
body=body[:2000], # Limit body length
date=msg_date,
is_read=is_read,
is_flagged=is_flagged,
mailbox=mailbox,
message_id=message_id,
account=self.account_name,
raw_message=raw_message[0][1]
)
messages.append(mail_msg)
except Exception as e:
logger.warning(f"Failed to parse email {email_id}: {e}")
continue
return messages
except Exception as e:
logger.error(f"Failed to fetch emails from {mailbox}: {e}")
return []
def mark_read(self, email_id: str) -> bool:
"""Mark email as read"""
if not self.is_connected:
return False
try:
self.imap.store(email_id, "+FLAGS", "\\Seen")
return True
except Exception as e:
logger.error(f"Failed to mark email as read: {e}")
return False
def move_to_folder(self, email_id: str, folder: str) -> bool:
"""Move email to another folder"""
if not self.is_connected:
return False
try:
self.imap.copy(email_id, folder)
self.imap.store(email_id, "+FLAGS", "\\Deleted")
self.imap.expunge()
return True
except Exception as e:
logger.error(f"Failed to move email: {e}")
return False
def flag_email(self, email_id: str, flagged: bool = True) -> bool:
"""Flag or unflag an email"""
if not self.is_connected:
return False
try:
if flagged:
self.imap.store(email_id, "+FLAGS", "\\Flagged")
else:
self.imap.store(email_id, "-FLAGS", "\\Flagged")
return True
except Exception as e:
logger.error(f"Failed to flag email: {e}")
return False
@staticmethod
def _extract_body(msg: email.message.Message) -> str:
"""Extract plain text body from email"""
body = ""
if msg.is_multipart():
for part in msg.walk():
if part.get_content_type() == "text/plain":
body = part.get_payload(decode=True).decode(errors="ignore")
break
else:
body = msg.get_payload(decode=True).decode(errors="ignore")
return body.strip()
class AppleScriptMailClient:
"""AppleScript-based fallback for macOS Mail.app"""
def __init__(self, account_name: str = "default"):
self.account_name = account_name
self.is_available = self._check_availability()
@staticmethod
def _check_availability() -> bool:
"""Check if AppleScript/osascript is available"""
try:
result = subprocess.run(["which", "osascript"], capture_output=True)
return result.returncode == 0
except:
return False
def run_script(self, script: str) -> Tuple[bool, str]:
"""Execute AppleScript and return result"""
if not self.is_available:
return False, "AppleScript not available"
try:
result = subprocess.run(
["osascript", "-e", script],
capture_output=True,
text=True,
timeout=10
)
if result.returncode == 0:
return True, result.stdout.strip()
else:
return False, result.stderr.strip()
except Exception as e:
return False, str(e)
def get_accounts(self) -> List[str]:
"""Get list of Mail.app accounts via AppleScript"""
script = """
tell application "Mail"
set accountList to {}
repeat with acc in accounts
set end of accountList to name of acc
end repeat
return accountList as string
end tell
"""
success, result = self.run_script(script)
if success:
return [acc.strip() for acc in result.split(",") if acc.strip()]
return []
def get_unread_count(self, account: str = None) -> int:
"""Get unread email count"""
if account:
script = f'tell application "Mail" to get unread count of account "{account}"'
else:
script = 'tell application "Mail" to get unread count'
success, result = self.run_script(script)
if success:
try:
return int(result)
except:
return 0
return 0
def mark_read_applescript(self, message_id: str, account: str) -> bool:
"""Mark email as read using AppleScript"""
script = f"""
tell application "Mail"
set theMessage to message id "{message_id}" of account "{account}"
set read status of theMessage to true
end tell
"""
success, _ = self.run_script(script)
return success
def move_to_trash_applescript(self, message_id: str, account: str) -> bool:
"""Move email to trash using AppleScript"""
script = f"""
tell application "Mail"
set theMessage to message id "{message_id}" of account "{account}"
delete theMessage
end tell
"""
success, _ = self.run_script(script)
return success
class HybridMailClient:
"""Hybrid IMAP + AppleScript mail client with failover"""
def __init__(self, imap_config: Dict):
"""
Initialize hybrid client
imap_config: {
'imap_host': 'imap.gmail.com',
'imap_port': 993,
'username': 'user@gmail.com',
'password': 'app-password',
'account_name': 'Gmail'
}
"""
self.imap_client = IMAPMailClient(**imap_config)
self.applescript_client = AppleScriptMailClient(imap_config.get('account_name', 'default'))
self.config = imap_config
def connect(self) -> bool:
"""Connect with IMAP primary, AppleScript fallback"""
if self.imap_client.connect():
logger.info(f"β
Using IMAP for {self.config.get('account_name')}")
return True
elif self.applescript_client.is_available:
logger.warning(f"β οΈ IMAP failed, falling back to AppleScript for {self.config.get('account_name')}")
return True
else:
logger.error(f"β No connection method available for {self.config.get('account_name')}")
return False
def disconnect(self):
"""Disconnect IMAP client"""
self.imap_client.disconnect()
def fetch_emails(self, mailbox: str = "INBOX", limit: int = 50) -> List[MailMessage]:
"""Fetch emails using IMAP (with AppleScript fallback if needed)"""
if self.imap_client.is_connected:
return self.imap_client.fetch_emails(mailbox, limit)
else:
logger.warning("IMAP not connected, cannot fetch emails via AppleScript yet")
return []
def mark_read(self, email: MailMessage) -> bool:
"""Mark email as read"""
if self.imap_client.is_connected:
return self.imap_client.mark_read(email.id)
elif self.applescript_client.is_available:
return self.applescript_client.mark_read_applescript(email.message_id, email.account)
return False
def move_to_spam(self, email: MailMessage) -> bool:
"""Move email to spam/junk folder"""
spam_folders = ["[Gmail]/Spam", "Junk", "Spam", "Junk Mail"]
if self.imap_client.is_connected:
for folder in spam_folders:
if self.imap_client.move_to_folder(email.id, folder):
return True
if self.applescript_client.is_available:
return self.applescript_client.move_to_trash_applescript(email.message_id, email.account)
return False
def archive_email(self, email: MailMessage) -> bool:
"""Archive email (move to appropriate folder)"""
if self.imap_client.is_connected:
# Try common archive folder names
archive_folders = ["[Gmail]/All Mail", "Archive", "All Mail"]
for folder in archive_folders:
if self.imap_client.move_to_folder(email.id, folder):
return True
return False
def flag_email(self, email: MailMessage, flagged: bool = True) -> bool:
"""Flag or unflag an email"""
if self.imap_client.is_connected:
return self.imap_client.flag_email(email.id, flagged)
return False
def create_mail_client(config: Dict) -> HybridMailClient:
"""Factory function to create a configured mail client"""
return HybridMailClient(config)