import smtplib import os import json from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from datetime import datetime def send_error_report_email(error_reports): """ Envoie un email avec le rapport d'erreurs et le JSON intΓ©grΓ© """ smtp_server = os.getenv("SMTP_SERVER") smtp_port = int(os.getenv("SMTP_PORT", 587)) smtp_email = os.getenv("SMTP_EMAIL") # main email address. If smt_from_alias is not, it will also be the mail sender smtp_password = os.getenv("SMTP_PASSWORD") smtp_from_alias = os.getenv("SMTP_FROM_ALIAS", smtp_email) # mail sender if not none (alias) admin_email = os.getenv("ADMIN_EMAIL") # this one will receive the mail if not all([smtp_server, smtp_email, smtp_password, admin_email]): print("❌ Configuration email manquante dans .env") return False try: # Create message msg = MIMEMultipart() if smtp_from_alias is not None: smtp_from = smtp_from_alias else: smtp_from = smtp_email msg['From'] = f"Air Paradis Monitor <{smtp_from}>" msg['To'] = admin_email msg['Subject'] = f"🚨 Rapport d'erreurs - PrΓ©dictions sentiments ({datetime.now().strftime('%d/%m/%Y %H:%M')})" # Prepare JSON data json_data = { "report_generated_on": datetime.now().isoformat(), "reports_amount": len(error_reports), "reports": [ { "id": i, "tweet": tweet, "incorrect_prediction": prediction, "tweet_length": len(tweet) } for i, (tweet, prediction) in enumerate(error_reports.items(), 1) ], "metadata": { "model": "SentimentAnalysisLSTM", "api_version": "1.0", "reporting_threshold": 3 } } # Message body with embedded JSON body = f""" Hello, A new error report has been generated for the Air Paradis sentiment prediction API. πŸ“Š Total number of reports : {len(error_reports)} πŸ“… Report date : {datetime.now().strftime('%d/%m/%Y Γ  %H:%M')} πŸ“‹ Report summary : """ for i, (tweet, prediction) in enumerate(error_reports.items(), 1): body += f"{i}. Tweet: \"{tweet[:100]}{'...' if len(tweet) > 100 else ''}\"\n" body += f" Prediction reported as incorrect: {prediction.upper()}\n\n" body += """ πŸ“ˆ Recommended actions: - Analyze reported tweets to identify patterns - Consider re-training the model if necessary - Check quality of training data πŸ“Š COMPLETE DATA (JSON) : ═══════════════════════════════════════════════════════════════════════════════ """ # βœ… Directly add JSON inside the body json_string = json.dumps(json_data, ensure_ascii=False, indent=2) body += f"```json\n{json_string}\n```" body += """ ═══════════════════════════════════════════════════════════════════════════════ --- Sincerely, πŸ€– API Air Paradis - Automatic monitoring system πŸ“§ Sent from : """ + smtp_from # Add message body msg.attach(MIMEText(body, 'plain', 'utf-8')) # Sending email with smtplib.SMTP(smtp_server, smtp_port) as server: server.starttls() server.login(smtp_email, smtp_password) server.send_message(msg) print(f"βœ… Report email successfully sent from {smtp_from}") print("πŸ“Š JSON embedded in message body") return True except Exception as e: print(f"❌ Error during email sending : {e}") return False