|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Simple script to test SMTP connection using environment variables. |
| 4 | +""" |
| 5 | +import os |
| 6 | +import smtplib |
| 7 | +from email.mime.text import MIMEText |
| 8 | +from email.mime.multipart import MIMEMultipart |
| 9 | +from dotenv import load_dotenv |
| 10 | + |
| 11 | +def check_smtp(): |
| 12 | + # Load environment variables from .env file |
| 13 | + load_dotenv() |
| 14 | + |
| 15 | + # Required environment variables |
| 16 | + required_vars = [ |
| 17 | + 'SMTP_SERVER', |
| 18 | + 'SMTP_PORT', |
| 19 | + 'SMTP_USER', |
| 20 | + 'SMTP_PASS', |
| 21 | + 'ALERT_EMAIL' |
| 22 | + ] |
| 23 | + |
| 24 | + # Check if all required variables are set |
| 25 | + missing_vars = [var for var in required_vars if not os.getenv(var)] |
| 26 | + if missing_vars: |
| 27 | + print(f"❌ Missing required environment variables: {', '.join(missing_vars)}") |
| 28 | + print("\nPlease add them to your .env file:") |
| 29 | + for var in missing_vars: |
| 30 | + print(f"{var}=your_value_here") |
| 31 | + return False |
| 32 | + |
| 33 | + # Get SMTP settings |
| 34 | + smtp_server = os.getenv('SMTP_SERVER') |
| 35 | + smtp_port = int(os.getenv('SMTP_PORT', '587')) |
| 36 | + smtp_user = os.getenv('SMTP_USER') |
| 37 | + smtp_pass = os.getenv('SMTP_PASS') |
| 38 | + to_email = os.getenv('ALERT_EMAIL') |
| 39 | + |
| 40 | + print(f"🔧 Testing SMTP connection to {smtp_server}:{smtp_port}") |
| 41 | + print(f"🔑 Using username: {smtp_user}") |
| 42 | + |
| 43 | + try: |
| 44 | + # Create message |
| 45 | + msg = MIMEMultipart() |
| 46 | + msg['From'] = smtp_user |
| 47 | + msg['To'] = to_email |
| 48 | + msg['Subject'] = 'SMTP Test from DialogChain' |
| 49 | + |
| 50 | + body = """ |
| 51 | + This is a test email to verify SMTP configuration. |
| 52 | + |
| 53 | + If you're reading this, the SMTP configuration is working correctly! |
| 54 | + """ |
| 55 | + msg.attach(MIMEText(body, 'plain')) |
| 56 | + |
| 57 | + # Connect to server and send email |
| 58 | + with smtplib.SMTP(smtp_server, smtp_port) as server: |
| 59 | + server.starttls() |
| 60 | + print(f"🔒 Attempting to authenticate with {smtp_user}:{'*' * len(smtp_pass)}") |
| 61 | + server.login(smtp_user, smtp_pass) |
| 62 | + print("✅ Successfully authenticated with SMTP server") |
| 63 | + |
| 64 | + print(f"📤 Sending test email to {to_email}...") |
| 65 | + server.send_message(msg) |
| 66 | + print("✅ Test email sent successfully!") |
| 67 | + return True |
| 68 | + |
| 69 | + except Exception as e: |
| 70 | + print(f"❌ Error: {e}") |
| 71 | + return False |
| 72 | + |
| 73 | +if __name__ == "__main__": |
| 74 | + check_smtp() |
0 commit comments