11import asyncio
22import json
33import smtplib
4+ import ssl
45import aiohttp
56import cv2
67import os
@@ -216,22 +217,30 @@ async def send(self, message: Any) -> None:
216217 # Format message body
217218 if isinstance (message , dict ):
218219 body = json .dumps (message , indent = 2 )
219- print (f"ℹ️ Message is a dictionary, converting to JSON" )
220+ logger . info (f"ℹ️ Message is a dictionary, converting to JSON" )
220221 else :
221222 body = str (message )
222- print (f"ℹ️ Message is a string, length: { len (body )} characters" )
223+ logger . info (f"ℹ️ Message is a string, length: { len (body )} characters" )
223224
224225 msg .attach (MIMEText (body , "plain" ))
225- print (f"✉️ Message prepared, connecting to SMTP server..." )
226+ logger . info (f"✉️ Message prepared, connecting to SMTP server..." )
226227
227228 # SMTP connection and sending
228- server = smtplib .SMTP (self .server , self .port , timeout = 10 )
229- print (f"🔌 Connected to SMTP server: { self .server } :{ self .port } " )
229+ logger .info (f"🔌 Connecting to SMTP server: { self .server } :{ self .port } " )
230230
231- server .starttls ()
232- logger .info ("🔒 Started TLS encryption" )
231+ # Use SMTP_SSL for port 465, regular SMTP for other ports with STARTTLS
232+ if self .port == 465 :
233+ logger .info ("🔒 Using SSL/TLS (port 465)" )
234+ context = ssl .create_default_context ()
235+ server = smtplib .SMTP_SSL (self .server , self .port , timeout = 10 , context = context )
236+ logger .info ("✅ Established SSL connection" )
237+ else :
238+ logger .info ("🔓 Using STARTTLS (port 587 or other)" )
239+ server = smtplib .SMTP (self .server , self .port , timeout = 10 )
240+ server .starttls ()
241+ logger .info ("✅ STARTTLS negotiation successful" )
233242
234- print (f"🔑 Authenticating user: { self .user } " )
243+ logger . info (f"🔑 Authenticating user: { self .user } " )
235244 server .login (self .user , self .password )
236245 logger .info ("✅ Authentication successful" )
237246
@@ -244,25 +253,25 @@ async def send(self, message: Any) -> None:
244253
245254 try :
246255 msg ["To" ] = clean_recipient
247- print (f"📤 Sending to: { clean_recipient } " )
256+ logger . info (f"📤 Sending to: { clean_recipient } " )
248257 server .send_message (msg )
249258 del msg ["To" ]
250259 success_count += 1
251- print (f"✅ Successfully sent to: { clean_recipient } " )
260+ logger . info (f"✅ Successfully sent to: { clean_recipient } " )
252261 except Exception as send_error :
253- print (f"❌ Failed to send to { clean_recipient } : { send_error } " )
262+ logger . error (f"❌ Failed to send to { clean_recipient } : { send_error } " )
254263
255264 server .quit ()
256- print (f"📬 Email sending complete. Successfully sent to { success_count } /{ len (self .recipients )} recipients" )
265+ logger . info (f"📬 Email sending complete. Successfully sent to { success_count } /{ len (self .recipients )} recipients" )
257266
258267 except smtplib .SMTPException as smtp_error :
259- print (f"❌ SMTP Error: { smtp_error } " )
260- print (f" SMTP Code: { getattr (smtp_error , 'smtp_code' , 'N/A' )} " )
261- print (f" SMTP Error: { getattr (smtp_error , 'smtp_error' , 'N/A' )} " )
268+ logger . error (f"❌ SMTP Error: { smtp_error } " )
269+ logger . error (f" SMTP Code: { getattr (smtp_error , 'smtp_code' , 'N/A' )} " )
270+ logger . error (f" SMTP Error: { getattr (smtp_error , 'smtp_error' , 'N/A' )} " )
262271 except Exception as e :
263272 import traceback
264- print (f"❌ Unexpected error: { e } " )
265- logger .info ("📝 Stack trace:" )
273+ logger . error (f"❌ Unexpected error: { e } " )
274+ logger .error ("📝 Stack trace:" )
266275 traceback .print_exc ()
267276
268277
@@ -279,13 +288,13 @@ async def send(self, message: Any) -> None:
279288 data = message if isinstance (message , dict ) else {"data" : message }
280289 async with session .post (self .uri , json = data ) as response :
281290 if response .status == 200 :
282- print (f"🌐 HTTP sent to { self .uri } " )
291+ logger . info (f"🌐 HTTP sent to { self .uri } " )
283292 else :
284- print (
293+ logger . error (
285294 f"❌ HTTP error { response .status } : { await response .text ()} "
286295 )
287296 except Exception as e :
288- print (f"❌ HTTP destination error: { e } " )
297+ logger . error (f"❌ HTTP destination error: { e } " )
289298
290299
291300class MQTTDestination (Destination ):
@@ -302,10 +311,10 @@ async def send(self, message: Any) -> None:
302311 try :
303312 # Note: Would need asyncio-mqtt library
304313 payload = json .dumps (message ) if isinstance (message , dict ) else str (message )
305- print (f"📡 MQTT sent to { self .broker } :{ self .port } /{ self .topic } " )
314+ logger . info (f"📡 MQTT sent to { self .broker } :{ self .port } /{ self .topic } " )
306315 # Implementation would use actual MQTT client
307316 except Exception as e :
308- print (f"❌ MQTT error: { e } " )
317+ logger . error (f"❌ MQTT error: { e } " )
309318
310319
311320class FileDestination (Destination ):
@@ -321,9 +330,9 @@ async def send(self, message: Any) -> None:
321330 content = json .dumps (message ) if isinstance (message , dict ) else str (message )
322331 with open (self .path , "a" ) as f :
323332 f .write (f"{ datetime .now ().isoformat ()} : { content } \n " )
324- print (f"📄 Written to { self .path } " )
333+ logger . info (f"📄 Written to { self .path } " )
325334 except Exception as e :
326- print (f"❌ File destination error: { e } " )
335+ logger . error (f"❌ File destination error: { e } " )
327336
328337
329338class LogDestination (Destination ):
@@ -351,7 +360,7 @@ async def send(self, message: Any) -> None:
351360 with open (self .log_file , "a" , encoding = "utf-8" ) as f :
352361 f .write (log_msg + "\n " )
353362 except Exception as e :
354- print (f"❌ Log file error: { e } " )
363+ logger . error (f"❌ Log file error: { e } " )
355364
356365
357366class GRPCDestination (Destination ):
@@ -364,6 +373,6 @@ async def send(self, message: Any) -> None:
364373 """Send gRPC message"""
365374 try :
366375 # Implementation would depend on specific gRPC service
367- print (f"🔗 gRPC sent to { self .uri } " )
376+ logger . info (f"🔗 gRPC sent to { self .uri } " )
368377 except Exception as e :
369- print (f"❌ gRPC destination error: { e } " )
378+ logger . error (f"❌ gRPC destination error: { e } " )
0 commit comments