lots of configuration progress.
This commit is contained in:
@@ -251,6 +251,102 @@ class IMAPService:
|
||||
self.connection = None
|
||||
return []
|
||||
|
||||
def get_folder_email_uids(self, folder_name: str) -> List[str]:
|
||||
"""Get the list of email UIDs in 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 UIDs
|
||||
resp_code, content = self.connection.search(None, 'ALL')
|
||||
if resp_code != 'OK':
|
||||
return []
|
||||
|
||||
# Extract UIDs
|
||||
email_uids = content[0].split()
|
||||
uid_list = [uid.decode('utf-8') for uid in email_uids]
|
||||
|
||||
# Close folder and logout
|
||||
self.connection.close()
|
||||
self.connection.logout()
|
||||
self.connection = None
|
||||
|
||||
return uid_list
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"Error getting email UIDs for folder {folder_name}: {str(e)}")
|
||||
if self.connection:
|
||||
try:
|
||||
self.connection.logout()
|
||||
except:
|
||||
pass
|
||||
self.connection = None
|
||||
return []
|
||||
|
||||
def get_email_headers(self, folder_name: str, email_uid: str) -> Dict[str, str]:
|
||||
"""Get email headers for a specific email UID."""
|
||||
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 {}
|
||||
|
||||
# Fetch email headers
|
||||
resp_code, content = self.connection.fetch(email_uid, '(RFC822.HEADER)')
|
||||
if resp_code != 'OK':
|
||||
return {}
|
||||
|
||||
# Parse the email headers
|
||||
raw_email = content[0][1]
|
||||
import email
|
||||
msg = email.message_from_bytes(raw_email)
|
||||
|
||||
# Extract headers
|
||||
headers = {
|
||||
'subject': msg.get('Subject', 'No Subject'),
|
||||
'date': msg.get('Date', ''),
|
||||
'from': msg.get('From', ''),
|
||||
'to': msg.get('To', ''),
|
||||
'message_id': msg.get('Message-ID', '')
|
||||
}
|
||||
|
||||
# Close folder and logout
|
||||
self.connection.close()
|
||||
self.connection.logout()
|
||||
self.connection = None
|
||||
|
||||
return headers
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"Error getting email headers for UID {email_uid} in 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:
|
||||
|
||||
Reference in New Issue
Block a user