Add email debugging and better error handling

- Add debug_railway_email.py script for Railway email configuration testing
- Add management command test_email for Railway email testing
- Improve error handling and logging in forgot password view
- Add detailed email configuration debug information

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Claude 2025-07-16 13:17:15 +05:30
parent d36cf9a79d
commit 498de2219e
5 changed files with 115 additions and 1 deletions

View File

View File

@ -0,0 +1,39 @@
from django.core.management.base import BaseCommand
from django.core.mail import send_mail
from django.conf import settings
from authentication.models import User
class Command(BaseCommand):
help = 'Test email configuration on Railway'
def add_arguments(self, parser):
parser.add_argument('--email', type=str, help='Email address to send test to')
def handle(self, *args, **options):
self.stdout.write("🔍 Testing email configuration...")
# Check settings
self.stdout.write(f"EMAIL_BACKEND: {settings.EMAIL_BACKEND}")
self.stdout.write(f"EMAIL_HOST: {settings.EMAIL_HOST}")
self.stdout.write(f"EMAIL_HOST_USER: {settings.EMAIL_HOST_USER}")
self.stdout.write(f"DEFAULT_FROM_EMAIL: {settings.DEFAULT_FROM_EMAIL}")
# Get email to send to
email = options.get('email') or settings.EMAIL_HOST_USER
self.stdout.write(f"Sending test email to: {email}")
try:
send_mail(
'Railway Email Test',
'This is a test email from Railway deployment to verify email functionality.',
settings.DEFAULT_FROM_EMAIL,
[email],
fail_silently=False,
)
self.stdout.write(
self.style.SUCCESS(f'✅ Email sent successfully to {email}!')
)
except Exception as e:
self.stdout.write(
self.style.ERROR(f'❌ Failed to send email: {str(e)}')
)

View File

@ -152,8 +152,19 @@ NetCop Team
fail_silently=False,
)
messages.success(request, 'Password reset instructions have been sent to your email.')
# Log successful email for debugging
import logging
logger = logging.getLogger(__name__)
logger.info(f"Password reset email sent successfully to {email}")
except Exception as e:
messages.error(request, 'Failed to send reset email. Please try again later.')
# Log the actual error for debugging
import logging
logger = logging.getLogger(__name__)
logger.error(f"Failed to send password reset email to {email}: {str(e)}")
messages.error(request, f'Failed to send reset email: {str(e)}')
except User.DoesNotExist:
# Don't reveal if email exists or not for security

64
debug_railway_email.py Normal file
View File

@ -0,0 +1,64 @@
#!/usr/bin/env python
import os
import django
from django.conf import settings
# Setup Django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'netcop_hub.settings')
django.setup()
def debug_railway_email():
print("🔍 RAILWAY EMAIL CONFIGURATION DEBUG")
print("=" * 50)
# Check environment
railway_env = os.environ.get('RAILWAY_ENVIRONMENT', 'Not set')
print(f"RAILWAY_ENVIRONMENT: {railway_env}")
# Check email settings
print(f"EMAIL_BACKEND: {getattr(settings, 'EMAIL_BACKEND', 'Not set')}")
print(f"EMAIL_HOST: {getattr(settings, 'EMAIL_HOST', 'Not set')}")
print(f"EMAIL_PORT: {getattr(settings, 'EMAIL_PORT', 'Not set')}")
print(f"EMAIL_USE_TLS: {getattr(settings, 'EMAIL_USE_TLS', 'Not set')}")
print(f"EMAIL_HOST_USER: {getattr(settings, 'EMAIL_HOST_USER', 'Not set')}")
print(f"EMAIL_HOST_PASSWORD: {'*' * len(settings.EMAIL_HOST_PASSWORD) if getattr(settings, 'EMAIL_HOST_PASSWORD', '') else 'Not set'}")
print(f"DEFAULT_FROM_EMAIL: {getattr(settings, 'DEFAULT_FROM_EMAIL', 'Not set')}")
print(f"SITE_URL: {getattr(settings, 'SITE_URL', 'Not set')}")
print("\n" + "=" * 50)
# Check if email is configured properly
if not getattr(settings, 'EMAIL_HOST_USER', '') or not getattr(settings, 'EMAIL_HOST_PASSWORD', ''):
print("❌ EMAIL CONFIGURATION INCOMPLETE")
print("Missing EMAIL_HOST_USER or EMAIL_HOST_PASSWORD")
print("\nTo fix this, add these environment variables to Railway:")
print("EMAIL_BACKEND=django.core.mail.backends.smtp.EmailBackend")
print("EMAIL_HOST=smtp.gmail.com")
print("EMAIL_PORT=587")
print("EMAIL_USE_TLS=True")
print("EMAIL_HOST_USER=thecyberlearn@gmail.com")
print("EMAIL_HOST_PASSWORD=ueqd ulan xcwl cfrr")
print("DEFAULT_FROM_EMAIL=NetCop <thecyberlearn@gmail.com>")
return False
# Test email sending
print("📧 Testing email sending...")
try:
from django.core.mail import send_mail
send_mail(
'Railway Email Test',
'This is a test email from Railway deployment.',
settings.DEFAULT_FROM_EMAIL,
[settings.EMAIL_HOST_USER],
fail_silently=False,
)
print("✅ Email sent successfully!")
return True
except Exception as e:
print(f"❌ Email sending failed: {e}")
return False
if __name__ == "__main__":
debug_railway_email()