Coverage for mindsdb / integrations / handlers / email_handler / email_handler.py: 0%
45 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-21 00:36 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-21 00:36 +0000
1from mindsdb.utilities import log
3from mindsdb.integrations.libs.api_handler import APIHandler
5from mindsdb.integrations.libs.response import (
6 HandlerStatusResponse as StatusResponse,
7)
8from mindsdb_sql_parser import parse_sql
10from mindsdb.integrations.handlers.email_handler.email_tables import EmailsTable
11from mindsdb.integrations.handlers.email_handler.email_client import EmailClient
12from mindsdb.integrations.handlers.email_handler.settings import EmailConnectionDetails
15logger = log.getLogger(__name__)
18class EmailHandler(APIHandler):
19 """
20 A class for handling connections and interactions with Email (send and search).
22 Parameters
23 ----------
24 name : str
25 The name of the handler
26 connection_data : EmailConnectionDetails
27 The connection details for the email server
29 see `EmailConnectionDetails` for more details and examples
31 """
33 def __init__(self, name=None, **kwargs):
34 super().__init__(name)
36 connection_data = kwargs.get("connection_data", {})
37 self.connection_data = EmailConnectionDetails(**connection_data)
38 self.kwargs = kwargs
40 self.connection = None
41 self.is_connected = False
43 emails = EmailsTable(self)
44 self._register_table('emails', emails)
46 def connect(self):
47 """Authenticate with the email servers using credentials."""
49 if self.is_connected is True:
50 return self.connection
52 try:
53 self.connection = EmailClient(self.connection_data)
54 except Exception as e:
55 logger.error(f'Error connecting to email api: {e}!')
56 raise e
58 self.is_connected = True
59 return self.connection
61 def disconnect(self):
62 """ Close any existing connections
64 Should switch self.is_connected.
65 """
66 self.is_connected = False
68 return self.connection.logout()
70 def check_connection(self) -> StatusResponse:
72 response = StatusResponse(False)
74 try:
75 self.connect()
76 response.success = True
78 except Exception as e:
79 response.error_message = f'Error connecting to Email: {e}. '
80 logger.error(response.error_message)
82 if response.success is False and self.is_connected is True:
83 self.is_connected = False
85 return response
87 def native_query(self, query: str) -> StatusResponse:
88 """Receive and process a raw query.
89 Parameters
90 ----------
91 query : str
92 query in a native format
93 Returns
94 -------
95 StatusResponse
96 Request status
97 """
98 ast = parse_sql(query)
99 return self.query(ast)