-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_smtp.py
More file actions
71 lines (57 loc) · 2.38 KB
/
test_smtp.py
File metadata and controls
71 lines (57 loc) · 2.38 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
import smtplib
import ssl
import os
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from dotenv import load_dotenv
def test_smtp_connection():
"""Test SMTP server connection and authentication"""
print("\n🔍 Testing SMTP connection...")
# Load environment variables
load_dotenv()
# Get SMTP settings from environment
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")
alert_email = os.getenv("ALERT_EMAIL")
if not all([smtp_server, smtp_user, smtp_pass, alert_email]):
print("❌ Missing required SMTP environment variables. Please set these in your .env file:")
print("SMTP_SERVER=smtp.example.com")
print("SMTP_PORT=587")
print("SMTP_USER=your_username")
print("SMTP_PASS=your_password")
print("ALERT_EMAIL=your@email.com")
return False
try:
# Create a secure SSL context
context = ssl.create_default_context()
# Try to log in to server and send email
with smtplib.SMTP(smtp_server, smtp_port) as server:
print(f"✅ Connected to SMTP server: {smtp_server}:{smtp_port}")
# Start TLS encryption
server.starttls(context=context)
print("✅ Started TLS encryption")
# Login
server.login(smtp_user, smtp_pass)
print("✅ Successfully authenticated with SMTP server")
# Create message
msg = MIMEMultipart()
msg['From'] = smtp_user
msg['To'] = alert_email
msg['Subject'] = 'DialogChain SMTP Test'
msg.attach(MIMEText('This is a test email from DialogChain SMTP test.', 'plain'))
# Send email
server.send_message(msg)
print(f"✅ Sent test email to: {alert_email}")
return True
except Exception as e:
print(f"❌ SMTP test failed: {e}")
return False
if __name__ == "__main__":
print("🚀 Starting SMTP connection test...")
success = test_smtp_connection()
if success:
print("\n✅ SMTP test completed successfully!")
else:
print("\n❌ SMTP test failed. Check the error messages above.")