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