-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_email_detailed.py
More file actions
324 lines (258 loc) · 11.8 KB
/
Copy pathdebug_email_detailed.py
File metadata and controls
324 lines (258 loc) · 11.8 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/env python
"""
Debug Email Configuration and Test Email Sending
This script helps debug email configuration issues and tests email sending functionality.
It provides detailed diagnostic information and tests basic email functionality.
"""
import os
import sys
import django
# Add the project root to Python path
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
# Setup Django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'backend.settings')
django.setup()
from django.conf import settings
from django.core.mail import send_mail
from django.contrib.auth.models import User
from datetime import datetime
def debug_email_configuration():
"""Debug and display email configuration."""
print("=" * 80)
print("EMAIL CONFIGURATION DEBUG")
print("=" * 80)
# Check all email-related settings
email_settings = [
'EMAIL_BACKEND',
'EMAIL_HOST',
'EMAIL_PORT',
'EMAIL_USE_TLS',
'EMAIL_USE_SSL',
'EMAIL_HOST_USER',
'EMAIL_HOST_PASSWORD',
'DEFAULT_FROM_EMAIL'
]
for setting in email_settings:
value = getattr(settings, setting, 'NOT SET')
if setting == 'EMAIL_HOST_PASSWORD':
# Don't show the actual password
display_value = 'SET' if value and value != 'NOT SET' else 'NOT SET'
print(f"[CONFIG] {setting}: {display_value}")
else:
print(f"[CONFIG] {setting}: {value}")
print("\n[CONFIG] Additional email settings:")
print(f"[CONFIG] EMAIL_TIMEOUT: {getattr(settings, 'EMAIL_TIMEOUT', 'NOT SET')}")
print(f"[CONFIG] EMAIL_SSL_CERTFILE: {getattr(settings, 'EMAIL_SSL_CERTFILE', 'NOT SET')}")
print(f"[CONFIG] EMAIL_SSL_KEYFILE: {getattr(settings, 'EMAIL_SSL_KEYFILE', 'NOT SET')}")
# Validate configuration
print("\n[VALIDATION] Configuration validation:")
if not getattr(settings, 'EMAIL_BACKEND', None):
print("[ERROR] EMAIL_BACKEND not set!")
elif settings.EMAIL_BACKEND == 'django.core.mail.backends.console.EmailBackend':
print("[WARNING] Using console backend - emails will be printed to console only")
elif settings.EMAIL_BACKEND == 'django.core.mail.backends.dummy.EmailBackend':
print("[WARNING] Using dummy backend - emails will be discarded")
elif settings.EMAIL_BACKEND == 'django.core.mail.backends.smtp.EmailBackend':
print("[INFO] Using SMTP backend - emails will be sent via SMTP")
if not getattr(settings, 'EMAIL_HOST', None):
print("[ERROR] EMAIL_HOST not set!")
if not getattr(settings, 'EMAIL_HOST_USER', None):
print("[ERROR] EMAIL_HOST_USER not set!")
if not getattr(settings, 'EMAIL_HOST_PASSWORD', None):
print("[ERROR] EMAIL_HOST_PASSWORD not set!")
if not getattr(settings, 'DEFAULT_FROM_EMAIL', None):
print("[ERROR] DEFAULT_FROM_EMAIL not set!")
# Gmail-specific checks
if getattr(settings, 'EMAIL_HOST', '') == 'smtp.gmail.com':
print("\n[GMAIL] Gmail-specific configuration:")
if getattr(settings, 'EMAIL_PORT', None) == 587:
print("[GMAIL] ✅ Port 587 is correct for Gmail with TLS")
elif getattr(settings, 'EMAIL_PORT', None) == 465:
print("[GMAIL] ⚠️ Port 465 requires EMAIL_USE_SSL=True (not EMAIL_USE_TLS)")
else:
print(f"[GMAIL] ❌ Port {getattr(settings, 'EMAIL_PORT', 'NOT SET')} may not work with Gmail")
if getattr(settings, 'EMAIL_USE_TLS', False):
print("[GMAIL] ✅ TLS is enabled")
else:
print("[GMAIL] ❌ TLS should be enabled for Gmail")
print("[GMAIL] 📝 Remember to use an App Password, not your regular password!")
print("[GMAIL] 📝 Generate App Password at: https://myaccount.google.com/apppasswords")
def test_smtp_connection():
"""Test SMTP connection without sending email."""
print("\n" + "=" * 80)
print("SMTP CONNECTION TEST")
print("=" * 80)
try:
from django.core.mail import get_connection
print("[SMTP] Testing SMTP connection...")
connection = get_connection(
backend=settings.EMAIL_BACKEND,
host=getattr(settings, 'EMAIL_HOST', None),
port=getattr(settings, 'EMAIL_PORT', None),
username=getattr(settings, 'EMAIL_HOST_USER', None),
password=getattr(settings, 'EMAIL_HOST_PASSWORD', None),
use_tls=getattr(settings, 'EMAIL_USE_TLS', False),
use_ssl=getattr(settings, 'EMAIL_USE_SSL', False),
)
print("[SMTP] Opening connection...")
connection.open()
print("[SMTP] ✅ Connection opened successfully!")
print("[SMTP] Closing connection...")
connection.close()
print("[SMTP] ✅ Connection closed successfully!")
return True
except Exception as e:
print(f"[SMTP] ❌ Connection failed: {str(e)}")
print(f"[SMTP] Error type: {type(e).__name__}")
# Specific error diagnostics
error_str = str(e).lower()
if "connection refused" in error_str:
print("[SMTP] 🔧 DIAGNOSIS: SMTP server refused connection")
print("[SMTP] - Check EMAIL_HOST and EMAIL_PORT")
print("[SMTP] - Verify the SMTP server is running")
print("[SMTP] - Check firewall settings")
elif "authentication failed" in error_str or "invalid credentials" in error_str:
print("[SMTP] 🔧 DIAGNOSIS: Authentication failed")
print("[SMTP] - Check EMAIL_HOST_USER and EMAIL_HOST_PASSWORD")
print("[SMTP] - For Gmail, use App Password instead of regular password")
elif "tls" in error_str or "ssl" in error_str:
print("[SMTP] 🔧 DIAGNOSIS: TLS/SSL issue")
print("[SMTP] - Check EMAIL_USE_TLS and EMAIL_USE_SSL settings")
print("[SMTP] - Verify SMTP server supports the selected encryption")
elif "timeout" in error_str:
print("[SMTP] 🔧 DIAGNOSIS: Connection timeout")
print("[SMTP] - Check network connectivity")
print("[SMTP] - Try a different EMAIL_PORT")
return False
def test_basic_email():
"""Test sending a basic email."""
print("\n" + "=" * 80)
print("BASIC EMAIL TEST")
print("=" * 80)
# Get test recipient
test_email = input("Enter test email address (or press Enter to skip): ").strip()
if not test_email:
print("[EMAIL] No test email provided, skipping basic email test")
return False
print(f"[EMAIL] Testing basic email to: {test_email}")
subject = "FTV Email Test - Basic Functionality"
message = f"""
Hello!
This is a test email from the FTV system.
Test details:
- Timestamp: {datetime.now().isoformat()}
- From: {settings.DEFAULT_FROM_EMAIL}
- Backend: {settings.EMAIL_BACKEND}
If you receive this email, the basic email configuration is working.
Best regards,
FTV System
"""
try:
print("[EMAIL] Sending test email...")
send_mail(
subject=subject,
message=message,
from_email=settings.DEFAULT_FROM_EMAIL,
recipient_list=[test_email],
fail_silently=False,
)
print("[EMAIL] ✅ Test email sent successfully!")
print("[EMAIL] Please check the recipient's inbox (and spam folder)")
return True
except Exception as e:
print(f"[EMAIL] ❌ Failed to send test email: {str(e)}")
print(f"[EMAIL] Error type: {type(e).__name__}")
return False
def test_html_email():
"""Test sending an HTML email using the new template system."""
print("\n" + "=" * 80)
print("HTML EMAIL TEMPLATE TEST")
print("=" * 80)
# Get test recipient
test_email = input("Enter test email address for HTML test (or press Enter to skip): ").strip()
if not test_email:
print("[HTML] No test email provided, skipping HTML email test")
return False
print(f"[HTML] Testing HTML email to: {test_email}")
try:
from backend.email_templates import get_base_email_template
# Create test content
content = """
<div class="content-section">
<h2>🧪 HTML Email Test</h2>
<p>This is a test of the new FTV HTML email template system.</p>
<p>If you can see this formatted email with the FTV branding, the HTML templates are working correctly!</p>
</div>
<div class="info-box">
<h3>Test Details</h3>
<div class="info-item"><strong>Timestamp:</strong> {timestamp}</div>
<div class="info-item"><strong>Template:</strong> Base FTV Email Template</div>
<div class="info-item"><strong>Status:</strong> HTML Email Support Active</div>
</div>
""".format(timestamp=datetime.now().isoformat())
# Generate HTML email
html_message = get_base_email_template(
title="HTML Email Test",
content=content,
button_text="Visit FTV System",
button_url="https://ftv.szlg.info"
)
# Plain text version
plain_message = f"""
HTML Email Test
This is a test of the new FTV HTML email template system.
Test Details:
- Timestamp: {datetime.now().isoformat()}
- Template: Base FTV Email Template
- Status: HTML Email Support Active
If you receive this email, the HTML email templates are working correctly!
Visit: https://ftv.szlg.info
"""
print("[HTML] Sending HTML email...")
send_mail(
subject="FTV HTML Email Test",
message=plain_message,
html_message=html_message,
from_email=settings.DEFAULT_FROM_EMAIL,
recipient_list=[test_email],
fail_silently=False,
)
print("[HTML] ✅ HTML test email sent successfully!")
print("[HTML] Please check the recipient's inbox for the formatted email")
return True
except Exception as e:
print(f"[HTML] ❌ Failed to send HTML test email: {str(e)}")
print(f"[HTML] Error type: {type(e).__name__}")
return False
def main():
"""Run all email debugging tests."""
print("🔍 FTV Email Debug and Test Tool")
print("This tool will help diagnose email configuration issues.\n")
# Debug configuration
debug_email_configuration()
# Test SMTP connection
smtp_ok = test_smtp_connection()
if not smtp_ok:
print("\n❌ SMTP connection failed. Please fix configuration issues before testing emails.")
return False
# Test basic email
basic_ok = test_basic_email()
# Test HTML email
html_ok = test_html_email()
# Summary
print("\n" + "=" * 80)
print("EMAIL TESTING SUMMARY")
print("=" * 80)
print(f"SMTP Connection: {'✅ PASS' if smtp_ok else '❌ FAIL'}")
print(f"Basic Email: {'✅ PASS' if basic_ok else '⏭️ SKIPPED'}")
print(f"HTML Email: {'✅ PASS' if html_ok else '⏭️ SKIPPED'}")
if smtp_ok and (basic_ok or html_ok):
print("\n🎉 Email system is working!")
print("You can now send emails from the FTV system.")
else:
print("\n❌ Email system has issues.")
print("Please review the error messages above and fix the configuration.")
return smtp_ok and (basic_ok or html_ok)
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)