-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_email.py
More file actions
123 lines (98 loc) · 4.12 KB
/
test_email.py
File metadata and controls
123 lines (98 loc) · 4.12 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from dotenv import load_dotenv
import os
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
load_dotenv()
def test_email_connection():
"""Test if email credentials work"""
sender = os.getenv("NOTIFY_SENDER_EMAIL")
app_password = os.getenv("NOTIFY_APP_PASSWORD")
recipient = os.getenv("NOTIFY_RECIPIENT")
print("=" * 60)
print("TESTING EMAIL CONFIGURATION")
print("=" * 60)
# Check if credentials exist
print(f"\n✓ Sender Email: {sender}")
print(f"✓ Recipient Email: {recipient}")
print(f"✓ App Password: {'*' * len(app_password) if app_password else 'MISSING'}")
if not sender or not app_password or not recipient:
print("\n❌ ERROR: Email credentials missing in .env file!")
return False
print("\n📧 Attempting to send test email...")
try:
# Create test message
msg = MIMEMultipart()
msg["Subject"] = "🧪 TEST: Pothole Detection System"
msg["From"] = sender
msg["To"] = recipient
body = """
TEST EMAIL FROM POTHOLE DETECTION SYSTEM
=========================================
If you receive this email, your email configuration is working correctly!
This is a test message to verify that the pothole detection system
can successfully send alerts to authorities.
Configuration Details:
- Sender: {}
- Recipient: {}
- SMTP Server: smtp.gmail.com:587
Next Step: Upload an image with potholes to test automatic alerts.
---
Automated Pothole Detection System
""".format(sender, recipient)
msg.attach(MIMEText(body, 'plain'))
# Try to add a sample image if exists
sample_image_path = 'static/results/detected'
if os.path.exists(sample_image_path):
images = [f for f in os.listdir(sample_image_path) if f.endswith(('.jpg', '.png'))]
if images:
img_path = os.path.join(sample_image_path, images[0])
print(f"📎 Attaching sample image: {images[0]}")
with open(img_path, 'rb') as f:
img_data = f.read()
image = MIMEImage(img_data, name=images[0])
msg.attach(image)
# Connect and send
print("🔌 Connecting to Gmail SMTP server...")
with smtplib.SMTP("smtp.gmail.com", 587, timeout=10) as server:
print("🔐 Starting TLS encryption...")
server.starttls()
print("🔑 Logging in...")
server.login(sender, app_password)
print("📤 Sending email...")
server.send_message(msg)
print("\n" + "=" * 60)
print("✅ SUCCESS! Test email sent successfully!")
print("=" * 60)
print(f"\n📬 Check your inbox at: {recipient}")
print(" (Check spam folder if not in inbox)\n")
return True
except smtplib.SMTPAuthenticationError as e:
print("\n" + "=" * 60)
print("❌ AUTHENTICATION FAILED!")
print("=" * 60)
print("\nPossible issues:")
print("1. App Password is incorrect")
print("2. 2-Step Verification not enabled on Gmail")
print("3. App Password not generated correctly")
print("\nHow to fix:")
print("1. Go to: https://myaccount.google.com/apppasswords")
print("2. Generate new App Password for 'Mail'")
print("3. Update NOTIFY_APP_PASSWORD in .env file")
print(f"\nError details: {e}\n")
return False
except smtplib.SMTPException as e:
print("\n" + "=" * 60)
print("❌ SMTP ERROR!")
print("=" * 60)
print(f"\nError: {e}\n")
return False
except Exception as e:
print("\n" + "=" * 60)
print("❌ UNEXPECTED ERROR!")
print("=" * 60)
print(f"\nError: {e}\n")
return False
if __name__ == "__main__":
test_email_connection()