starts work on recent emails.

This commit is contained in:
Bryce
2025-08-05 12:37:36 -07:00
parent 2335c4ceca
commit 27fc2e29a1
7 changed files with 209 additions and 2 deletions

View File

@@ -3,6 +3,7 @@ import ssl
import logging
from typing import List, Dict, Optional, Tuple
from email.header import decode_header
from email.utils import parsedate_to_datetime
from app.models import db, Folder, User
from app import create_app
@@ -173,6 +174,83 @@ class IMAPService:
self.connection = None
return 0
def get_recent_emails(self, folder_name: str, limit: int = 3) -> List[Dict[str, any]]:
"""Get the most recent email subjects and dates from a specific folder."""
try:
# Connect to IMAP server
self._connect()
# Login
self.connection.login(
self.config.get('username', ''),
self.config.get('password', '')
)
# Select the folder
resp_code, content = self.connection.select(folder_name)
if resp_code != 'OK':
return []
# Get email IDs (most recent first)
resp_code, content = self.connection.search(None, 'ALL')
if resp_code != 'OK':
return []
# Get the most recent emails (limit to 3)
email_ids = content[0].split()
recent_email_ids = email_ids[:limit] if len(email_ids) >= limit else email_ids
recent_emails = []
for email_id in reversed(recent_email_ids): # Process from newest to oldest
# Fetch the email headers
resp_code, content = self.connection.fetch(email_id, '(RFC822.HEADER)')
if resp_code != 'OK':
continue
# Parse the email headers
raw_email = content[0][1]
import email
msg = email.message_from_bytes(raw_email)
# Extract subject and date
subject = msg.get('Subject', 'No Subject')
date_str = msg.get('Date', '')
# Decode the subject if needed
try:
decoded_parts = decode_header(subject)
subject = ''.join([str(part, encoding or 'utf-8') if isinstance(part, bytes) else part for part, encoding in decoded_parts])
except Exception:
pass # If decoding fails, use the original subject
# Parse date if available
try:
email_date = parsedate_to_datetime(date_str) if date_str else None
except Exception:
email_date = None
recent_emails.append({
'subject': subject,
'date': email_date.isoformat() if email_date else None
})
# Close folder and logout
self.connection.close()
self.connection.logout()
self.connection = None
return recent_emails
except Exception as e:
logging.error(f"Error getting recent emails for folder {folder_name}: {str(e)}")
if self.connection:
try:
self.connection.logout()
except:
pass
self.connection = None
return []
def sync_folders(self) -> Tuple[bool, str]:
"""Sync IMAP folders with local database."""
try:
@@ -215,11 +293,15 @@ class IMAPService:
db.session.add(new_folder)
synced_count += 1
else:
# Update existing folder with email counts
# Update existing folder with email counts and recent emails
# Get the total count of emails in this folder
total_count = self.get_folder_email_count(folder_name)
existing_folder.total_count = total_count
existing_folder.pending_count = 0 # Initially set to 0
# Get the most recent emails for this folder
recent_emails = self.get_recent_emails(folder_name, 3)
existing_folder.recent_emails = recent_emails
db.session.commit()
return True, f"Successfully synced {synced_count} folders"