import sys import smtplib import os from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart # Ensure UTF-8 output for terminal/logging if hasattr(sys.stdout, 'reconfigure'): sys.stdout.reconfigure(encoding='utf-8') # Load configuration from environment or fallback defaults SMTP_SERVER = os.environ.get("SMTP_SERVER", "smtp.gmail.com") SMTP_PORT = int(os.environ.get("SMTP_PORT", 587)) SENDER_EMAIL = os.environ.get("SENDER_EMAIL", "krishnaowoxd@gmail.com") SENDER_PASSWORD = os.environ.get("SENDER_PASSWORD", "qkwylwtcsnlrpree").replace(" ", "") # Load recipients list from recipients.txt file recipients_file = os.path.join(os.path.dirname(__file__), "recipients.txt") recipients = [] if os.path.exists(recipients_file): with open(recipients_file, "r", encoding="utf-8") as f: recipients = [line.strip() for line in f if line.strip() and not line.startswith("#")] if not recipients: env_recipient = os.environ.get("RECIPIENT_EMAIL", "dorutoslayer@gmail.com") recipients = [r.strip() for r in env_recipient.split(",") if r.strip()] recipient_str = ", ".join(recipients) if len(sys.argv) < 7: print("Usage: python send_git_email.py [deploy_status]") sys.exit(1) branch = sys.argv[1] author = sys.argv[2] commit_hash = sys.argv[3] timestamp = sys.argv[4] message = sys.argv[5] changed_files = sys.argv[6] deploy_status = sys.argv[7] if len(sys.argv) > 7 else os.environ.get("DEPLOY_STATUS", "SUCCESS") is_success = "SUCCESS" in deploy_status.upper() or "ONLINE" in deploy_status.upper() status_bg = "#ecfdf5" if is_success else "#fef2f2" status_border = "#10b981" if is_success else "#ef4444" status_text = "#047857" if is_success else "#b91c1c" status_icon = "✅" if is_success else "❌" subject = f"[Freshers-Hub VPS] {'🟢 ONLINE' if is_success else '🔴 FAILED'} - Deployment Report {commit_hash} ({branch})" html_content = f"""
🚀 RIT Freshers-Hub CI/CD Report
{status_icon} Deployment Status: {deploy_status.upper()}
Branch: {branch}
Author: {author}
Commit Hash: {commit_hash}
Deployment Time: {timestamp}
Commit Message:
{message}
Verified Microservices Online:
🟢 springboot.service (Java API :8085)
🟢 chatbot.service (Go AI Engine :8081)
🟢 telegram-bot.service (Python Intermediary :8082)
🟢 nginx.service (Web Server :80/:443)
Changed Files (Top Modified):
{changed_files}
""" msg = MIMEMultipart("alternative") msg["Subject"] = subject msg["From"] = SENDER_EMAIL msg["To"] = recipient_str msg.attach(MIMEText(html_content, "html")) try: with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server: server.starttls() server.login(SENDER_EMAIL, SENDER_PASSWORD) server.sendmail(SENDER_EMAIL, recipients, msg.as_string()) print("✅ Email report sent successfully.") except Exception as e: print(f"❌ Failed to send email report: {e}")