supports syncing of folders.
This commit is contained in:
@@ -12,20 +12,29 @@ class IMAPService:
|
||||
self.config = user.imap_config or {}
|
||||
self.connection = None
|
||||
|
||||
def _connect(self):
|
||||
"""Create an IMAP connection based on configuration."""
|
||||
server = self.config.get('server', 'imap.gmail.com')
|
||||
port = self.config.get('port', 993)
|
||||
use_ssl = self.config.get('use_ssl', True)
|
||||
|
||||
if use_ssl:
|
||||
# Create SSL context
|
||||
context = ssl.create_default_context()
|
||||
# Connect using SSL
|
||||
self.connection = imaplib.IMAP4_SSL(server, port)
|
||||
else:
|
||||
# Connect without SSL
|
||||
self.connection = imaplib.IMAP4(server, port)
|
||||
|
||||
def test_connection(self) -> Tuple[bool, str]:
|
||||
"""Test IMAP connection with current configuration."""
|
||||
try:
|
||||
if not self.config:
|
||||
return False, "No IMAP configuration found"
|
||||
|
||||
# Create SSL context
|
||||
context = ssl.create_default_context()
|
||||
|
||||
# Connect to IMAP server
|
||||
self.connection = imaplib.IMAP4_SSL(
|
||||
self.config.get('server', 'imap.gmail.com'),
|
||||
self.config.get('port', 993)
|
||||
)
|
||||
self._connect()
|
||||
|
||||
# Login
|
||||
self.connection.login(
|
||||
@@ -34,17 +43,27 @@ class IMAPService:
|
||||
)
|
||||
|
||||
# Select inbox to verify connection
|
||||
self.connection.select('INBOX')
|
||||
resp_code, content = self.connection.select('INBOX')
|
||||
print(resp_code, content)
|
||||
|
||||
# Close connection
|
||||
# Close the folder, not the connection
|
||||
self.connection.close()
|
||||
|
||||
# Logout
|
||||
self.connection.logout()
|
||||
self.connection = None
|
||||
|
||||
return True, "Connection successful"
|
||||
|
||||
except imaplib.IMAP4.error as e:
|
||||
print(e)
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return False, f"IMAP connection error: {str(e)}"
|
||||
except Exception as e:
|
||||
print(e)
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return False, f"Connection error: {str(e)}"
|
||||
finally:
|
||||
if self.connection:
|
||||
@@ -59,14 +78,8 @@ class IMAPService:
|
||||
if not self.config:
|
||||
return []
|
||||
|
||||
# Create SSL context
|
||||
context = ssl.create_default_context()
|
||||
|
||||
# Connect to IMAP server
|
||||
self.connection = imaplib.IMAP4_SSL(
|
||||
self.config.get('server', 'imap.gmail.com'),
|
||||
self.config.get('port', 993)
|
||||
)
|
||||
self._connect()
|
||||
|
||||
# Login
|
||||
self.connection.login(
|
||||
@@ -79,6 +92,8 @@ class IMAPService:
|
||||
|
||||
if status != 'OK':
|
||||
return []
|
||||
|
||||
print(folder_data)
|
||||
|
||||
folders = []
|
||||
for folder_item in folder_data:
|
||||
@@ -101,8 +116,6 @@ class IMAPService:
|
||||
'full_path': folder_name
|
||||
})
|
||||
|
||||
# Close connection
|
||||
self.connection.close()
|
||||
self.connection.logout()
|
||||
|
||||
return folders
|
||||
@@ -163,5 +176,8 @@ class IMAPService:
|
||||
return True, f"Successfully synced {synced_count} folders"
|
||||
|
||||
except Exception as e:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
print(e)
|
||||
db.session.rollback()
|
||||
return False, f"Sync error: {str(e)}"
|
||||
return False, f"Sync error: {str(e)}"
|
||||
|
||||
Reference in New Issue
Block a user