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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479import os
import base64
import datetime
import json
from pathlib import Path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
class GmailService:
def __init__(self, credentials_file, token_file, scopes):
"""Initialize Gmail service with OAuth credentials"""
self.credentials_file = credentials_file
self.token_file = token_file
self.scopes = scopes
self.service = self._authenticate()
def _authenticate(self):
"""Authenticate with Gmail API"""
creds = None
# Check if token.json exists
if os.path.exists(self.token_file):
creds = Credentials.from_authorized_user_info(
json.loads(open(self.token_file).read()),
self.scopes
)
# If credentials don't exist or are invalid, get new ones
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
self.credentials_file, self.scopes
)
creds = flow.run_local_server(port=0)
# Save the updated credentials
with open(self.token_file, 'w') as token:
token.write(creds.to_json())
return build('gmail', 'v1', credentials=creds)
def search_emails(self, query, max_results=500):
"""Search for emails matching the query string
Args:
query (str): Gmail search query
max_results (int): Maximum number of results to return
Returns:
list: List of message dictionaries with id and threadId
"""
messages = []
next_page_token = None
# Keep track of how many emails we've fetched
total_fetched = 0
fetch_count = 0
print(f"Fetching emails (up to {max_results})...")
while total_fetched < max_results:
# Calculate how many results to request in this page
# We can request at most 500 per page in Gmail API
page_size = min(500, max_results - total_fetched)
# Request this page of results
request = self.service.users().messages().list(
userId='me',
q=query,
maxResults=page_size,
pageToken=next_page_token
)
result = request.execute()
# Add the messages from this page to our list
page_messages = result.get('messages', [])
messages.extend(page_messages)
total_fetched += len(page_messages)
fetch_count += 1
# Get the next page token
next_page_token = result.get('nextPageToken')
# If there's no next page token, we've reached the end
if not next_page_token:
break
# If we've already fetched the max number of results, stop
if total_fetched >= max_results:
break
# Show progress for each page after the first
print(f"Fetched {total_fetched} emails so far... (page {fetch_count})")
if total_fetched >= 1000:
print(f"Completed fetching {total_fetched} emails ({fetch_count} pages)")
return messages
def get_email(self, msg_id):
"""Get full email details by ID
Args:
msg_id (str): Email ID from Gmail API
Returns:
dict: Full email message
"""
try:
message = self.service.users().messages().get(
userId='me', id=msg_id, format='full'
).execute()
return message
except Exception as e:
print(f"Error getting email {msg_id}")
# Return a minimal message that won't cause errors
return {'id': msg_id, 'payload': {'headers': [], 'body': {'data': ''}}}
def get_email_content(self, message):
"""Extract email content (subject, sender, body, date) from a message
Args:
message (dict): Full email message from Gmail API
Returns:
dict: Extracted email content with id, subject, sender, date, body_text, body_html
"""
try:
# Check if message has required fields
if 'payload' not in message or 'headers' not in message['payload']:
return {
'id': message.get('id', 'unknown'),
'thread_id': message.get('threadId', ''),
'subject': '',
'sender': '',
'date': datetime.datetime.now(),
'body_html': '',
'body_text': '',
'headers': []
}
headers = message['payload']['headers']
parts = self._get_parts(message['payload'])
# Extract headers
subject = next((h['value'] for h in headers if h['name'] == 'Subject'), '')
sender = next((h['value'] for h in headers if h['name'] == 'From'), '')
date_str = next((h['value'] for h in headers if h['name'] == 'Date'), '')
# Extract body content
body_html = None
body_text = None
for part in parts:
if part['mimeType'] == 'text/html' and 'body' in part and 'data' in part['body']:
body_html = part['body']['data']
elif part['mimeType'] == 'text/plain' and 'body' in part and 'data' in part['body']:
body_text = part['body']['data']
# Decode body content
if body_html:
try:
body_html = base64.urlsafe_b64decode(body_html).decode('utf-8')
except Exception:
body_html = ''
if body_text:
try:
body_text = base64.urlsafe_b64decode(body_text).decode('utf-8')
except Exception:
body_text = ''
# Parse date
date = None
if date_str:
try:
# This is a simplified approach - proper date parsing might need more work
date = datetime.datetime.strptime(date_str[:25], '%a, %d %b %Y %H:%M:%S')
except ValueError:
# Fall back to current date if parsing fails
date = datetime.datetime.now()
else:
date = datetime.datetime.now()
return {
'id': message['id'],
'thread_id': message.get('threadId', ''),
'subject': subject,
'sender': sender,
'date': date,
'body_html': body_html or '',
'body_text': body_text or '',
'headers': headers
}
except Exception:
# Return a minimal content that won't cause errors
return {
'id': message.get('id', 'unknown'),
'thread_id': message.get('threadId', ''),
'subject': '',
'sender': '',
'date': datetime.datetime.now(),
'body_html': '',
'body_text': '',
'headers': []
}
def _get_parts(self, payload):
"""Recursively extract all parts from message payload"""
parts = []
# If this part has a body
if 'body' in payload and 'data' in payload['body']:
parts.append(payload)
# If this part has sub-parts
if 'parts' in payload:
for part in payload['parts']:
parts.extend(self._get_parts(part))
return parts
def find_matching_emails(self, rules, processed_emails=None, ignored_emails=None, months_back=1, debug=False):
"""Search for emails matching the rules
Args:
rules (list): List of rules to match against
processed_emails (list, optional): List of already processed email IDs
ignored_emails (list, optional): List of ignored email IDs
months_back (int, optional): Number of months to look back
debug (bool, optional): Enable additional debug output
Returns:
list: Matched emails with their matching rule
"""
if processed_emails is None:
processed_emails = []
if ignored_emails is None:
ignored_emails = []
# Get emails from the last N months
start_date = (datetime.datetime.now() - datetime.timedelta(days=30 * months_back)).strftime('%Y/%m/%d')
matching_emails = []
# Process each rule with its own targeted search query
for rule in rules:
print(f"\nChecking rule: sender='{rule.get('sender', 'any')}', subject='{rule.get('subject', 'any')}'")
# Show body_contains criteria if present
if rule.get('body_contains'):
if isinstance(rule['body_contains'], list):
body_terms = "', '".join(rule['body_contains'])
print(f"Body must contain ALL: '{body_terms}'")
else:
print(f"Body must contain: '{rule['body_contains']}'")
# Build a specific query for each rule
query_parts = [f'after:{start_date}']
if rule.get('sender'):
query_parts.append(f'from:{rule["sender"]}')
# For subject, don't use subject: prefix as it's too restrictive
# Instead, just search for the term in all email content
# if rule.get('subject'):
# query_parts.append(f'subject:"{rule["subject"]}"')
query = ' '.join(query_parts)
print(f"Search query: {query}")
try:
# Search using the rule-specific query
messages = self.search_emails(query, max_results=1000)
if not messages:
print("No matching emails found for this rule.")
continue
print(f"Found {len(messages)} potential matches for this rule. Processing...")
# Print first few subjects if debugging
if debug and messages:
print("First few matching emails:")
for i, msg in enumerate(messages[:5]):
try:
msg_data = self.get_email(msg['id'])
content = self.get_email_content(msg_data)
print(f" {i+1}. Subject: '{content.get('subject', 'No subject')}' from {content.get('sender', 'unknown')}")
except Exception as e:
print(f" {i+1}. Error getting details: {str(e)}")
rule_matches = 0
for message in messages:
message_id = message['id']
# Skip if already processed or ignored
if message_id in processed_emails or message_id in ignored_emails:
continue
# Get full message details
message_data = self.get_email(message_id)
# Extract content for matching
email_content = self.get_email_content(message_data)
# We've already filtered by sender in the API query,
# but we need to check subject and body_contains manually
matches = True
# Check subject if specified in the rule
if rule.get('subject'):
subject = email_content.get('subject', '')
if not subject or rule['subject'] not in subject:
matches = False
if debug:
print(f"Subject mismatch: '{rule['subject']}' not found in '{subject}'")
continue
elif debug:
print(f"Subject match: '{rule['subject']}' found in '{subject}'")
# Check body_contains criteria
if matches and rule.get('body_contains'):
body_text = email_content.get('body_text', '')
body_html = email_content.get('body_html', '')
# Convert single string to list for consistent handling
required_terms = rule['body_contains']
if isinstance(required_terms, str):
required_terms = [required_terms]
# Check if all terms are in either body_text or body_html
for term in required_terms:
text_match = body_text and term in body_text
html_match = body_html and term in body_html
if not (text_match or html_match):
matches = False
if debug:
print(f"Body content mismatch: '{term}' not found in email body")
# Print a more substantial preview of the body content
print(f"Email subject: {email_content.get('subject', 'No subject')}")
if body_text:
print(f"Text body preview (first 200 chars): {body_text[:200].replace('\n', ' ')}...")
if body_html:
html_preview = body_html.replace('\n', ' ').replace('\r', '')
html_preview = ' '.join(html_preview.split()) # Normalize whitespace
print(f"HTML body preview (first 200 chars): {html_preview[:200]}...")
break
if matches:
print(f">>> MATCH FOUND: {email_content['subject']} from {email_content['sender']}")
rule_matches += 1
matching_emails.append({
'email': email_content,
'rule': rule
})
print(f"Found {rule_matches} matching emails for this rule.")
except Exception as e:
print(f"Error processing rule: {str(e)}")
# Sort matching emails by date (newest first)
matching_emails.sort(key=lambda x: x['email'].get('date', datetime.datetime.now()), reverse=True)
return matching_emails
def _email_matches_rule(self, email, rule, debug=False):
"""Check if an email matches a rule
Args:
email (dict): Email content with subject, sender, body_text, body_html
rule (dict): Rule with sender, subject, and body_contains criteria
debug (bool, optional): Enable additional debug output
Returns:
bool: True if the email matches the rule
"""
# Debug output tracking
if not hasattr(self, 'debug_count'):
self.debug_count = 0
should_debug = debug and self.debug_count < 15
debug_prefix = f"[DEBUG {self.debug_count}]" if should_debug else ""
# Always show debug info for emails with specific senders we're looking for if debugging is on
if debug and rule.get('sender') and rule['sender'] in email.get('sender', ''):
should_debug = True
debug_prefix = f"[SENDER-MATCH]"
if should_debug:
self.debug_count += 1
email_snippet = f"Email: '{email.get('subject', '')}' from '{email.get('sender', '')}'"
rule_desc = f"Rule: sender='{rule.get('sender', 'any')}', subject='{rule.get('subject', 'any')}'"
print(f"{debug_prefix} Checking {email_snippet} against {rule_desc}")
# Check sender
if rule.get('sender'):
sender = email.get('sender', '')
if not sender or rule['sender'] not in sender:
if should_debug:
print(f"{debug_prefix} Sender mismatch: '{rule['sender']}' not in '{sender}'")
return False
elif should_debug:
print(f"{debug_prefix} Sender match: '{rule['sender']}' found in '{sender}'")
# Check subject
if rule.get('subject'):
subject = email.get('subject', '')
if not subject or rule['subject'] not in subject:
if should_debug:
print(f"{debug_prefix} Subject mismatch: '{rule['subject']}' not in '{subject}'")
return False
elif should_debug:
print(f"{debug_prefix} Subject match: '{rule['subject']}' found in '{subject}'")
# Check body content
if rule.get('body_contains'):
body_text = email.get('body_text', '')
body_html = email.get('body_html', '')
# Convert single string to list for consistent handling
required_terms = rule['body_contains']
if isinstance(required_terms, str):
required_terms = [required_terms]
# Check if all terms are in either body_text or body_html
for term in required_terms:
text_match = body_text and term in body_text
html_match = body_html and term in body_html
if not (text_match or html_match):
if should_debug:
print(f"{debug_prefix} Body content mismatch: '{term}' not found in email body")
# Show first 100 chars of body for debugging
body_preview = (body_text or body_html or "")[:100].replace("\n", " ") + "..."
print(f"{debug_prefix} Body preview: {body_preview}")
return False
elif should_debug:
print(f"{debug_prefix} Body content match: '{term}' found in email body")
# If we made it here, all terms matched
if should_debug:
print(f"{debug_prefix} ALL CRITERIA MATCHED - THIS IS A MATCH!")
return True
# If we get here and there was no body_contains check,
# the email matches based on sender and subject
if should_debug:
print(f"{debug_prefix} No body_contains criteria, matching based on sender/subject only - THIS IS A MATCH!")
return True
def _get_header_value(self, email, header_name):
"""Extract a header value from an email
Args:
email (dict): Email details
header_name (str): Name of the header to extract
Returns:
str: Header value or empty string if not found
"""
if 'headers' not in email:
return ""
for header in email['headers']:
if header['name'].lower() == header_name.lower():
return header['value']
return ""