diff --git a/authentication/management/__init__.py b/authentication/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/authentication/management/commands/__init__.py b/authentication/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/authentication/management/commands/test_email.py b/authentication/management/commands/test_email.py new file mode 100644 index 0000000..37226f9 --- /dev/null +++ b/authentication/management/commands/test_email.py @@ -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)}') + ) \ No newline at end of file diff --git a/authentication/views.py b/authentication/views.py index 3f85af9..7a11654 100644 --- a/authentication/views.py +++ b/authentication/views.py @@ -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 diff --git a/debug_railway_email.py b/debug_railway_email.py new file mode 100644 index 0000000..63c5964 --- /dev/null +++ b/debug_railway_email.py @@ -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 ") + 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() \ No newline at end of file