|
11 | 11 | from email.mime.text import MIMEText |
12 | 12 | from email.mime.multipart import MIMEMultipart |
13 | 13 | from datetime import datetime |
| 14 | +from dialogchain.utils.logger import setup_logger |
| 15 | +logger = setup_logger(__name__) |
| 16 | + |
14 | 17 |
|
15 | 18 |
|
16 | 19 | class Source(ABC): |
@@ -58,7 +61,7 @@ async def receive(self) -> AsyncIterator[Dict[str, Any]]: |
58 | 61 | while True: |
59 | 62 | ret, frame = cap.read() |
60 | 63 | if not ret: |
61 | | - print("📹 Lost connection to camera") |
| 64 | + logger.info("📹 Lost connection to camera") |
62 | 65 | break |
63 | 66 |
|
64 | 67 | # Skip frames for performance |
@@ -194,34 +197,73 @@ def __init__(self, uri: str): |
194 | 197 | self.recipients = self.recipients[0].split(",") |
195 | 198 |
|
196 | 199 | async def send(self, message: Any) -> None: |
197 | | - """Send email""" |
| 200 | + """Send email with enhanced logging""" |
198 | 201 | try: |
| 202 | + print(f"🔧 Preparing email with server: {self.server}:{self.port}") |
| 203 | + print(f"🔧 Authenticating as user: {self.user}") |
| 204 | + |
199 | 205 | msg = MIMEMultipart() |
200 | 206 | msg["From"] = self.user |
201 | | - msg["Subject"] = "Camel Router Alert" |
| 207 | + |
| 208 | + # Extract subject from message if it's a dict and has a subject field |
| 209 | + if isinstance(message, dict) and 'subject' in message: |
| 210 | + msg["Subject"] = message.get('subject', 'Camel Router Alert') |
| 211 | + print(f"📨 Using subject from message: {msg['Subject']}") |
| 212 | + else: |
| 213 | + msg["Subject"] = "Camel Router Alert" |
| 214 | + logger.info("ℹ️ Using default subject") |
202 | 215 |
|
203 | | - # Format message |
| 216 | + # Format message body |
204 | 217 | if isinstance(message, dict): |
205 | 218 | body = json.dumps(message, indent=2) |
| 219 | + print(f"ℹ️ Message is a dictionary, converting to JSON") |
206 | 220 | else: |
207 | 221 | body = str(message) |
| 222 | + print(f"ℹ️ Message is a string, length: {len(body)} characters") |
208 | 223 |
|
209 | 224 | msg.attach(MIMEText(body, "plain")) |
| 225 | + print(f"✉️ Message prepared, connecting to SMTP server...") |
210 | 226 |
|
211 | | - server = smtplib.SMTP(self.server, self.port) |
| 227 | + # 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}") |
| 230 | + |
212 | 231 | server.starttls() |
| 232 | + logger.info("🔒 Started TLS encryption") |
| 233 | + |
| 234 | + print(f"🔑 Authenticating user: {self.user}") |
213 | 235 | server.login(self.user, self.password) |
| 236 | + logger.info("✅ Authentication successful") |
214 | 237 |
|
| 238 | + success_count = 0 |
215 | 239 | for recipient in self.recipients: |
216 | | - msg["To"] = recipient.strip() |
217 | | - server.send_message(msg) |
218 | | - del msg["To"] |
| 240 | + clean_recipient = recipient.strip() |
| 241 | + if not clean_recipient: |
| 242 | + logger.info("⚠️ Empty recipient, skipping") |
| 243 | + continue |
| 244 | + |
| 245 | + try: |
| 246 | + msg["To"] = clean_recipient |
| 247 | + print(f"📤 Sending to: {clean_recipient}") |
| 248 | + server.send_message(msg) |
| 249 | + del msg["To"] |
| 250 | + success_count += 1 |
| 251 | + print(f"✅ Successfully sent to: {clean_recipient}") |
| 252 | + except Exception as send_error: |
| 253 | + print(f"❌ Failed to send to {clean_recipient}: {send_error}") |
219 | 254 |
|
220 | 255 | server.quit() |
221 | | - print(f"📧 Email sent to {len(self.recipients)} recipients") |
| 256 | + print(f"📬 Email sending complete. Successfully sent to {success_count}/{len(self.recipients)} recipients") |
222 | 257 |
|
| 258 | + 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')}") |
223 | 262 | except Exception as e: |
224 | | - print(f"❌ Email error: {e}") |
| 263 | + import traceback |
| 264 | + print(f"❌ Unexpected error: {e}") |
| 265 | + logger.info("📝 Stack trace:") |
| 266 | + traceback.print_exc() |
225 | 267 |
|
226 | 268 |
|
227 | 269 | class HTTPDestination(Destination): |
@@ -297,7 +339,7 @@ def __init__(self, uri: str): |
297 | 339 | async def send(self, message: Any) -> None: |
298 | 340 | """Log message to console and optionally to a file""" |
299 | 341 | log_msg = f"📝 {datetime.now().isoformat()}: {message}" |
300 | | - print(log_msg) |
| 342 | + logger.info(log_msg) |
301 | 343 |
|
302 | 344 | if self.log_file: |
303 | 345 | try: |
|
0 commit comments